@@ -91,7 +91,12 @@ impl<'tcx> UnsafetyVisitor<'_, 'tcx> {
91
91
kind. emit_unsafe_op_in_unsafe_fn_lint ( self . tcx , self . hir_context , span) ;
92
92
}
93
93
SafetyContext :: Safe => {
94
- kind. emit_requires_unsafe_err ( self . tcx , span, unsafe_op_in_unsafe_fn_allowed) ;
94
+ kind. emit_requires_unsafe_err (
95
+ self . tcx ,
96
+ span,
97
+ self . hir_context ,
98
+ unsafe_op_in_unsafe_fn_allowed,
99
+ ) ;
95
100
}
96
101
}
97
102
}
@@ -602,98 +607,164 @@ impl UnsafeOpKind {
602
607
& self ,
603
608
tcx : TyCtxt < ' _ > ,
604
609
span : Span ,
610
+ hir_context : hir:: HirId ,
605
611
unsafe_op_in_unsafe_fn_allowed : bool ,
606
612
) {
613
+ let note_non_inherited = tcx. hir ( ) . parent_iter ( hir_context) . find ( |( id, node) | {
614
+ if let hir:: Node :: Expr ( block) = node
615
+ && let hir:: ExprKind :: Block ( block, _) = block. kind
616
+ && let hir:: BlockCheckMode :: UnsafeBlock ( _) = block. rules
617
+ {
618
+ true
619
+ }
620
+ else if let Some ( sig) = tcx. hir ( ) . fn_sig_by_hir_id ( * id)
621
+ && sig. header . is_unsafe ( )
622
+ {
623
+ true
624
+ } else {
625
+ false
626
+ }
627
+ } ) ;
628
+ let unsafe_not_inherited_note = if let Some ( ( id, _) ) = note_non_inherited {
629
+ let span = tcx. hir ( ) . span ( id) ;
630
+ let span = tcx. sess . source_map ( ) . guess_head_span ( span) ;
631
+ Some ( UnsafeNotInheritedNote { span } )
632
+ } else {
633
+ None
634
+ } ;
635
+
607
636
match self {
608
637
CallToUnsafeFunction ( Some ( did) ) if unsafe_op_in_unsafe_fn_allowed => {
609
638
tcx. sess . emit_err ( CallToUnsafeFunctionRequiresUnsafeUnsafeOpInUnsafeFnAllowed {
610
639
span,
640
+ unsafe_not_inherited_note,
611
641
function : & tcx. def_path_str ( * did) ,
612
642
} ) ;
613
643
}
614
644
CallToUnsafeFunction ( Some ( did) ) => {
615
645
tcx. sess . emit_err ( CallToUnsafeFunctionRequiresUnsafe {
616
646
span,
647
+ unsafe_not_inherited_note,
617
648
function : & tcx. def_path_str ( * did) ,
618
649
} ) ;
619
650
}
620
651
CallToUnsafeFunction ( None ) if unsafe_op_in_unsafe_fn_allowed => {
621
652
tcx. sess . emit_err (
622
- CallToUnsafeFunctionRequiresUnsafeNamelessUnsafeOpInUnsafeFnAllowed { span } ,
653
+ CallToUnsafeFunctionRequiresUnsafeNamelessUnsafeOpInUnsafeFnAllowed {
654
+ span,
655
+ unsafe_not_inherited_note,
656
+ } ,
623
657
) ;
624
658
}
625
659
CallToUnsafeFunction ( None ) => {
626
- tcx. sess . emit_err ( CallToUnsafeFunctionRequiresUnsafeNameless { span } ) ;
660
+ tcx. sess . emit_err ( CallToUnsafeFunctionRequiresUnsafeNameless {
661
+ span,
662
+ unsafe_not_inherited_note,
663
+ } ) ;
627
664
}
628
665
UseOfInlineAssembly if unsafe_op_in_unsafe_fn_allowed => {
629
- tcx. sess
630
- . emit_err ( UseOfInlineAssemblyRequiresUnsafeUnsafeOpInUnsafeFnAllowed { span } ) ;
666
+ tcx. sess . emit_err ( UseOfInlineAssemblyRequiresUnsafeUnsafeOpInUnsafeFnAllowed {
667
+ span,
668
+ unsafe_not_inherited_note,
669
+ } ) ;
631
670
}
632
671
UseOfInlineAssembly => {
633
- tcx. sess . emit_err ( UseOfInlineAssemblyRequiresUnsafe { span } ) ;
672
+ tcx. sess . emit_err ( UseOfInlineAssemblyRequiresUnsafe {
673
+ span,
674
+ unsafe_not_inherited_note,
675
+ } ) ;
634
676
}
635
677
InitializingTypeWith if unsafe_op_in_unsafe_fn_allowed => {
636
- tcx. sess
637
- . emit_err ( InitializingTypeWithRequiresUnsafeUnsafeOpInUnsafeFnAllowed { span } ) ;
678
+ tcx. sess . emit_err ( InitializingTypeWithRequiresUnsafeUnsafeOpInUnsafeFnAllowed {
679
+ span,
680
+ unsafe_not_inherited_note,
681
+ } ) ;
638
682
}
639
683
InitializingTypeWith => {
640
- tcx. sess . emit_err ( InitializingTypeWithRequiresUnsafe { span } ) ;
684
+ tcx. sess . emit_err ( InitializingTypeWithRequiresUnsafe {
685
+ span,
686
+ unsafe_not_inherited_note,
687
+ } ) ;
641
688
}
642
689
UseOfMutableStatic if unsafe_op_in_unsafe_fn_allowed => {
643
- tcx. sess
644
- . emit_err ( UseOfMutableStaticRequiresUnsafeUnsafeOpInUnsafeFnAllowed { span } ) ;
690
+ tcx. sess . emit_err ( UseOfMutableStaticRequiresUnsafeUnsafeOpInUnsafeFnAllowed {
691
+ span,
692
+ unsafe_not_inherited_note,
693
+ } ) ;
645
694
}
646
695
UseOfMutableStatic => {
647
- tcx. sess . emit_err ( UseOfMutableStaticRequiresUnsafe { span } ) ;
696
+ tcx. sess
697
+ . emit_err ( UseOfMutableStaticRequiresUnsafe { span, unsafe_not_inherited_note } ) ;
648
698
}
649
699
UseOfExternStatic if unsafe_op_in_unsafe_fn_allowed => {
650
- tcx. sess
651
- . emit_err ( UseOfExternStaticRequiresUnsafeUnsafeOpInUnsafeFnAllowed { span } ) ;
700
+ tcx. sess . emit_err ( UseOfExternStaticRequiresUnsafeUnsafeOpInUnsafeFnAllowed {
701
+ span,
702
+ unsafe_not_inherited_note,
703
+ } ) ;
652
704
}
653
705
UseOfExternStatic => {
654
- tcx. sess . emit_err ( UseOfExternStaticRequiresUnsafe { span } ) ;
706
+ tcx. sess
707
+ . emit_err ( UseOfExternStaticRequiresUnsafe { span, unsafe_not_inherited_note } ) ;
655
708
}
656
709
DerefOfRawPointer if unsafe_op_in_unsafe_fn_allowed => {
657
- tcx. sess
658
- . emit_err ( DerefOfRawPointerRequiresUnsafeUnsafeOpInUnsafeFnAllowed { span } ) ;
710
+ tcx. sess . emit_err ( DerefOfRawPointerRequiresUnsafeUnsafeOpInUnsafeFnAllowed {
711
+ span,
712
+ unsafe_not_inherited_note,
713
+ } ) ;
659
714
}
660
715
DerefOfRawPointer => {
661
- tcx. sess . emit_err ( DerefOfRawPointerRequiresUnsafe { span } ) ;
716
+ tcx. sess
717
+ . emit_err ( DerefOfRawPointerRequiresUnsafe { span, unsafe_not_inherited_note } ) ;
662
718
}
663
719
AccessToUnionField if unsafe_op_in_unsafe_fn_allowed => {
664
- tcx. sess
665
- . emit_err ( AccessToUnionFieldRequiresUnsafeUnsafeOpInUnsafeFnAllowed { span } ) ;
720
+ tcx. sess . emit_err ( AccessToUnionFieldRequiresUnsafeUnsafeOpInUnsafeFnAllowed {
721
+ span,
722
+ unsafe_not_inherited_note,
723
+ } ) ;
666
724
}
667
725
AccessToUnionField => {
668
- tcx. sess . emit_err ( AccessToUnionFieldRequiresUnsafe { span } ) ;
726
+ tcx. sess
727
+ . emit_err ( AccessToUnionFieldRequiresUnsafe { span, unsafe_not_inherited_note } ) ;
669
728
}
670
729
MutationOfLayoutConstrainedField if unsafe_op_in_unsafe_fn_allowed => {
671
730
tcx. sess . emit_err (
672
731
MutationOfLayoutConstrainedFieldRequiresUnsafeUnsafeOpInUnsafeFnAllowed {
673
732
span,
733
+ unsafe_not_inherited_note,
674
734
} ,
675
735
) ;
676
736
}
677
737
MutationOfLayoutConstrainedField => {
678
- tcx. sess . emit_err ( MutationOfLayoutConstrainedFieldRequiresUnsafe { span } ) ;
738
+ tcx. sess . emit_err ( MutationOfLayoutConstrainedFieldRequiresUnsafe {
739
+ span,
740
+ unsafe_not_inherited_note,
741
+ } ) ;
679
742
}
680
743
BorrowOfLayoutConstrainedField if unsafe_op_in_unsafe_fn_allowed => {
681
744
tcx. sess . emit_err (
682
- BorrowOfLayoutConstrainedFieldRequiresUnsafeUnsafeOpInUnsafeFnAllowed { span } ,
745
+ BorrowOfLayoutConstrainedFieldRequiresUnsafeUnsafeOpInUnsafeFnAllowed {
746
+ span,
747
+ unsafe_not_inherited_note,
748
+ } ,
683
749
) ;
684
750
}
685
751
BorrowOfLayoutConstrainedField => {
686
- tcx. sess . emit_err ( BorrowOfLayoutConstrainedFieldRequiresUnsafe { span } ) ;
752
+ tcx. sess . emit_err ( BorrowOfLayoutConstrainedFieldRequiresUnsafe {
753
+ span,
754
+ unsafe_not_inherited_note,
755
+ } ) ;
687
756
}
688
757
CallToFunctionWith ( did) if unsafe_op_in_unsafe_fn_allowed => {
689
758
tcx. sess . emit_err ( CallToFunctionWithRequiresUnsafeUnsafeOpInUnsafeFnAllowed {
690
759
span,
760
+ unsafe_not_inherited_note,
691
761
function : & tcx. def_path_str ( * did) ,
692
762
} ) ;
693
763
}
694
764
CallToFunctionWith ( did) => {
695
765
tcx. sess . emit_err ( CallToFunctionWithRequiresUnsafe {
696
766
span,
767
+ unsafe_not_inherited_note,
697
768
function : & tcx. def_path_str ( * did) ,
698
769
} ) ;
699
770
}
0 commit comments