@@ -163,53 +163,53 @@ pub fn check_attribute_safety(
163163 safety : AttributeSafety ,
164164 attr : & Attribute ,
165165) {
166- if features. unsafe_attributes {
167- let attr_item = attr. get_normal_item ( ) ;
166+ if !features. unsafe_attributes {
167+ return ;
168+ }
168169
169- if safety == AttributeSafety :: Unsafe {
170- if let ast:: Safety :: Default = attr_item. unsafety {
171- let path_span = attr_item. path . span ;
170+ let attr_item = attr. get_normal_item ( ) ;
172171
173- // If the `attr_item`'s span is not from a macro, then just suggest
174- // wrapping it in `unsafe(...)`. Otherwise, we suggest putting the
175- // `unsafe(`, `)` right after and right before the opening and closing
176- // square bracket respectively.
177- let diag_span = if attr_item. span ( ) . can_be_used_for_suggestions ( ) {
178- attr_item. span ( )
179- } else {
180- attr. span
181- . with_lo ( attr. span . lo ( ) + BytePos ( 2 ) )
182- . with_hi ( attr. span . hi ( ) - BytePos ( 1 ) )
183- } ;
172+ if safety == AttributeSafety :: Unsafe {
173+ if let ast:: Safety :: Default = attr_item. unsafety {
174+ let path_span = attr_item. path . span ;
184175
185- if attr. span . at_least_rust_2024 ( ) {
186- psess. dcx ( ) . emit_err ( errors:: UnsafeAttrOutsideUnsafe {
187- span : path_span,
188- suggestion : errors:: UnsafeAttrOutsideUnsafeSuggestion {
189- left : diag_span. shrink_to_lo ( ) ,
190- right : diag_span. shrink_to_hi ( ) ,
191- } ,
192- } ) ;
193- } else {
194- psess. buffer_lint (
195- UNSAFE_ATTR_OUTSIDE_UNSAFE ,
196- path_span,
197- ast:: CRATE_NODE_ID ,
198- BuiltinLintDiag :: UnsafeAttrOutsideUnsafe {
199- attribute_name_span : path_span,
200- sugg_spans : ( diag_span. shrink_to_lo ( ) , diag_span. shrink_to_hi ( ) ) ,
201- } ,
202- ) ;
203- }
204- }
205- } else {
206- if let Safety :: Unsafe ( unsafe_span) = attr_item. unsafety {
207- psess. dcx ( ) . emit_err ( errors:: InvalidAttrUnsafe {
208- span : unsafe_span,
209- name : attr_item. path . clone ( ) ,
176+ // If the `attr_item`'s span is not from a macro, then just suggest
177+ // wrapping it in `unsafe(...)`. Otherwise, we suggest putting the
178+ // `unsafe(`, `)` right after and right before the opening and closing
179+ // square bracket respectively.
180+ let diag_span = if attr_item. span ( ) . can_be_used_for_suggestions ( ) {
181+ attr_item. span ( )
182+ } else {
183+ attr. span . with_lo ( attr. span . lo ( ) + BytePos ( 2 ) ) . with_hi ( attr. span . hi ( ) - BytePos ( 1 ) )
184+ } ;
185+
186+ if attr. span . at_least_rust_2024 ( ) {
187+ psess. dcx ( ) . emit_err ( errors:: UnsafeAttrOutsideUnsafe {
188+ span : path_span,
189+ suggestion : errors:: UnsafeAttrOutsideUnsafeSuggestion {
190+ left : diag_span. shrink_to_lo ( ) ,
191+ right : diag_span. shrink_to_hi ( ) ,
192+ } ,
210193 } ) ;
194+ } else {
195+ psess. buffer_lint (
196+ UNSAFE_ATTR_OUTSIDE_UNSAFE ,
197+ path_span,
198+ ast:: CRATE_NODE_ID ,
199+ BuiltinLintDiag :: UnsafeAttrOutsideUnsafe {
200+ attribute_name_span : path_span,
201+ sugg_spans : ( diag_span. shrink_to_lo ( ) , diag_span. shrink_to_hi ( ) ) ,
202+ } ,
203+ ) ;
211204 }
212205 }
206+ } else {
207+ if let Safety :: Unsafe ( unsafe_span) = attr_item. unsafety {
208+ psess. dcx ( ) . emit_err ( errors:: InvalidAttrUnsafe {
209+ span : unsafe_span,
210+ name : attr_item. path . clone ( ) ,
211+ } ) ;
212+ }
213213 }
214214}
215215
0 commit comments