1- use rustc_data_structures:: fx:: FxHashMap ;
1+ use rustc_data_structures:: fx:: FxHashSet ;
22use rustc_errors:: struct_span_err;
33use rustc_hir as hir;
44use rustc_hir:: def_id:: { DefId , LocalDefId } ;
55use rustc_hir:: hir_id:: HirId ;
66use rustc_hir:: intravisit;
77use rustc_middle:: mir:: visit:: { MutatingUseContext , PlaceContext , Visitor } ;
8+ use rustc_middle:: mir:: * ;
89use rustc_middle:: ty:: query:: Providers ;
910use rustc_middle:: ty:: { self , TyCtxt } ;
10- use rustc_middle:: { lint, mir:: * } ;
1111use rustc_session:: lint:: builtin:: { UNSAFE_OP_IN_UNSAFE_FN , UNUSED_UNSAFE } ;
1212use rustc_session:: lint:: Level ;
1313
14- use std:: collections:: hash_map;
1514use std:: ops:: Bound ;
1615
1716pub struct UnsafetyChecker < ' a , ' tcx > {
@@ -23,10 +22,7 @@ pub struct UnsafetyChecker<'a, 'tcx> {
2322 param_env : ty:: ParamEnv < ' tcx > ,
2423
2524 /// 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 > ,
3026}
3127
3228impl < ' a , ' tcx > UnsafetyChecker < ' a , ' tcx > {
@@ -130,10 +126,7 @@ impl<'tcx> Visitor<'tcx> for UnsafetyChecker<'_, 'tcx> {
130126 & AggregateKind :: Closure ( def_id, _) | & AggregateKind :: Generator ( def_id, _, _) => {
131127 let UnsafetyCheckResult { violations, used_unsafe_blocks, .. } =
132128 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 ( ) ) ;
137130 }
138131 } ,
139132 _ => { }
@@ -257,22 +250,8 @@ impl<'tcx> UnsafetyChecker<'_, 'tcx> {
257250 fn register_violations < ' a > (
258251 & mut self ,
259252 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 > ,
261254 ) {
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- } ;
276255 let safety = self . body . source_scopes [ self . source_info . scope ]
277256 . local_data
278257 . as_ref ( )
@@ -299,22 +278,14 @@ impl<'tcx> UnsafetyChecker<'_, 'tcx> {
299278 }
300279 } ) ,
301280 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) ;
312283 } ) ,
313284 } ;
314285
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+ } ) ;
318289 }
319290 fn check_mut_borrowing_layout_constrained_field (
320291 & mut self ,
@@ -411,34 +382,28 @@ enum Context {
411382
412383struct UnusedUnsafeVisitor < ' a , ' tcx > {
413384 tcx : TyCtxt < ' tcx > ,
414- used_unsafe_blocks : & ' a FxHashMap < HirId , UsedUnsafeBlockData > ,
385+ used_unsafe_blocks : & ' a FxHashSet < HirId > ,
415386 context : Context ,
416387 unused_unsafes : & ' a mut Vec < ( HirId , UnusedUnsafe ) > ,
417388}
418389
419390impl < ' tcx > intravisit:: Visitor < ' tcx > for UnusedUnsafeVisitor < ' _ , ' tcx > {
420391 fn visit_block ( & mut self , block : & ' tcx hir:: Block < ' tcx > ) {
421- use UsedUnsafeBlockData :: { AllAllowedInUnsafeFn , SomeDisallowedInUnsafeFn } ;
422-
423392 if let hir:: BlockCheckMode :: UnsafeBlock ( hir:: UnsafeSource :: UserProvided ) = block. rules {
424393 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 ) ,
427396 } ;
428397 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 ) => {
432400 let previous_context = self . context ;
433401 self . context = Context :: UnsafeBlock ( block. hir_id ) ;
434402 intravisit:: walk_block ( self , block) ;
435403 self . context = previous_context;
436404 return ;
437405 }
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) ,
442407 } ;
443408 self . unused_unsafes . push ( ( block. hir_id , unused_unsafe) ) ;
444409 }
@@ -462,7 +427,7 @@ impl<'tcx> intravisit::Visitor<'tcx> for UnusedUnsafeVisitor<'_, 'tcx> {
462427fn check_unused_unsafe (
463428 tcx : TyCtxt < ' _ > ,
464429 def_id : LocalDefId ,
465- used_unsafe_blocks : & FxHashMap < HirId , UsedUnsafeBlockData > ,
430+ used_unsafe_blocks : & FxHashSet < HirId > ,
466431) -> Vec < ( HirId , UnusedUnsafe ) > {
467432 let body_id = tcx. hir ( ) . maybe_body_owned_by ( def_id) ;
468433
@@ -535,25 +500,6 @@ fn report_unused_unsafe(tcx: TyCtxt<'_>, kind: UnusedUnsafe, id: HirId) {
535500 "because it's nested under this `unsafe` block" ,
536501 ) ;
537502 }
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- }
557503 }
558504
559505 db. emit ( ) ;
0 commit comments