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 } ;
5
5
use rustc_hir:: hir_id:: HirId ;
6
6
use rustc_hir:: intravisit;
7
7
use rustc_middle:: mir:: visit:: { MutatingUseContext , PlaceContext , Visitor } ;
8
+ use rustc_middle:: mir:: * ;
8
9
use rustc_middle:: ty:: query:: Providers ;
9
10
use rustc_middle:: ty:: { self , TyCtxt } ;
10
- use rustc_middle:: { lint, mir:: * } ;
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 > {
@@ -23,10 +22,7 @@ pub struct UnsafetyChecker<'a, 'tcx> {
23
22
param_env : ty:: ParamEnv < ' tcx > ,
24
23
25
24
/// Used `unsafe` blocks in this function. This is used for the "unused_unsafe" lint.
26
- ///
27
- /// The keys are the used `unsafe` blocks, the UnusedUnsafeKind indicates whether
28
- /// 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 > ,
25
+ used_unsafe_blocks : FxHashSet < HirId > ,
30
26
}
31
27
32
28
impl < ' a , ' tcx > UnsafetyChecker < ' a , ' tcx > {
@@ -130,10 +126,7 @@ impl<'tcx> Visitor<'tcx> for UnsafetyChecker<'_, 'tcx> {
130
126
& AggregateKind :: Closure ( def_id, _) | & AggregateKind :: Generator ( def_id, _, _) => {
131
127
let UnsafetyCheckResult { violations, used_unsafe_blocks, .. } =
132
128
self . tcx . unsafety_check_result ( def_id) ;
133
- self . register_violations (
134
- violations,
135
- used_unsafe_blocks. iter ( ) . map ( |( & h, & d) | ( h, d) ) ,
136
- ) ;
129
+ self . register_violations ( violations, used_unsafe_blocks. iter ( ) . copied ( ) ) ;
137
130
}
138
131
} ,
139
132
_ => { }
@@ -257,22 +250,8 @@ impl<'tcx> UnsafetyChecker<'_, 'tcx> {
257
250
fn register_violations < ' a > (
258
251
& mut self ,
259
252
violations : impl IntoIterator < Item = & ' a UnsafetyViolation > ,
260
- new_used_unsafe_blocks : impl IntoIterator < Item = ( HirId , UsedUnsafeBlockData ) > ,
253
+ new_used_unsafe_blocks : impl IntoIterator < Item = HirId > ,
261
254
) {
262
- use UsedUnsafeBlockData :: { AllAllowedInUnsafeFn , SomeDisallowedInUnsafeFn } ;
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
255
let safety = self . body . source_scopes [ self . source_info . scope ]
277
256
. local_data
278
257
. as_ref ( )
@@ -299,22 +278,14 @@ impl<'tcx> UnsafetyChecker<'_, 'tcx> {
299
278
}
300
279
} ) ,
301
280
Safety :: BuiltinUnsafe => { }
302
- Safety :: ExplicitUnsafe ( hir_id) => violations. into_iter ( ) . for_each ( |violation| {
303
- update_entry (
304
- self ,
305
- hir_id,
306
- match self . tcx . lint_level_at_node ( UNSAFE_OP_IN_UNSAFE_FN , violation. lint_root ) . 0
307
- {
308
- Level :: Allow => AllAllowedInUnsafeFn ( violation. lint_root ) ,
309
- _ => SomeDisallowedInUnsafeFn ,
310
- } ,
311
- )
281
+ Safety :: ExplicitUnsafe ( hir_id) => violations. into_iter ( ) . for_each ( |_violation| {
282
+ self . used_unsafe_blocks . insert ( hir_id) ;
312
283
} ) ,
313
284
} ;
314
285
315
- new_used_unsafe_blocks
316
- . into_iter ( )
317
- . for_each ( | ( hir_id , usage_data ) | update_entry ( self , hir_id , usage_data ) ) ;
286
+ new_used_unsafe_blocks. into_iter ( ) . for_each ( |hir_id| {
287
+ self . used_unsafe_blocks . insert ( hir_id ) ;
288
+ } ) ;
318
289
}
319
290
fn check_mut_borrowing_layout_constrained_field (
320
291
& mut self ,
@@ -411,34 +382,28 @@ enum Context {
411
382
412
383
struct UnusedUnsafeVisitor < ' a , ' tcx > {
413
384
tcx : TyCtxt < ' tcx > ,
414
- used_unsafe_blocks : & ' a FxHashMap < HirId , UsedUnsafeBlockData > ,
385
+ used_unsafe_blocks : & ' a FxHashSet < HirId > ,
415
386
context : Context ,
416
387
unused_unsafes : & ' a mut Vec < ( HirId , UnusedUnsafe ) > ,
417
388
}
418
389
419
390
impl < ' tcx > intravisit:: Visitor < ' tcx > for UnusedUnsafeVisitor < ' _ , ' tcx > {
420
391
fn visit_block ( & mut self , block : & ' tcx hir:: Block < ' tcx > ) {
421
- use UsedUnsafeBlockData :: { AllAllowedInUnsafeFn , SomeDisallowedInUnsafeFn } ;
422
-
423
392
if let hir:: BlockCheckMode :: UnsafeBlock ( hir:: UnsafeSource :: UserProvided ) = block. rules {
424
393
let used = match self . tcx . lint_level_at_node ( UNUSED_UNSAFE , block. hir_id ) {
425
- ( Level :: Allow , _) => Some ( SomeDisallowedInUnsafeFn ) ,
426
- _ => self . used_unsafe_blocks . get ( & block. hir_id ) . copied ( ) ,
394
+ ( Level :: Allow , _) => true ,
395
+ _ => self . used_unsafe_blocks . contains ( & block. hir_id ) ,
427
396
} ;
428
397
let unused_unsafe = match ( self . context , used) {
429
- ( _, None ) => UnusedUnsafe :: Unused ,
430
- ( Context :: Safe , Some ( _) )
431
- | ( Context :: UnsafeFn ( _) , Some ( SomeDisallowedInUnsafeFn ) ) => {
398
+ ( _, false ) => UnusedUnsafe :: Unused ,
399
+ ( Context :: Safe , true ) | ( Context :: UnsafeFn ( _) , true ) => {
432
400
let previous_context = self . context ;
433
401
self . context = Context :: UnsafeBlock ( block. hir_id ) ;
434
402
intravisit:: walk_block ( self , block) ;
435
403
self . context = previous_context;
436
404
return ;
437
405
}
438
- ( Context :: UnsafeFn ( hir_id) , Some ( AllAllowedInUnsafeFn ( lint_root) ) ) => {
439
- UnusedUnsafe :: InUnsafeFn ( hir_id, lint_root)
440
- }
441
- ( Context :: UnsafeBlock ( hir_id) , Some ( _) ) => UnusedUnsafe :: InUnsafeBlock ( hir_id) ,
406
+ ( Context :: UnsafeBlock ( hir_id) , true ) => UnusedUnsafe :: InUnsafeBlock ( hir_id) ,
442
407
} ;
443
408
self . unused_unsafes . push ( ( block. hir_id , unused_unsafe) ) ;
444
409
}
@@ -462,7 +427,7 @@ impl<'tcx> intravisit::Visitor<'tcx> for UnusedUnsafeVisitor<'_, 'tcx> {
462
427
fn check_unused_unsafe (
463
428
tcx : TyCtxt < ' _ > ,
464
429
def_id : LocalDefId ,
465
- used_unsafe_blocks : & FxHashMap < HirId , UsedUnsafeBlockData > ,
430
+ used_unsafe_blocks : & FxHashSet < HirId > ,
466
431
) -> Vec < ( HirId , UnusedUnsafe ) > {
467
432
let body_id = tcx. hir ( ) . maybe_body_owned_by ( def_id) ;
468
433
@@ -535,25 +500,6 @@ fn report_unused_unsafe(tcx: TyCtxt<'_>, kind: UnusedUnsafe, id: HirId) {
535
500
"because it's nested under this `unsafe` block" ,
536
501
) ;
537
502
}
538
- UnusedUnsafe :: InUnsafeFn ( id, usage_lint_root) => {
539
- db. span_label (
540
- tcx. sess . source_map ( ) . guess_head_span ( tcx. hir ( ) . span ( id) ) ,
541
- "because it's nested under this `unsafe` fn" ,
542
- )
543
- . note (
544
- "this `unsafe` block does contain unsafe operations, \
545
- but those are already allowed in an `unsafe fn`",
546
- ) ;
547
- let ( level, source) =
548
- tcx. lint_level_at_node ( UNSAFE_OP_IN_UNSAFE_FN , usage_lint_root) ;
549
- assert_eq ! ( level, Level :: Allow ) ;
550
- lint:: explain_lint_level_source (
551
- UNSAFE_OP_IN_UNSAFE_FN ,
552
- Level :: Allow ,
553
- source,
554
- & mut db,
555
- ) ;
556
- }
557
503
}
558
504
559
505
db. emit ( ) ;
0 commit comments