11use hir:: InFile ;
2+ use syntax:: ast;
23
3- use crate :: { Diagnostic , DiagnosticCode , DiagnosticsContext , Severity } ;
4+ use crate :: { adjusted_display_range , Diagnostic , DiagnosticCode , DiagnosticsContext , Severity } ;
45
56// Diagnostic: trait-impl-incorrect-safety
67//
@@ -9,15 +10,28 @@ pub(crate) fn trait_impl_incorrect_safety(
910 ctx : & DiagnosticsContext < ' _ > ,
1011 d : & hir:: TraitImplIncorrectSafety ,
1112) -> Diagnostic {
12- Diagnostic :: new_with_syntax_node_ptr (
13- ctx,
13+ Diagnostic :: new (
1414 DiagnosticCode :: Ra ( "trait-impl-incorrect-safety" , Severity :: Error ) ,
1515 if d. should_be_safe {
1616 "unsafe impl for safe trait"
1717 } else {
1818 "impl for unsafe trait needs to be unsafe"
1919 } ,
20- InFile :: new ( d. file_id , d. impl_ . clone ( ) . into ( ) ) ,
20+ adjusted_display_range :: < ast:: Impl > (
21+ ctx,
22+ InFile { file_id : d. file_id , value : d. impl_ . syntax_node_ptr ( ) } ,
23+ & |impl_| {
24+ if d. should_be_safe {
25+ Some ( match ( impl_. unsafe_token ( ) , impl_. impl_token ( ) ) {
26+ ( None , None ) => return None ,
27+ ( None , Some ( t) ) | ( Some ( t) , None ) => t. text_range ( ) ,
28+ ( Some ( t1) , Some ( t2) ) => t1. text_range ( ) . cover ( t2. text_range ( ) ) ,
29+ } )
30+ } else {
31+ impl_. impl_token ( ) . map ( |t| t. text_range ( ) )
32+ }
33+ } ,
34+ ) ,
2135 )
2236}
2337
@@ -35,10 +49,10 @@ unsafe trait Unsafe {}
3549 impl Safe for () {}
3650
3751 impl Unsafe for () {}
38- //^^^^^^^^^^^^^^^^^^^^^ error: impl for unsafe trait needs to be unsafe
52+ //^^^^ error: impl for unsafe trait needs to be unsafe
3953
4054 unsafe impl Safe for () {}
41- //^^^^^^^^^^^^^^^^^^^^^^^^^^ error: unsafe impl for safe trait
55+ //^^^^^^^^^^^ error: unsafe impl for safe trait
4256
4357 unsafe impl Unsafe for () {}
4458"# ,
@@ -57,20 +71,20 @@ struct L<'l>;
5771 impl<T> Drop for S<T> {}
5872
5973 impl<#[may_dangle] T> Drop for S<T> {}
60- //^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ error: impl for unsafe trait needs to be unsafe
74+ //^^^^ error: impl for unsafe trait needs to be unsafe
6175
6276 unsafe impl<T> Drop for S<T> {}
63- //^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ error: unsafe impl for safe trait
77+ //^^^^^^^^^^^ error: unsafe impl for safe trait
6478
6579 unsafe impl<#[may_dangle] T> Drop for S<T> {}
6680
6781 impl<'l> Drop for L<'l> {}
6882
6983 impl<#[may_dangle] 'l> Drop for L<'l> {}
70- //^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ error: impl for unsafe trait needs to be unsafe
84+ //^^^^ error: impl for unsafe trait needs to be unsafe
7185
7286 unsafe impl<'l> Drop for L<'l> {}
73- //^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ error: unsafe impl for safe trait
87+ //^^^^^^^^^^^ error: unsafe impl for safe trait
7488
7589 unsafe impl<#[may_dangle] 'l> Drop for L<'l> {}
7690"# ,
@@ -86,14 +100,14 @@ trait Trait {}
86100 impl !Trait for () {}
87101
88102 unsafe impl !Trait for () {}
89- //^^^^^^^^^^^^^^^^^^^^^^^^^^^^ error: unsafe impl for safe trait
103+ //^^^^^^^^^^^ error: unsafe impl for safe trait
90104
91105unsafe trait UnsafeTrait {}
92106
93107 impl !UnsafeTrait for () {}
94108
95109 unsafe impl !UnsafeTrait for () {}
96- //^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ error: unsafe impl for safe trait
110+ //^^^^^^^^^^^ error: unsafe impl for safe trait
97111
98112"# ,
99113 ) ;
@@ -108,7 +122,7 @@ struct S;
108122 impl S {}
109123
110124 unsafe impl S {}
111- //^^^^^^^^^^^^^^^^ error: unsafe impl for safe trait
125+ //^^^^^^^^^^^ error: unsafe impl for safe trait
112126"# ,
113127 ) ;
114128 }
0 commit comments