1
- use rustc_data_structures:: fx:: FxHashMap ;
1
+ use rustc_data_structures:: fx:: FxHashSet ;
2
2
use rustc_errors:: struct_span_err;
3
3
use rustc_hir as hir;
4
4
use rustc_hir:: def_id:: { DefId , LocalDefId } ;
@@ -11,7 +11,6 @@ use rustc_middle::ty::{self, TyCtxt};
11
11
use rustc_session:: lint:: builtin:: { UNSAFE_OP_IN_UNSAFE_FN , UNUSED_UNSAFE } ;
12
12
use rustc_session:: lint:: Level ;
13
13
14
- use std:: collections:: hash_map;
15
14
use std:: ops:: Bound ;
16
15
17
16
pub struct UnsafetyChecker < ' a , ' tcx > {
@@ -26,7 +25,7 @@ pub struct UnsafetyChecker<'a, 'tcx> {
26
25
///
27
26
/// The keys are the used `unsafe` blocks, the UnusedUnsafeKind indicates whether
28
27
/// or not any of the usages happen at a place that doesn't allow `unsafe_op_in_unsafe_fn`.
29
- used_unsafe_blocks : FxHashMap < HirId , UsedUnsafeBlockData > ,
28
+ used_unsafe_blocks : FxHashSet < HirId > ,
30
29
}
31
30
32
31
impl < ' a , ' tcx > UnsafetyChecker < ' a , ' tcx > {
@@ -130,10 +129,7 @@ impl<'tcx> Visitor<'tcx> for UnsafetyChecker<'_, 'tcx> {
130
129
& AggregateKind :: Closure ( def_id, _) | & AggregateKind :: Generator ( def_id, _, _) => {
131
130
let UnsafetyCheckResult { violations, used_unsafe_blocks, .. } =
132
131
self . tcx . unsafety_check_result ( def_id) ;
133
- self . register_violations (
134
- violations,
135
- used_unsafe_blocks. iter ( ) . map ( |( & h, & d) | ( h, d) ) ,
136
- ) ;
132
+ self . register_violations ( violations, used_unsafe_blocks. iter ( ) . copied ( ) ) ;
137
133
}
138
134
} ,
139
135
_ => { }
@@ -257,22 +253,8 @@ impl<'tcx> UnsafetyChecker<'_, 'tcx> {
257
253
fn register_violations < ' a > (
258
254
& mut self ,
259
255
violations : impl IntoIterator < Item = & ' a UnsafetyViolation > ,
260
- new_used_unsafe_blocks : impl IntoIterator < Item = ( HirId , UsedUnsafeBlockData ) > ,
256
+ new_used_unsafe_blocks : impl IntoIterator < Item = HirId > ,
261
257
) {
262
- use UsedUnsafeBlockData :: * ;
263
-
264
- let update_entry = |this : & mut Self , hir_id, new_usage| {
265
- match this. used_unsafe_blocks . entry ( hir_id) {
266
- hash_map:: Entry :: Occupied ( mut entry) => {
267
- if new_usage == SomeDisallowedInUnsafeFn {
268
- * entry. get_mut ( ) = SomeDisallowedInUnsafeFn ;
269
- }
270
- }
271
- hash_map:: Entry :: Vacant ( entry) => {
272
- entry. insert ( new_usage) ;
273
- }
274
- } ;
275
- } ;
276
258
let safety = self . body . source_scopes [ self . source_info . scope ]
277
259
. local_data
278
260
. as_ref ( )
@@ -300,17 +282,13 @@ impl<'tcx> UnsafetyChecker<'_, 'tcx> {
300
282
} ) ,
301
283
Safety :: BuiltinUnsafe => { }
302
284
Safety :: ExplicitUnsafe ( hir_id) => violations. into_iter ( ) . for_each ( |_violation| {
303
- update_entry (
304
- self ,
305
- hir_id,
306
- SomeDisallowedInUnsafeFn ,
307
- )
285
+ self . used_unsafe_blocks . insert ( hir_id) ;
308
286
} ) ,
309
287
} ;
310
288
311
- new_used_unsafe_blocks
312
- . into_iter ( )
313
- . for_each ( | ( hir_id , usage_data ) | update_entry ( self , hir_id , usage_data ) ) ;
289
+ new_used_unsafe_blocks. into_iter ( ) . for_each ( |hir_id| {
290
+ self . used_unsafe_blocks . insert ( hir_id ) ;
291
+ } ) ;
314
292
}
315
293
fn check_mut_borrowing_layout_constrained_field (
316
294
& mut self ,
@@ -407,34 +385,28 @@ enum Context {
407
385
408
386
struct UnusedUnsafeVisitor < ' a , ' tcx > {
409
387
tcx : TyCtxt < ' tcx > ,
410
- used_unsafe_blocks : & ' a FxHashMap < HirId , UsedUnsafeBlockData > ,
388
+ used_unsafe_blocks : & ' a FxHashSet < HirId > ,
411
389
context : Context ,
412
390
unused_unsafes : & ' a mut Vec < ( HirId , UnusedUnsafe ) > ,
413
391
}
414
392
415
393
impl < ' tcx > intravisit:: Visitor < ' tcx > for UnusedUnsafeVisitor < ' _ , ' tcx > {
416
394
fn visit_block ( & mut self , block : & ' tcx hir:: Block < ' tcx > ) {
417
- use UsedUnsafeBlockData :: { AllAllowedInUnsafeFn , SomeDisallowedInUnsafeFn } ;
418
-
419
395
if let hir:: BlockCheckMode :: UnsafeBlock ( hir:: UnsafeSource :: UserProvided ) = block. rules {
420
396
let used = match self . tcx . lint_level_at_node ( UNUSED_UNSAFE , block. hir_id ) {
421
- ( Level :: Allow , _) => Some ( SomeDisallowedInUnsafeFn ) ,
422
- _ => self . used_unsafe_blocks . get ( & block. hir_id ) . copied ( ) ,
397
+ ( Level :: Allow , _) => true ,
398
+ _ => self . used_unsafe_blocks . contains ( & block. hir_id ) ,
423
399
} ;
424
400
let unused_unsafe = match ( self . context , used) {
425
- ( _, None ) => UnusedUnsafe :: Unused ,
426
- ( Context :: Safe , Some ( _) )
427
- | ( Context :: UnsafeFn ( _) , Some ( SomeDisallowedInUnsafeFn ) ) => {
401
+ ( _, false ) => UnusedUnsafe :: Unused ,
402
+ ( Context :: Safe , true ) | ( Context :: UnsafeFn ( _) , true ) => {
428
403
let previous_context = self . context ;
429
404
self . context = Context :: UnsafeBlock ( block. hir_id ) ;
430
405
intravisit:: walk_block ( self , block) ;
431
406
self . context = previous_context;
432
407
return ;
433
408
}
434
- ( Context :: UnsafeFn ( hir_id) , Some ( AllAllowedInUnsafeFn ( lint_root) ) ) => {
435
- UnusedUnsafe :: InUnsafeFn ( hir_id, lint_root)
436
- }
437
- ( Context :: UnsafeBlock ( hir_id) , Some ( _) ) => UnusedUnsafe :: InUnsafeBlock ( hir_id) ,
409
+ ( Context :: UnsafeBlock ( hir_id) , true ) => UnusedUnsafe :: InUnsafeBlock ( hir_id) ,
438
410
} ;
439
411
self . unused_unsafes . push ( ( block. hir_id , unused_unsafe) ) ;
440
412
}
@@ -458,7 +430,7 @@ impl<'tcx> intravisit::Visitor<'tcx> for UnusedUnsafeVisitor<'_, 'tcx> {
458
430
fn check_unused_unsafe (
459
431
tcx : TyCtxt < ' _ > ,
460
432
def_id : LocalDefId ,
461
- used_unsafe_blocks : & FxHashMap < HirId , UsedUnsafeBlockData > ,
433
+ used_unsafe_blocks : & FxHashSet < HirId > ,
462
434
) -> Vec < ( HirId , UnusedUnsafe ) > {
463
435
let body_id = tcx. hir ( ) . maybe_body_owned_by ( def_id) ;
464
436
@@ -518,11 +490,6 @@ fn unsafety_check_result<'tcx>(
518
490
}
519
491
520
492
fn report_unused_unsafe ( tcx : TyCtxt < ' _ > , kind : UnusedUnsafe , id : HirId ) {
521
- if matches ! ( kind, UnusedUnsafe :: InUnsafeFn ( ..) ) {
522
- // We do *not* warn here, these unsafe blocks are actually required when
523
- // `unsafe_op_in_unsafe_fn` is warn or higher.
524
- return ;
525
- }
526
493
let span = tcx. sess . source_map ( ) . guess_head_span ( tcx. hir ( ) . span ( id) ) ;
527
494
tcx. struct_span_lint_hir ( UNUSED_UNSAFE , id, span, |lint| {
528
495
let msg = "unnecessary `unsafe` block" ;
@@ -536,7 +503,6 @@ fn report_unused_unsafe(tcx: TyCtxt<'_>, kind: UnusedUnsafe, id: HirId) {
536
503
"because it's nested under this `unsafe` block" ,
537
504
) ;
538
505
}
539
- UnusedUnsafe :: InUnsafeFn ( _id, _usage_lint_root) => unreachable ! ( ) ,
540
506
}
541
507
542
508
db. emit ( ) ;
0 commit comments