@@ -29,11 +29,7 @@ struct UnsafetyVisitor<'a, 'tcx> {
2929}
3030
3131impl < ' 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 ) ) {
3733 if let (
3834 SafetyContext :: UnsafeBlock { span : enclosing_span, .. } ,
3935 SafetyContext :: UnsafeBlock { span : block_span, hir_id, .. } ,
@@ -63,14 +59,14 @@ impl<'tcx> UnsafetyVisitor<'_, 'tcx> {
6359 ) ;
6460 }
6561 self . safety_context = prev_context;
66- return ;
6762 }
6863 }
6964
7065 fn requires_unsafe ( & mut self , span : Span , kind : UnsafeOpKind ) {
7166 let ( description, note) = kind. description_and_note ( ) ;
7267 let unsafe_op_in_unsafe_fn_allowed = self . unsafe_op_in_unsafe_fn_allowed ( ) ;
7368 match self . safety_context {
69+ SafetyContext :: BuiltinUnsafeBlock => { }
7470 SafetyContext :: UnsafeBlock { ref mut used, .. } => {
7571 if !self . body_unsafety . is_unsafe ( ) || !unsafe_op_in_unsafe_fn_allowed {
7672 // Mark this block as useful
@@ -142,13 +138,23 @@ impl<'a, 'tcx> Visitor<'a, 'tcx> for UnsafetyVisitor<'a, 'tcx> {
142138 }
143139
144140 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+ }
152158 }
153159 }
154160
@@ -250,6 +256,7 @@ impl<'a, 'tcx> Visitor<'a, 'tcx> for UnsafetyVisitor<'a, 'tcx> {
250256#[ derive( Clone , Copy ) ]
251257enum SafetyContext {
252258 Safe ,
259+ BuiltinUnsafeBlock ,
253260 UnsafeFn ,
254261 UnsafeBlock { span : Span , hir_id : hir:: HirId , used : bool } ,
255262}
0 commit comments