@@ -29,11 +29,7 @@ struct UnsafetyVisitor<'a, 'tcx> {
29
29
}
30
30
31
31
impl < ' tcx > UnsafetyVisitor < ' _ , ' tcx > {
32
- fn in_safety_context < R > (
33
- & mut self ,
34
- safety_context : SafetyContext ,
35
- f : impl FnOnce ( & mut Self ) -> R ,
36
- ) {
32
+ fn in_safety_context ( & mut self , safety_context : SafetyContext , f : impl FnOnce ( & mut Self ) ) {
37
33
if let (
38
34
SafetyContext :: UnsafeBlock { span : enclosing_span, .. } ,
39
35
SafetyContext :: UnsafeBlock { span : block_span, hir_id, .. } ,
@@ -63,14 +59,14 @@ impl<'tcx> UnsafetyVisitor<'_, 'tcx> {
63
59
) ;
64
60
}
65
61
self . safety_context = prev_context;
66
- return ;
67
62
}
68
63
}
69
64
70
65
fn requires_unsafe ( & mut self , span : Span , kind : UnsafeOpKind ) {
71
66
let ( description, note) = kind. description_and_note ( ) ;
72
67
let unsafe_op_in_unsafe_fn_allowed = self . unsafe_op_in_unsafe_fn_allowed ( ) ;
73
68
match self . safety_context {
69
+ SafetyContext :: BuiltinUnsafeBlock => { }
74
70
SafetyContext :: UnsafeBlock { ref mut used, .. } => {
75
71
if !self . body_unsafety . is_unsafe ( ) || !unsafe_op_in_unsafe_fn_allowed {
76
72
// Mark this block as useful
@@ -142,13 +138,23 @@ impl<'a, 'tcx> Visitor<'a, 'tcx> for UnsafetyVisitor<'a, 'tcx> {
142
138
}
143
139
144
140
fn visit_block ( & mut self , block : & Block ) {
145
- if let BlockSafety :: ExplicitUnsafe ( hir_id) = block. safety_mode {
146
- self . in_safety_context (
147
- SafetyContext :: UnsafeBlock { span : block. span , hir_id, used : false } ,
148
- |this| visit:: walk_block ( this, block) ,
149
- ) ;
150
- } else {
151
- visit:: walk_block ( self , block) ;
141
+ match block. safety_mode {
142
+ // compiler-generated unsafe code should not count towards the usefulness of
143
+ // an outer unsafe block
144
+ BlockSafety :: BuiltinUnsafe => {
145
+ self . in_safety_context ( SafetyContext :: BuiltinUnsafeBlock , |this| {
146
+ visit:: walk_block ( this, block)
147
+ } ) ;
148
+ }
149
+ BlockSafety :: ExplicitUnsafe ( hir_id) => {
150
+ self . in_safety_context (
151
+ SafetyContext :: UnsafeBlock { span : block. span , hir_id, used : false } ,
152
+ |this| visit:: walk_block ( this, block) ,
153
+ ) ;
154
+ }
155
+ BlockSafety :: Safe => {
156
+ visit:: walk_block ( self , block) ;
157
+ }
152
158
}
153
159
}
154
160
@@ -250,6 +256,7 @@ impl<'a, 'tcx> Visitor<'a, 'tcx> for UnsafetyVisitor<'a, 'tcx> {
250
256
#[ derive( Clone , Copy ) ]
251
257
enum SafetyContext {
252
258
Safe ,
259
+ BuiltinUnsafeBlock ,
253
260
UnsafeFn ,
254
261
UnsafeBlock { span : Span , hir_id : hir:: HirId , used : bool } ,
255
262
}
0 commit comments