@@ -42,7 +42,7 @@ impl<'tcx> UnsafetyVisitor<'_, 'tcx> {
4242 self . warn_unused_unsafe (
4343 hir_id,
4444 block_span,
45- Some ( self . tcx . sess . source_map ( ) . guess_head_span ( enclosing_span) ) ,
45+ Some ( ( self . tcx . sess . source_map ( ) . guess_head_span ( enclosing_span) , "block" ) ) ,
4646 ) ;
4747 f ( self ) ;
4848 } else {
@@ -52,7 +52,15 @@ impl<'tcx> UnsafetyVisitor<'_, 'tcx> {
5252 f ( self ) ;
5353
5454 if let SafetyContext :: UnsafeBlock { used : false , span, hir_id } = self . safety_context {
55- self . warn_unused_unsafe ( hir_id, span, self . body_unsafety . unsafe_fn_sig_span ( ) ) ;
55+ self . warn_unused_unsafe (
56+ hir_id,
57+ span,
58+ if self . unsafe_op_in_unsafe_fn_allowed ( ) {
59+ self . body_unsafety . unsafe_fn_sig_span ( ) . map ( |span| ( span, "fn" ) )
60+ } else {
61+ None
62+ } ,
63+ ) ;
5664 }
5765 self . safety_context = prev_context;
5866 return ;
@@ -108,18 +116,15 @@ impl<'tcx> UnsafetyVisitor<'_, 'tcx> {
108116 & self ,
109117 hir_id : hir:: HirId ,
110118 block_span : Span ,
111- enclosing_span : Option < Span > ,
119+ enclosing_unsafe : Option < ( Span , & ' static str ) > ,
112120 ) {
113121 let block_span = self . tcx . sess . source_map ( ) . guess_head_span ( block_span) ;
114122 self . tcx . struct_span_lint_hir ( UNUSED_UNSAFE , hir_id, block_span, |lint| {
115123 let msg = "unnecessary `unsafe` block" ;
116124 let mut db = lint. build ( msg) ;
117125 db. span_label ( block_span, msg) ;
118- if let Some ( enclosing_span) = enclosing_span {
119- db. span_label (
120- enclosing_span,
121- format ! ( "because it's nested under this `unsafe` block" ) ,
122- ) ;
126+ if let Some ( ( span, kind) ) = enclosing_unsafe {
127+ db. span_label ( span, format ! ( "because it's nested under this `unsafe` {}" , kind) ) ;
123128 }
124129 db. emit ( ) ;
125130 } ) ;
0 commit comments