@@ -5,9 +5,9 @@ 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
@@ -259,7 +259,7 @@ impl<'tcx> UnsafetyChecker<'_, 'tcx> {
259
259
violations : impl IntoIterator < Item = & ' a UnsafetyViolation > ,
260
260
new_used_unsafe_blocks : impl IntoIterator < Item = ( HirId , UsedUnsafeBlockData ) > ,
261
261
) {
262
- use UsedUnsafeBlockData :: { AllAllowedInUnsafeFn , SomeDisallowedInUnsafeFn } ;
262
+ use UsedUnsafeBlockData :: * ;
263
263
264
264
let update_entry = |this : & mut Self , hir_id, new_usage| {
265
265
match this. used_unsafe_blocks . entry ( hir_id) {
@@ -299,15 +299,11 @@ impl<'tcx> UnsafetyChecker<'_, 'tcx> {
299
299
}
300
300
} ) ,
301
301
Safety :: BuiltinUnsafe => { }
302
- Safety :: ExplicitUnsafe ( hir_id) => violations. into_iter ( ) . for_each ( |violation | {
302
+ Safety :: ExplicitUnsafe ( hir_id) => violations. into_iter ( ) . for_each ( |_violation | {
303
303
update_entry (
304
304
self ,
305
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
- } ,
306
+ SomeDisallowedInUnsafeFn ,
311
307
)
312
308
} ) ,
313
309
} ;
@@ -522,6 +518,11 @@ fn unsafety_check_result<'tcx>(
522
518
}
523
519
524
520
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
+ }
525
526
let span = tcx. sess . source_map ( ) . guess_head_span ( tcx. hir ( ) . span ( id) ) ;
526
527
tcx. struct_span_lint_hir ( UNUSED_UNSAFE , id, span, |lint| {
527
528
let msg = "unnecessary `unsafe` block" ;
@@ -535,25 +536,7 @@ fn report_unused_unsafe(tcx: TyCtxt<'_>, kind: UnusedUnsafe, id: HirId) {
535
536
"because it's nested under this `unsafe` block" ,
536
537
) ;
537
538
}
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
- }
539
+ UnusedUnsafe :: InUnsafeFn ( _id, _usage_lint_root) => unreachable ! ( ) ,
557
540
}
558
541
559
542
db. emit ( ) ;
0 commit comments