@@ -93,7 +93,7 @@ impl<'tcx> UnsafetyVisitor<'_, 'tcx> {
93
93
"{} is unsafe and requires unsafe block (error E0133)" ,
94
94
description,
95
95
) )
96
- . span_label ( span, description )
96
+ . span_label ( span, kind . simple_description ( ) )
97
97
. note ( note)
98
98
. emit ( ) ;
99
99
} ,
@@ -110,7 +110,7 @@ impl<'tcx> UnsafetyVisitor<'_, 'tcx> {
110
110
description,
111
111
fn_sugg,
112
112
)
113
- . span_label ( span, description )
113
+ . span_label ( span, kind . simple_description ( ) )
114
114
. note ( note)
115
115
. emit ( ) ;
116
116
}
@@ -546,57 +546,75 @@ enum UnsafeOpKind {
546
546
use UnsafeOpKind :: * ;
547
547
548
548
impl UnsafeOpKind {
549
+ pub fn simple_description ( & self ) -> & ' static str {
550
+ match self {
551
+ CallToUnsafeFunction ( ..) => "call to unsafe function" ,
552
+ UseOfInlineAssembly => "use of inline assembly" ,
553
+ InitializingTypeWith => "initializing type with `rustc_layout_scalar_valid_range` attr" ,
554
+ UseOfMutableStatic => "use of mutable static" ,
555
+ UseOfExternStatic => "use of extern static" ,
556
+ DerefOfRawPointer => "dereference of raw pointer" ,
557
+ AssignToDroppingUnionField => "assignment to union field that might need dropping" ,
558
+ AccessToUnionField => "access to union field" ,
559
+ MutationOfLayoutConstrainedField => "mutation of layout constrained field" ,
560
+ BorrowOfLayoutConstrainedField => {
561
+ "borrow of layout constrained field with interior mutability"
562
+ }
563
+ CallToFunctionWith ( ..) => "call to function with `#[target_feature]`" ,
564
+ }
565
+ }
566
+
549
567
pub fn description_and_note ( & self , tcx : TyCtxt < ' _ > ) -> ( Cow < ' static , str > , & ' static str ) {
550
568
match self {
551
569
CallToUnsafeFunction ( did) => (
552
570
if let Some ( did) = did {
553
571
Cow :: from ( format ! ( "call to unsafe function `{}`" , tcx. def_path_str( * did) ) )
554
572
} else {
555
- Cow :: Borrowed ( "call to unsafe function" )
573
+ Cow :: Borrowed ( self . simple_description ( ) )
556
574
} ,
557
575
"consult the function's documentation for information on how to avoid undefined \
558
576
behavior",
559
577
) ,
560
578
UseOfInlineAssembly => (
561
- Cow :: Borrowed ( "use of inline assembly" ) ,
579
+ Cow :: Borrowed ( self . simple_description ( ) ) ,
562
580
"inline assembly is entirely unchecked and can cause undefined behavior" ,
563
581
) ,
564
582
InitializingTypeWith => (
565
- Cow :: Borrowed ( "initializing type with `rustc_layout_scalar_valid_range` attr" ) ,
583
+ Cow :: Borrowed ( self . simple_description ( ) ) ,
566
584
"initializing a layout restricted type's field with a value outside the valid \
567
585
range is undefined behavior",
568
586
) ,
569
587
UseOfMutableStatic => (
570
- Cow :: Borrowed ( "use of mutable static" ) ,
588
+ Cow :: Borrowed ( self . simple_description ( ) ) ,
571
589
"mutable statics can be mutated by multiple threads: aliasing violations or data \
572
590
races will cause undefined behavior",
573
591
) ,
574
592
UseOfExternStatic => (
575
- Cow :: Borrowed ( "use of extern static" ) ,
593
+ Cow :: Borrowed ( self . simple_description ( ) ) ,
576
594
"extern statics are not controlled by the Rust type system: invalid data, \
577
595
aliasing violations or data races will cause undefined behavior",
578
596
) ,
579
597
DerefOfRawPointer => (
580
- Cow :: Borrowed ( "dereference of raw pointer" ) ,
598
+ Cow :: Borrowed ( self . simple_description ( ) ) ,
581
599
"raw pointers may be null, dangling or unaligned; they can violate aliasing rules \
582
600
and cause data races: all of these are undefined behavior",
583
601
) ,
584
602
AssignToDroppingUnionField => (
585
- Cow :: Borrowed ( "assignment to union field that might need dropping" ) ,
603
+ Cow :: Borrowed ( self . simple_description ( ) ) ,
586
604
"the previous content of the field will be dropped, which causes undefined \
587
605
behavior if the field was not properly initialized",
588
606
) ,
589
607
AccessToUnionField => (
590
- Cow :: Borrowed ( "access to union field" ) ,
608
+ Cow :: Borrowed ( self . simple_description ( ) ) ,
591
609
"the field may not be properly initialized: using uninitialized data will cause \
592
610
undefined behavior",
593
611
) ,
594
612
MutationOfLayoutConstrainedField => (
595
- Cow :: Borrowed ( "mutation of layout constrained field" ) ,
613
+ Cow :: Borrowed ( self . simple_description ( ) ) ,
596
614
"mutating layout constrained fields cannot statically be checked for valid values" ,
597
615
) ,
598
616
BorrowOfLayoutConstrainedField => (
599
- Cow :: Borrowed ( "borrow of layout constrained field with interior mutability" ) ,
617
+ Cow :: Borrowed ( self . simple_description ( ) ) ,
600
618
"references to fields of layout constrained fields lose the constraints. Coupled \
601
619
with interior mutability, the field can be changed to invalid values",
602
620
) ,
0 commit comments