@@ -115,6 +115,7 @@ declare_lint_pass! {
115
115
UNNAMEABLE_TYPES ,
116
116
UNREACHABLE_CODE ,
117
117
UNREACHABLE_PATTERNS ,
118
+ UNSAFE_ATTR_OUTSIDE_UNSAFE ,
118
119
UNSAFE_OP_IN_UNSAFE_FN ,
119
120
UNSTABLE_NAME_COLLISIONS ,
120
121
UNSTABLE_SYNTAX_PRE_EXPANSION ,
@@ -4902,3 +4903,45 @@ declare_lint! {
4902
4903
reference: "issue #123743 <https://github.com/rust-lang/rust/issues/123743>" ,
4903
4904
} ;
4904
4905
}
4906
+
4907
+ declare_lint ! {
4908
+ /// The `unsafe_attr_outside_unsafe` lint detects a missing unsafe keyword
4909
+ /// on attributes considered unsafe.
4910
+ ///
4911
+ /// ### Example
4912
+ ///
4913
+ /// ```rust
4914
+ /// #![feature(unsafe_attributes)]
4915
+ /// #![warn(unsafe_attr_outside_unsafe)]
4916
+ ///
4917
+ /// #[no_mangle]
4918
+ /// extern "C" fn foo() {}
4919
+ ///
4920
+ /// fn main() {}
4921
+ /// ```
4922
+ ///
4923
+ /// {{produces}}
4924
+ ///
4925
+ /// ### Explanation
4926
+ ///
4927
+ /// Some attributes (e.g. `no_mangle`, `export_name`, `link_section` -- see
4928
+ /// [issue #82499] for a more complete list) are considered "unsafe" attributes.
4929
+ /// An unsafe attribute must only be used inside unsafe(...).
4930
+ ///
4931
+ /// This lint can automatically wrap the attributes in `unsafe(...)` , but this
4932
+ /// obviously cannot verify that the preconditions of the `unsafe`
4933
+ /// attributes are fulfilled, so that is still up to the user.
4934
+ ///
4935
+ /// The lint is currently "allow" by default, but that might change in the
4936
+ /// future.
4937
+ ///
4938
+ /// [editions]: https://doc.rust-lang.org/edition-guide/
4939
+ /// [issue #82499]: https://github.com/rust-lang/rust/issues/82499
4940
+ pub UNSAFE_ATTR_OUTSIDE_UNSAFE ,
4941
+ Allow ,
4942
+ "detects unsafe attributes outside of unsafe" ,
4943
+ @future_incompatible = FutureIncompatibleInfo {
4944
+ reason: FutureIncompatibilityReason :: EditionError ( Edition :: Edition2024 ) ,
4945
+ reference: "issue #123757 <https://github.com/rust-lang/rust/issues/123757>" ,
4946
+ } ;
4947
+ }
0 commit comments