From ee3fc9dff8f2cff217883cd4a6f979677a1263f9 Mon Sep 17 00:00:00 2001 From: Ralf Jung Date: Tue, 2 Aug 2022 15:47:14 -0400 Subject: [PATCH 1/4] never consider unsafe blocks unused if they would be required with unsafe_op_in_unsafe_fn --- .../rustc_errors/src/diagnostic_builder.rs | 2 +- .../rustc_mir_build/src/check_unsafety.rs | 8 +- .../rustc_mir_transform/src/check_unsafety.rs | 37 +- src/test/ui/span/lint-unused-unsafe-thir.rs | 4 +- .../ui/span/lint-unused-unsafe-thir.stderr | 18 +- .../ui/span/lint-unused-unsafe.mir.stderr | 548 ++---------------- src/test/ui/span/lint-unused-unsafe.rs | 74 +-- ...rfc-2585-unsafe_op_in_unsafe_fn.mir.stderr | 38 +- .../unsafe/rfc-2585-unsafe_op_in_unsafe_fn.rs | 2 - ...fc-2585-unsafe_op_in_unsafe_fn.thir.stderr | 24 +- 10 files changed, 124 insertions(+), 631 deletions(-) diff --git a/compiler/rustc_errors/src/diagnostic_builder.rs b/compiler/rustc_errors/src/diagnostic_builder.rs index 9e68ee282e652..04159bff4ff41 100644 --- a/compiler/rustc_errors/src/diagnostic_builder.rs +++ b/compiler/rustc_errors/src/diagnostic_builder.rs @@ -566,7 +566,7 @@ impl Drop for DiagnosticBuilderInner<'_> { ), )); handler.emit_diagnostic(&mut self.diagnostic); - panic!(); + panic!("error was constructed but not emitted"); } } // `.emit()` was previously called, or maybe we're during `.cancel()`. diff --git a/compiler/rustc_mir_build/src/check_unsafety.rs b/compiler/rustc_mir_build/src/check_unsafety.rs index 864caf0ba3197..e3157a4300195 100644 --- a/compiler/rustc_mir_build/src/check_unsafety.rs +++ b/compiler/rustc_mir_build/src/check_unsafety.rs @@ -75,10 +75,10 @@ impl<'tcx> UnsafetyVisitor<'_, 'tcx> { match self.safety_context { SafetyContext::BuiltinUnsafeBlock => {} SafetyContext::UnsafeBlock { ref mut used, .. } => { - if !self.body_unsafety.is_unsafe() || !unsafe_op_in_unsafe_fn_allowed { - // Mark this block as useful - *used = true; - } + // Mark this block as useful (even inside `unsafe fn`, where it is technically + // redundant -- but we want to eventually enable `unsafe_op_in_unsafe_fn` by + // default which will require those blocks). + *used = true; } SafetyContext::UnsafeFn if unsafe_op_in_unsafe_fn_allowed => {} SafetyContext::UnsafeFn => { diff --git a/compiler/rustc_mir_transform/src/check_unsafety.rs b/compiler/rustc_mir_transform/src/check_unsafety.rs index d564f48016626..9ea87245d034a 100644 --- a/compiler/rustc_mir_transform/src/check_unsafety.rs +++ b/compiler/rustc_mir_transform/src/check_unsafety.rs @@ -5,9 +5,9 @@ use rustc_hir::def_id::{DefId, LocalDefId}; use rustc_hir::hir_id::HirId; use rustc_hir::intravisit; use rustc_middle::mir::visit::{MutatingUseContext, PlaceContext, Visitor}; +use rustc_middle::mir::*; use rustc_middle::ty::query::Providers; use rustc_middle::ty::{self, TyCtxt}; -use rustc_middle::{lint, mir::*}; use rustc_session::lint::builtin::{UNSAFE_OP_IN_UNSAFE_FN, UNUSED_UNSAFE}; use rustc_session::lint::Level; @@ -259,7 +259,7 @@ impl<'tcx> UnsafetyChecker<'_, 'tcx> { violations: impl IntoIterator, new_used_unsafe_blocks: impl IntoIterator, ) { - use UsedUnsafeBlockData::{AllAllowedInUnsafeFn, SomeDisallowedInUnsafeFn}; + use UsedUnsafeBlockData::*; let update_entry = |this: &mut Self, hir_id, new_usage| { match this.used_unsafe_blocks.entry(hir_id) { @@ -299,15 +299,11 @@ impl<'tcx> UnsafetyChecker<'_, 'tcx> { } }), Safety::BuiltinUnsafe => {} - Safety::ExplicitUnsafe(hir_id) => violations.into_iter().for_each(|violation| { + Safety::ExplicitUnsafe(hir_id) => violations.into_iter().for_each(|_violation| { update_entry( self, hir_id, - match self.tcx.lint_level_at_node(UNSAFE_OP_IN_UNSAFE_FN, violation.lint_root).0 - { - Level::Allow => AllAllowedInUnsafeFn(violation.lint_root), - _ => SomeDisallowedInUnsafeFn, - }, + SomeDisallowedInUnsafeFn, ) }), }; @@ -522,6 +518,11 @@ fn unsafety_check_result<'tcx>( } fn report_unused_unsafe(tcx: TyCtxt<'_>, kind: UnusedUnsafe, id: HirId) { + if matches!(kind, UnusedUnsafe::InUnsafeFn(..)) { + // We do *not* warn here, these unsafe blocks are actually required when + // `unsafe_op_in_unsafe_fn` is warn or higher. + return; + } let span = tcx.sess.source_map().guess_head_span(tcx.hir().span(id)); tcx.struct_span_lint_hir(UNUSED_UNSAFE, id, span, |lint| { let msg = "unnecessary `unsafe` block"; @@ -535,25 +536,7 @@ fn report_unused_unsafe(tcx: TyCtxt<'_>, kind: UnusedUnsafe, id: HirId) { "because it's nested under this `unsafe` block", ); } - UnusedUnsafe::InUnsafeFn(id, usage_lint_root) => { - db.span_label( - tcx.sess.source_map().guess_head_span(tcx.hir().span(id)), - "because it's nested under this `unsafe` fn", - ) - .note( - "this `unsafe` block does contain unsafe operations, \ - but those are already allowed in an `unsafe fn`", - ); - let (level, source) = - tcx.lint_level_at_node(UNSAFE_OP_IN_UNSAFE_FN, usage_lint_root); - assert_eq!(level, Level::Allow); - lint::explain_lint_level_source( - UNSAFE_OP_IN_UNSAFE_FN, - Level::Allow, - source, - &mut db, - ); - } + UnusedUnsafe::InUnsafeFn(_id, _usage_lint_root) => unreachable!(), } db.emit(); diff --git a/src/test/ui/span/lint-unused-unsafe-thir.rs b/src/test/ui/span/lint-unused-unsafe-thir.rs index 95a537ed28230..adb72c26bba47 100644 --- a/src/test/ui/span/lint-unused-unsafe-thir.rs +++ b/src/test/ui/span/lint-unused-unsafe-thir.rs @@ -22,7 +22,7 @@ fn bad1() { unsafe {} } //~ ERROR: unnecessary `unsafe` block fn bad2() { unsafe { bad1() } } //~ ERROR: unnecessary `unsafe` block unsafe fn bad3() { unsafe {} } //~ ERROR: unnecessary `unsafe` block fn bad4() { unsafe { callback(||{}) } } //~ ERROR: unnecessary `unsafe` block -unsafe fn bad5() { unsafe { unsf() } } //~ ERROR: unnecessary `unsafe` block +unsafe fn bad5() { unsafe { unsf() } } fn bad6() { unsafe { // don't put the warning here unsafe { //~ ERROR: unnecessary `unsafe` block @@ -31,7 +31,7 @@ fn bad6() { } } unsafe fn bad7() { - unsafe { //~ ERROR: unnecessary `unsafe` block + unsafe { unsafe { //~ ERROR: unnecessary `unsafe` block unsf() } diff --git a/src/test/ui/span/lint-unused-unsafe-thir.stderr b/src/test/ui/span/lint-unused-unsafe-thir.stderr index 6654910c5cdc3..3bcbb759775aa 100644 --- a/src/test/ui/span/lint-unused-unsafe-thir.stderr +++ b/src/test/ui/span/lint-unused-unsafe-thir.stderr @@ -30,14 +30,6 @@ error: unnecessary `unsafe` block LL | fn bad4() { unsafe { callback(||{}) } } | ^^^^^^ unnecessary `unsafe` block -error: unnecessary `unsafe` block - --> $DIR/lint-unused-unsafe-thir.rs:25:20 - | -LL | unsafe fn bad5() { unsafe { unsf() } } - | ---------------- ^^^^^^ unnecessary `unsafe` block - | | - | because it's nested under this `unsafe` fn - error: unnecessary `unsafe` block --> $DIR/lint-unused-unsafe-thir.rs:28:9 | @@ -54,13 +46,5 @@ LL | unsafe { LL | unsafe { | ^^^^^^ unnecessary `unsafe` block -error: unnecessary `unsafe` block - --> $DIR/lint-unused-unsafe-thir.rs:34:5 - | -LL | unsafe fn bad7() { - | ---------------- because it's nested under this `unsafe` fn -LL | unsafe { - | ^^^^^^ unnecessary `unsafe` block - -error: aborting due to 8 previous errors +error: aborting due to 6 previous errors diff --git a/src/test/ui/span/lint-unused-unsafe.mir.stderr b/src/test/ui/span/lint-unused-unsafe.mir.stderr index 850550a1d8f70..d8412908c7383 100644 --- a/src/test/ui/span/lint-unused-unsafe.mir.stderr +++ b/src/test/ui/span/lint-unused-unsafe.mir.stderr @@ -28,17 +28,6 @@ error: unnecessary `unsafe` block LL | fn bad4() { unsafe { callback(||{}) } } | ^^^^^^ unnecessary `unsafe` block -error: unnecessary `unsafe` block - --> $DIR/lint-unused-unsafe.rs:30:20 - | -LL | unsafe fn bad5() { unsafe { unsf() } } - | ---------------- ^^^^^^ unnecessary `unsafe` block - | | - | because it's nested under this `unsafe` fn - | - = note: this `unsafe` block does contain unsafe operations, but those are already allowed in an `unsafe fn` - = note: `#[allow(unsafe_op_in_unsafe_fn)]` on by default - error: unnecessary `unsafe` block --> $DIR/lint-unused-unsafe.rs:32:5 | @@ -51,17 +40,6 @@ error: unnecessary `unsafe` block LL | unsafe { | ^^^^^^ unnecessary `unsafe` block -error: unnecessary `unsafe` block - --> $DIR/lint-unused-unsafe.rs:40:9 - | -LL | unsafe fn bad7() { - | ---------------- because it's nested under this `unsafe` fn -LL | unsafe { -LL | unsafe { - | ^^^^^^ unnecessary `unsafe` block - | - = note: this `unsafe` block does contain unsafe operations, but those are already allowed in an `unsafe fn` - error: unnecessary `unsafe` block --> $DIR/lint-unused-unsafe.rs:74:9 | @@ -272,91 +250,32 @@ error: unnecessary `unsafe` block LL | unsafe { | ^^^^^^ unnecessary `unsafe` block -error: unnecessary `unsafe` block - --> $DIR/lint-unused-unsafe.rs:197:13 - | -LL | unsafe fn granularity_2() { - | ------------------------- because it's nested under this `unsafe` fn -LL | unsafe { -LL | unsafe { unsf() } - | ^^^^^^ unnecessary `unsafe` block - | - = note: this `unsafe` block does contain unsafe operations, but those are already allowed in an `unsafe fn` -note: the lint level is defined here - --> $DIR/lint-unused-unsafe.rs:194:13 - | -LL | #[allow(unsafe_op_in_unsafe_fn)] - | ^^^^^^^^^^^^^^^^^^^^^^ - -error: unnecessary `unsafe` block - --> $DIR/lint-unused-unsafe.rs:198:13 - | -LL | unsafe fn granularity_2() { - | ------------------------- because it's nested under this `unsafe` fn -... -LL | unsafe { unsf() } - | ^^^^^^ unnecessary `unsafe` block - | - = note: this `unsafe` block does contain unsafe operations, but those are already allowed in an `unsafe fn` - -error: unnecessary `unsafe` block - --> $DIR/lint-unused-unsafe.rs:199:13 - | -LL | unsafe fn granularity_2() { - | ------------------------- because it's nested under this `unsafe` fn -... -LL | unsafe { unsf() } - | ^^^^^^ unnecessary `unsafe` block - | - = note: this `unsafe` block does contain unsafe operations, but those are already allowed in an `unsafe fn` - -error: unnecessary `unsafe` block - --> $DIR/lint-unused-unsafe.rs:205:9 - | -LL | unsafe fn top_level_used_2() { - | ---------------------------- because it's nested under this `unsafe` fn -LL | unsafe { - | ^^^^^^ unnecessary `unsafe` block - | - = note: this `unsafe` block does contain unsafe operations, but those are already allowed in an `unsafe fn` -note: the lint level is defined here - --> $DIR/lint-unused-unsafe.rs:203:13 - | -LL | #[allow(unsafe_op_in_unsafe_fn)] - | ^^^^^^^^^^^^^^^^^^^^^^ - error: unnecessary `unsafe` block --> $DIR/lint-unused-unsafe.rs:207:13 | -LL | unsafe fn top_level_used_2() { - | ---------------------------- because it's nested under this `unsafe` fn -... +LL | unsafe { + | ------ because it's nested under this `unsafe` block +LL | unsf(); LL | unsafe { unsf() } | ^^^^^^ unnecessary `unsafe` block - | - = note: this `unsafe` block does contain unsafe operations, but those are already allowed in an `unsafe fn` error: unnecessary `unsafe` block --> $DIR/lint-unused-unsafe.rs:208:13 | -LL | unsafe fn top_level_used_2() { - | ---------------------------- because it's nested under this `unsafe` fn +LL | unsafe { + | ------ because it's nested under this `unsafe` block ... LL | unsafe { unsf() } | ^^^^^^ unnecessary `unsafe` block - | - = note: this `unsafe` block does contain unsafe operations, but those are already allowed in an `unsafe fn` error: unnecessary `unsafe` block --> $DIR/lint-unused-unsafe.rs:209:13 | -LL | unsafe fn top_level_used_2() { - | ---------------------------- because it's nested under this `unsafe` fn +LL | unsafe { + | ------ because it's nested under this `unsafe` block ... LL | unsafe { unsf() } | ^^^^^^ unnecessary `unsafe` block - | - = note: this `unsafe` block does contain unsafe operations, but those are already allowed in an `unsafe fn` error: unnecessary `unsafe` block --> $DIR/lint-unused-unsafe.rs:220:17 @@ -398,19 +317,12 @@ LL | unsafe { | ^^^^^^ unnecessary `unsafe` block error: unnecessary `unsafe` block - --> $DIR/lint-unused-unsafe.rs:254:9 + --> $DIR/lint-unused-unsafe.rs:255:13 | -LL | unsafe fn granular_disallow_op_in_unsafe_fn_3() { - | ----------------------------------------------- because it's nested under this `unsafe` fn LL | unsafe { - | ^^^^^^ unnecessary `unsafe` block - | - = note: this `unsafe` block does contain unsafe operations, but those are already allowed in an `unsafe fn` -note: the lint level is defined here - --> $DIR/lint-unused-unsafe.rs:252:13 - | -LL | #[allow(unsafe_op_in_unsafe_fn)] - | ^^^^^^^^^^^^^^^^^^^^^^ + | ------ because it's nested under this `unsafe` block +LL | unsafe { + | ^^^^^^ unnecessary `unsafe` block error: unnecessary `unsafe` block --> $DIR/lint-unused-unsafe.rs:268:13 @@ -630,91 +542,32 @@ error: unnecessary `unsafe` block LL | let _ = || unsafe { | ^^^^^^ unnecessary `unsafe` block -error: unnecessary `unsafe` block - --> $DIR/lint-unused-unsafe.rs:409:24 - | -LL | unsafe fn granularity_2() { - | ------------------------- because it's nested under this `unsafe` fn -LL | let _ = || unsafe { -LL | let _ = || unsafe { unsf() }; - | ^^^^^^ unnecessary `unsafe` block - | - = note: this `unsafe` block does contain unsafe operations, but those are already allowed in an `unsafe fn` -note: the lint level is defined here - --> $DIR/lint-unused-unsafe.rs:406:13 - | -LL | #[allow(unsafe_op_in_unsafe_fn)] - | ^^^^^^^^^^^^^^^^^^^^^^ - -error: unnecessary `unsafe` block - --> $DIR/lint-unused-unsafe.rs:410:24 - | -LL | unsafe fn granularity_2() { - | ------------------------- because it's nested under this `unsafe` fn -... -LL | let _ = || unsafe { unsf() }; - | ^^^^^^ unnecessary `unsafe` block - | - = note: this `unsafe` block does contain unsafe operations, but those are already allowed in an `unsafe fn` - -error: unnecessary `unsafe` block - --> $DIR/lint-unused-unsafe.rs:411:24 - | -LL | unsafe fn granularity_2() { - | ------------------------- because it's nested under this `unsafe` fn -... -LL | let _ = || unsafe { unsf() }; - | ^^^^^^ unnecessary `unsafe` block - | - = note: this `unsafe` block does contain unsafe operations, but those are already allowed in an `unsafe fn` - -error: unnecessary `unsafe` block - --> $DIR/lint-unused-unsafe.rs:417:20 - | -LL | unsafe fn top_level_used_2() { - | ---------------------------- because it's nested under this `unsafe` fn -LL | let _ = || unsafe { - | ^^^^^^ unnecessary `unsafe` block - | - = note: this `unsafe` block does contain unsafe operations, but those are already allowed in an `unsafe fn` -note: the lint level is defined here - --> $DIR/lint-unused-unsafe.rs:415:13 - | -LL | #[allow(unsafe_op_in_unsafe_fn)] - | ^^^^^^^^^^^^^^^^^^^^^^ - error: unnecessary `unsafe` block --> $DIR/lint-unused-unsafe.rs:419:24 | -LL | unsafe fn top_level_used_2() { - | ---------------------------- because it's nested under this `unsafe` fn -... +LL | let _ = || unsafe { + | ------ because it's nested under this `unsafe` block +LL | unsf(); LL | let _ = || unsafe { unsf() }; | ^^^^^^ unnecessary `unsafe` block - | - = note: this `unsafe` block does contain unsafe operations, but those are already allowed in an `unsafe fn` error: unnecessary `unsafe` block --> $DIR/lint-unused-unsafe.rs:420:24 | -LL | unsafe fn top_level_used_2() { - | ---------------------------- because it's nested under this `unsafe` fn +LL | let _ = || unsafe { + | ------ because it's nested under this `unsafe` block ... LL | let _ = || unsafe { unsf() }; | ^^^^^^ unnecessary `unsafe` block - | - = note: this `unsafe` block does contain unsafe operations, but those are already allowed in an `unsafe fn` error: unnecessary `unsafe` block --> $DIR/lint-unused-unsafe.rs:421:24 | -LL | unsafe fn top_level_used_2() { - | ---------------------------- because it's nested under this `unsafe` fn +LL | let _ = || unsafe { + | ------ because it's nested under this `unsafe` block ... LL | let _ = || unsafe { unsf() }; | ^^^^^^ unnecessary `unsafe` block - | - = note: this `unsafe` block does contain unsafe operations, but those are already allowed in an `unsafe fn` error: unnecessary `unsafe` block --> $DIR/lint-unused-unsafe.rs:432:28 @@ -756,19 +609,12 @@ LL | let _ = || unsafe { | ^^^^^^ unnecessary `unsafe` block error: unnecessary `unsafe` block - --> $DIR/lint-unused-unsafe.rs:466:20 + --> $DIR/lint-unused-unsafe.rs:467:24 | -LL | unsafe fn granular_disallow_op_in_unsafe_fn_3() { - | ----------------------------------------------- because it's nested under this `unsafe` fn LL | let _ = || unsafe { - | ^^^^^^ unnecessary `unsafe` block - | - = note: this `unsafe` block does contain unsafe operations, but those are already allowed in an `unsafe fn` -note: the lint level is defined here - --> $DIR/lint-unused-unsafe.rs:464:13 - | -LL | #[allow(unsafe_op_in_unsafe_fn)] - | ^^^^^^^^^^^^^^^^^^^^^^ + | ------ because it's nested under this `unsafe` block +LL | let _ = || unsafe { + | ^^^^^^ unnecessary `unsafe` block error: unnecessary `unsafe` block --> $DIR/lint-unused-unsafe.rs:480:24 @@ -988,91 +834,32 @@ error: unnecessary `unsafe` block LL | let _ = || unsafe { | ^^^^^^ unnecessary `unsafe` block -error: unnecessary `unsafe` block - --> $DIR/lint-unused-unsafe.rs:622:24 - | -LL | unsafe fn granularity_2() { - | ------------------------- because it's nested under this `unsafe` fn -LL | let _ = || unsafe { -LL | let _ = || unsafe { let _ = || unsf(); }; - | ^^^^^^ unnecessary `unsafe` block - | - = note: this `unsafe` block does contain unsafe operations, but those are already allowed in an `unsafe fn` -note: the lint level is defined here - --> $DIR/lint-unused-unsafe.rs:619:13 - | -LL | #[allow(unsafe_op_in_unsafe_fn)] - | ^^^^^^^^^^^^^^^^^^^^^^ - -error: unnecessary `unsafe` block - --> $DIR/lint-unused-unsafe.rs:623:24 - | -LL | unsafe fn granularity_2() { - | ------------------------- because it's nested under this `unsafe` fn -... -LL | let _ = || unsafe { let _ = || unsf(); }; - | ^^^^^^ unnecessary `unsafe` block - | - = note: this `unsafe` block does contain unsafe operations, but those are already allowed in an `unsafe fn` - -error: unnecessary `unsafe` block - --> $DIR/lint-unused-unsafe.rs:624:24 - | -LL | unsafe fn granularity_2() { - | ------------------------- because it's nested under this `unsafe` fn -... -LL | let _ = || unsafe { let _ = || unsf(); }; - | ^^^^^^ unnecessary `unsafe` block - | - = note: this `unsafe` block does contain unsafe operations, but those are already allowed in an `unsafe fn` - -error: unnecessary `unsafe` block - --> $DIR/lint-unused-unsafe.rs:630:20 - | -LL | unsafe fn top_level_used_2() { - | ---------------------------- because it's nested under this `unsafe` fn -LL | let _ = || unsafe { - | ^^^^^^ unnecessary `unsafe` block - | - = note: this `unsafe` block does contain unsafe operations, but those are already allowed in an `unsafe fn` -note: the lint level is defined here - --> $DIR/lint-unused-unsafe.rs:628:13 - | -LL | #[allow(unsafe_op_in_unsafe_fn)] - | ^^^^^^^^^^^^^^^^^^^^^^ - error: unnecessary `unsafe` block --> $DIR/lint-unused-unsafe.rs:632:24 | -LL | unsafe fn top_level_used_2() { - | ---------------------------- because it's nested under this `unsafe` fn -... +LL | let _ = || unsafe { + | ------ because it's nested under this `unsafe` block +LL | let _ = || unsf(); LL | let _ = || unsafe { let _ = || unsf(); }; | ^^^^^^ unnecessary `unsafe` block - | - = note: this `unsafe` block does contain unsafe operations, but those are already allowed in an `unsafe fn` error: unnecessary `unsafe` block --> $DIR/lint-unused-unsafe.rs:633:24 | -LL | unsafe fn top_level_used_2() { - | ---------------------------- because it's nested under this `unsafe` fn +LL | let _ = || unsafe { + | ------ because it's nested under this `unsafe` block ... LL | let _ = || unsafe { let _ = || unsf(); }; | ^^^^^^ unnecessary `unsafe` block - | - = note: this `unsafe` block does contain unsafe operations, but those are already allowed in an `unsafe fn` error: unnecessary `unsafe` block --> $DIR/lint-unused-unsafe.rs:634:24 | -LL | unsafe fn top_level_used_2() { - | ---------------------------- because it's nested under this `unsafe` fn +LL | let _ = || unsafe { + | ------ because it's nested under this `unsafe` block ... LL | let _ = || unsafe { let _ = || unsf(); }; | ^^^^^^ unnecessary `unsafe` block - | - = note: this `unsafe` block does contain unsafe operations, but those are already allowed in an `unsafe fn` error: unnecessary `unsafe` block --> $DIR/lint-unused-unsafe.rs:645:28 @@ -1114,19 +901,12 @@ LL | let _ = || unsafe { | ^^^^^^ unnecessary `unsafe` block error: unnecessary `unsafe` block - --> $DIR/lint-unused-unsafe.rs:679:20 + --> $DIR/lint-unused-unsafe.rs:680:24 | -LL | unsafe fn granular_disallow_op_in_unsafe_fn_3() { - | ----------------------------------------------- because it's nested under this `unsafe` fn LL | let _ = || unsafe { - | ^^^^^^ unnecessary `unsafe` block - | - = note: this `unsafe` block does contain unsafe operations, but those are already allowed in an `unsafe fn` -note: the lint level is defined here - --> $DIR/lint-unused-unsafe.rs:677:13 - | -LL | #[allow(unsafe_op_in_unsafe_fn)] - | ^^^^^^^^^^^^^^^^^^^^^^ + | ------ because it's nested under this `unsafe` block +LL | let _ = || unsafe { + | ^^^^^^ unnecessary `unsafe` block error: unnecessary `unsafe` block --> $DIR/lint-unused-unsafe.rs:693:24 @@ -1256,91 +1036,32 @@ error: unnecessary `unsafe` block LL | let _ = || unsafe { | ^^^^^^ unnecessary `unsafe` block -error: unnecessary `unsafe` block - --> $DIR/lint-unused-unsafe.rs:784:28 - | -LL | unsafe fn granularity_2() { - | ------------------------- because it's nested under this `unsafe` fn -LL | let _ = || unsafe { -LL | let _ = || unsafe { unsf() }; - | ^^^^^^ unnecessary `unsafe` block - | - = note: this `unsafe` block does contain unsafe operations, but those are already allowed in an `unsafe fn` -note: the lint level is defined here - --> $DIR/lint-unused-unsafe.rs:781:17 - | -LL | #[allow(unsafe_op_in_unsafe_fn)] - | ^^^^^^^^^^^^^^^^^^^^^^ - -error: unnecessary `unsafe` block - --> $DIR/lint-unused-unsafe.rs:785:28 - | -LL | unsafe fn granularity_2() { - | ------------------------- because it's nested under this `unsafe` fn -... -LL | let _ = || unsafe { unsf() }; - | ^^^^^^ unnecessary `unsafe` block - | - = note: this `unsafe` block does contain unsafe operations, but those are already allowed in an `unsafe fn` - -error: unnecessary `unsafe` block - --> $DIR/lint-unused-unsafe.rs:786:28 - | -LL | unsafe fn granularity_2() { - | ------------------------- because it's nested under this `unsafe` fn -... -LL | let _ = || unsafe { unsf() }; - | ^^^^^^ unnecessary `unsafe` block - | - = note: this `unsafe` block does contain unsafe operations, but those are already allowed in an `unsafe fn` - -error: unnecessary `unsafe` block - --> $DIR/lint-unused-unsafe.rs:792:24 - | -LL | unsafe fn top_level_used_2() { - | ---------------------------- because it's nested under this `unsafe` fn -LL | let _ = || unsafe { - | ^^^^^^ unnecessary `unsafe` block - | - = note: this `unsafe` block does contain unsafe operations, but those are already allowed in an `unsafe fn` -note: the lint level is defined here - --> $DIR/lint-unused-unsafe.rs:790:17 - | -LL | #[allow(unsafe_op_in_unsafe_fn)] - | ^^^^^^^^^^^^^^^^^^^^^^ - error: unnecessary `unsafe` block --> $DIR/lint-unused-unsafe.rs:794:28 | -LL | unsafe fn top_level_used_2() { - | ---------------------------- because it's nested under this `unsafe` fn -... +LL | let _ = || unsafe { + | ------ because it's nested under this `unsafe` block +LL | unsf(); LL | let _ = || unsafe { unsf() }; | ^^^^^^ unnecessary `unsafe` block - | - = note: this `unsafe` block does contain unsafe operations, but those are already allowed in an `unsafe fn` error: unnecessary `unsafe` block --> $DIR/lint-unused-unsafe.rs:795:28 | -LL | unsafe fn top_level_used_2() { - | ---------------------------- because it's nested under this `unsafe` fn +LL | let _ = || unsafe { + | ------ because it's nested under this `unsafe` block ... LL | let _ = || unsafe { unsf() }; | ^^^^^^ unnecessary `unsafe` block - | - = note: this `unsafe` block does contain unsafe operations, but those are already allowed in an `unsafe fn` error: unnecessary `unsafe` block --> $DIR/lint-unused-unsafe.rs:796:28 | -LL | unsafe fn top_level_used_2() { - | ---------------------------- because it's nested under this `unsafe` fn +LL | let _ = || unsafe { + | ------ because it's nested under this `unsafe` block ... LL | let _ = || unsafe { unsf() }; | ^^^^^^ unnecessary `unsafe` block - | - = note: this `unsafe` block does contain unsafe operations, but those are already allowed in an `unsafe fn` error: unnecessary `unsafe` block --> $DIR/lint-unused-unsafe.rs:807:32 @@ -1382,19 +1103,12 @@ LL | let _ = || unsafe { | ^^^^^^ unnecessary `unsafe` block error: unnecessary `unsafe` block - --> $DIR/lint-unused-unsafe.rs:841:24 + --> $DIR/lint-unused-unsafe.rs:842:28 | -LL | unsafe fn granular_disallow_op_in_unsafe_fn_3() { - | ----------------------------------------------- because it's nested under this `unsafe` fn LL | let _ = || unsafe { - | ^^^^^^ unnecessary `unsafe` block - | - = note: this `unsafe` block does contain unsafe operations, but those are already allowed in an `unsafe fn` -note: the lint level is defined here - --> $DIR/lint-unused-unsafe.rs:839:17 - | -LL | #[allow(unsafe_op_in_unsafe_fn)] - | ^^^^^^^^^^^^^^^^^^^^^^ + | ------ because it's nested under this `unsafe` block +LL | let _ = || unsafe { + | ^^^^^^ unnecessary `unsafe` block error: unnecessary `unsafe` block --> $DIR/lint-unused-unsafe.rs:855:28 @@ -1524,91 +1238,32 @@ error: unnecessary `unsafe` block LL | let _ = || unsafe { | ^^^^^^ unnecessary `unsafe` block -error: unnecessary `unsafe` block - --> $DIR/lint-unused-unsafe.rs:942:28 - | -LL | unsafe fn granularity_2() { - | ------------------------- because it's nested under this `unsafe` fn -LL | let _ = || unsafe { -LL | let _ = || unsafe { unsf() }; - | ^^^^^^ unnecessary `unsafe` block - | - = note: this `unsafe` block does contain unsafe operations, but those are already allowed in an `unsafe fn` -note: the lint level is defined here - --> $DIR/lint-unused-unsafe.rs:939:17 - | -LL | #[allow(unsafe_op_in_unsafe_fn)] - | ^^^^^^^^^^^^^^^^^^^^^^ - -error: unnecessary `unsafe` block - --> $DIR/lint-unused-unsafe.rs:943:28 - | -LL | unsafe fn granularity_2() { - | ------------------------- because it's nested under this `unsafe` fn -... -LL | let _ = || unsafe { unsf() }; - | ^^^^^^ unnecessary `unsafe` block - | - = note: this `unsafe` block does contain unsafe operations, but those are already allowed in an `unsafe fn` - -error: unnecessary `unsafe` block - --> $DIR/lint-unused-unsafe.rs:944:28 - | -LL | unsafe fn granularity_2() { - | ------------------------- because it's nested under this `unsafe` fn -... -LL | let _ = || unsafe { unsf() }; - | ^^^^^^ unnecessary `unsafe` block - | - = note: this `unsafe` block does contain unsafe operations, but those are already allowed in an `unsafe fn` - -error: unnecessary `unsafe` block - --> $DIR/lint-unused-unsafe.rs:950:24 - | -LL | unsafe fn top_level_used_2() { - | ---------------------------- because it's nested under this `unsafe` fn -LL | let _ = || unsafe { - | ^^^^^^ unnecessary `unsafe` block - | - = note: this `unsafe` block does contain unsafe operations, but those are already allowed in an `unsafe fn` -note: the lint level is defined here - --> $DIR/lint-unused-unsafe.rs:948:17 - | -LL | #[allow(unsafe_op_in_unsafe_fn)] - | ^^^^^^^^^^^^^^^^^^^^^^ - error: unnecessary `unsafe` block --> $DIR/lint-unused-unsafe.rs:952:28 | -LL | unsafe fn top_level_used_2() { - | ---------------------------- because it's nested under this `unsafe` fn -... +LL | let _ = || unsafe { + | ------ because it's nested under this `unsafe` block +LL | unsf(); LL | let _ = || unsafe { unsf() }; | ^^^^^^ unnecessary `unsafe` block - | - = note: this `unsafe` block does contain unsafe operations, but those are already allowed in an `unsafe fn` error: unnecessary `unsafe` block --> $DIR/lint-unused-unsafe.rs:953:28 | -LL | unsafe fn top_level_used_2() { - | ---------------------------- because it's nested under this `unsafe` fn +LL | let _ = || unsafe { + | ------ because it's nested under this `unsafe` block ... LL | let _ = || unsafe { unsf() }; | ^^^^^^ unnecessary `unsafe` block - | - = note: this `unsafe` block does contain unsafe operations, but those are already allowed in an `unsafe fn` error: unnecessary `unsafe` block --> $DIR/lint-unused-unsafe.rs:954:28 | -LL | unsafe fn top_level_used_2() { - | ---------------------------- because it's nested under this `unsafe` fn +LL | let _ = || unsafe { + | ------ because it's nested under this `unsafe` block ... LL | let _ = || unsafe { unsf() }; | ^^^^^^ unnecessary `unsafe` block - | - = note: this `unsafe` block does contain unsafe operations, but those are already allowed in an `unsafe fn` error: unnecessary `unsafe` block --> $DIR/lint-unused-unsafe.rs:965:32 @@ -1650,19 +1305,12 @@ LL | let _ = || unsafe { | ^^^^^^ unnecessary `unsafe` block error: unnecessary `unsafe` block - --> $DIR/lint-unused-unsafe.rs:999:24 + --> $DIR/lint-unused-unsafe.rs:1000:28 | -LL | unsafe fn granular_disallow_op_in_unsafe_fn_3() { - | ----------------------------------------------- because it's nested under this `unsafe` fn LL | let _ = || unsafe { - | ^^^^^^ unnecessary `unsafe` block - | - = note: this `unsafe` block does contain unsafe operations, but those are already allowed in an `unsafe fn` -note: the lint level is defined here - --> $DIR/lint-unused-unsafe.rs:997:17 - | -LL | #[allow(unsafe_op_in_unsafe_fn)] - | ^^^^^^^^^^^^^^^^^^^^^^ + | ------ because it's nested under this `unsafe` block +LL | let _ = || unsafe { + | ^^^^^^ unnecessary `unsafe` block error: unnecessary `unsafe` block --> $DIR/lint-unused-unsafe.rs:1013:28 @@ -1672,21 +1320,6 @@ LL | let _ = || unsafe { LL | let _ = || unsafe { | ^^^^^^ unnecessary `unsafe` block -error: unnecessary `unsafe` block - --> $DIR/lint-unused-unsafe.rs:1044:9 - | -LL | unsafe fn multiple_unsafe_op_in_unsafe_fn_allows() { - | -------------------------------------------------- because it's nested under this `unsafe` fn -LL | unsafe { - | ^^^^^^ unnecessary `unsafe` block - | - = note: this `unsafe` block does contain unsafe operations, but those are already allowed in an `unsafe fn` -note: the lint level is defined here - --> $DIR/lint-unused-unsafe.rs:1045:21 - | -LL | #[allow(unsafe_op_in_unsafe_fn)] - | ^^^^^^^^^^^^^^^^^^^^^^ - error: unnecessary `unsafe` block --> $DIR/lint-unused-unsafe.rs:1059:29 | @@ -1726,87 +1359,32 @@ error: unnecessary `unsafe` block LL | let _ = async { unsafe { | ^^^^^^ unnecessary `unsafe` block -error: unnecessary `unsafe` block - --> $DIR/lint-unused-unsafe.rs:1074:33 - | -LL | async unsafe fn async_blocks() { - | ------------------------------ because it's nested under this `unsafe` fn -... -LL | let _ = async { unsafe { let _ = async { unsf() }; }}; - | ^^^^^^ unnecessary `unsafe` block - | - = note: this `unsafe` block does contain unsafe operations, but those are already allowed in an `unsafe fn` -note: the lint level is defined here - --> $DIR/lint-unused-unsafe.rs:1071:17 - | -LL | #[allow(unsafe_op_in_unsafe_fn)] - | ^^^^^^^^^^^^^^^^^^^^^^ - -error: unnecessary `unsafe` block - --> $DIR/lint-unused-unsafe.rs:1075:33 - | -LL | async unsafe fn async_blocks() { - | ------------------------------ because it's nested under this `unsafe` fn -... -LL | let _ = async { unsafe { let _ = async { unsf() }; }}; - | ^^^^^^ unnecessary `unsafe` block - | - = note: this `unsafe` block does contain unsafe operations, but those are already allowed in an `unsafe fn` - -error: unnecessary `unsafe` block - --> $DIR/lint-unused-unsafe.rs:1076:33 - | -LL | async unsafe fn async_blocks() { - | ------------------------------ because it's nested under this `unsafe` fn -... -LL | let _ = async { unsafe { let _ = async { unsf() }; }}; - | ^^^^^^ unnecessary `unsafe` block - | - = note: this `unsafe` block does contain unsafe operations, but those are already allowed in an `unsafe fn` - -error: unnecessary `unsafe` block - --> $DIR/lint-unused-unsafe.rs:1078:29 - | -LL | async unsafe fn async_blocks() { - | ------------------------------ because it's nested under this `unsafe` fn -... -LL | let _ = async { unsafe { - | ^^^^^^ unnecessary `unsafe` block - | - = note: this `unsafe` block does contain unsafe operations, but those are already allowed in an `unsafe fn` - error: unnecessary `unsafe` block --> $DIR/lint-unused-unsafe.rs:1080:33 | -LL | async unsafe fn async_blocks() { - | ------------------------------ because it's nested under this `unsafe` fn -... +LL | let _ = async { unsafe { + | ------ because it's nested under this `unsafe` block +LL | let _ = async { unsf() }; LL | let _ = async { unsafe { let _ = async { unsf() }; }}; | ^^^^^^ unnecessary `unsafe` block - | - = note: this `unsafe` block does contain unsafe operations, but those are already allowed in an `unsafe fn` error: unnecessary `unsafe` block --> $DIR/lint-unused-unsafe.rs:1081:33 | -LL | async unsafe fn async_blocks() { - | ------------------------------ because it's nested under this `unsafe` fn +LL | let _ = async { unsafe { + | ------ because it's nested under this `unsafe` block ... LL | let _ = async { unsafe { let _ = async { unsf() }; }}; | ^^^^^^ unnecessary `unsafe` block - | - = note: this `unsafe` block does contain unsafe operations, but those are already allowed in an `unsafe fn` error: unnecessary `unsafe` block --> $DIR/lint-unused-unsafe.rs:1082:33 | -LL | async unsafe fn async_blocks() { - | ------------------------------ because it's nested under this `unsafe` fn +LL | let _ = async { unsafe { + | ------ because it's nested under this `unsafe` block ... LL | let _ = async { unsafe { let _ = async { unsf() }; }}; | ^^^^^^ unnecessary `unsafe` block - | - = note: this `unsafe` block does contain unsafe operations, but those are already allowed in an `unsafe fn` error: unnecessary `unsafe` block --> $DIR/lint-unused-unsafe.rs:1092:22 @@ -1820,5 +1398,5 @@ error: unnecessary `unsafe` block LL | let _x: [(); unsafe { unsafe { size() } }] = []; | ^^^^^^ unnecessary `unsafe` block -error: aborting due to 201 previous errors +error: aborting due to 174 previous errors diff --git a/src/test/ui/span/lint-unused-unsafe.rs b/src/test/ui/span/lint-unused-unsafe.rs index f8d1dff3572be..5d042768be002 100644 --- a/src/test/ui/span/lint-unused-unsafe.rs +++ b/src/test/ui/span/lint-unused-unsafe.rs @@ -27,7 +27,7 @@ fn bad1() { unsafe {} } //~ ERROR: unnecessary `unsafe` block fn bad2() { unsafe { bad1() } } //~ ERROR: unnecessary `unsafe` block unsafe fn bad3() { unsafe {} } //~ ERROR: unnecessary `unsafe` block fn bad4() { unsafe { callback(||{}) } } //~ ERROR: unnecessary `unsafe` block -unsafe fn bad5() { unsafe { unsf() } } //~ ERROR: unnecessary `unsafe` block +unsafe fn bad5() { unsafe { unsf() } } fn bad6() { unsafe { //~ ERROR: unnecessary `unsafe` block unsafe { // don't put the warning here @@ -37,7 +37,7 @@ fn bad6() { } unsafe fn bad7() { unsafe { //~ ERROR: unnecessary `unsafe` block - unsafe { //~ ERROR: unnecessary `unsafe` block + unsafe { unsf() } } @@ -194,15 +194,15 @@ mod additional_tests { #[allow(unsafe_op_in_unsafe_fn)] unsafe fn granularity_2() { unsafe { //~ ERROR: unnecessary `unsafe` block - unsafe { unsf() } //~ ERROR: unnecessary `unsafe` block - unsafe { unsf() } //~ ERROR: unnecessary `unsafe` block - unsafe { unsf() } //~ ERROR: unnecessary `unsafe` block + unsafe { unsf() } + unsafe { unsf() } + unsafe { unsf() } } } #[allow(unsafe_op_in_unsafe_fn)] unsafe fn top_level_used_2() { - unsafe { //~ ERROR: unnecessary `unsafe` block + unsafe { unsf(); unsafe { unsf() } //~ ERROR: unnecessary `unsafe` block unsafe { unsf() } //~ ERROR: unnecessary `unsafe` block @@ -251,8 +251,8 @@ mod additional_tests { #[allow(unsafe_op_in_unsafe_fn)] unsafe fn granular_disallow_op_in_unsafe_fn_3() { - unsafe { //~ ERROR: unnecessary `unsafe` block - unsafe { + unsafe { + unsafe { //~ ERROR: unnecessary `unsafe` block #[deny(unsafe_op_in_unsafe_fn)] { unsf(); @@ -406,15 +406,15 @@ mod additional_tests_closures { #[allow(unsafe_op_in_unsafe_fn)] unsafe fn granularity_2() { let _ = || unsafe { //~ ERROR: unnecessary `unsafe` block - let _ = || unsafe { unsf() }; //~ ERROR: unnecessary `unsafe` block - let _ = || unsafe { unsf() }; //~ ERROR: unnecessary `unsafe` block - let _ = || unsafe { unsf() }; //~ ERROR: unnecessary `unsafe` block + let _ = || unsafe { unsf() }; + let _ = || unsafe { unsf() }; + let _ = || unsafe { unsf() }; }; } #[allow(unsafe_op_in_unsafe_fn)] unsafe fn top_level_used_2() { - let _ = || unsafe { //~ ERROR: unnecessary `unsafe` block + let _ = || unsafe { unsf(); let _ = || unsafe { unsf() }; //~ ERROR: unnecessary `unsafe` block let _ = || unsafe { unsf() }; //~ ERROR: unnecessary `unsafe` block @@ -463,8 +463,8 @@ mod additional_tests_closures { #[allow(unsafe_op_in_unsafe_fn)] unsafe fn granular_disallow_op_in_unsafe_fn_3() { - let _ = || unsafe { //~ ERROR: unnecessary `unsafe` block - let _ = || unsafe { + let _ = || unsafe { + let _ = || unsafe { //~ ERROR: unnecessary `unsafe` block #[deny(unsafe_op_in_unsafe_fn)] { unsf(); @@ -619,15 +619,15 @@ mod additional_tests_even_more_closures { #[allow(unsafe_op_in_unsafe_fn)] unsafe fn granularity_2() { let _ = || unsafe { //~ ERROR: unnecessary `unsafe` block - let _ = || unsafe { let _ = || unsf(); }; //~ ERROR: unnecessary `unsafe` block - let _ = || unsafe { let _ = || unsf(); }; //~ ERROR: unnecessary `unsafe` block - let _ = || unsafe { let _ = || unsf(); }; //~ ERROR: unnecessary `unsafe` block + let _ = || unsafe { let _ = || unsf(); }; + let _ = || unsafe { let _ = || unsf(); }; + let _ = || unsafe { let _ = || unsf(); }; }; } #[allow(unsafe_op_in_unsafe_fn)] unsafe fn top_level_used_2() { - let _ = || unsafe { //~ ERROR: unnecessary `unsafe` block + let _ = || unsafe { let _ = || unsf(); let _ = || unsafe { let _ = || unsf(); }; //~ ERROR: unnecessary `unsafe` block let _ = || unsafe { let _ = || unsf(); }; //~ ERROR: unnecessary `unsafe` block @@ -676,8 +676,8 @@ mod additional_tests_even_more_closures { #[allow(unsafe_op_in_unsafe_fn)] unsafe fn granular_disallow_op_in_unsafe_fn_3() { - let _ = || unsafe { //~ ERROR: unnecessary `unsafe` block - let _ = || unsafe { + let _ = || unsafe { + let _ = || unsafe { //~ ERROR: unnecessary `unsafe` block #[deny(unsafe_op_in_unsafe_fn)] { let _ = || unsf(); @@ -781,15 +781,15 @@ mod item_likes { #[allow(unsafe_op_in_unsafe_fn)] unsafe fn granularity_2() { let _ = || unsafe { //~ ERROR: unnecessary `unsafe` block - let _ = || unsafe { unsf() }; //~ ERROR: unnecessary `unsafe` block - let _ = || unsafe { unsf() }; //~ ERROR: unnecessary `unsafe` block - let _ = || unsafe { unsf() }; //~ ERROR: unnecessary `unsafe` block + let _ = || unsafe { unsf() }; + let _ = || unsafe { unsf() }; + let _ = || unsafe { unsf() }; }; } #[allow(unsafe_op_in_unsafe_fn)] unsafe fn top_level_used_2() { - let _ = || unsafe { //~ ERROR: unnecessary `unsafe` block + let _ = || unsafe { unsf(); let _ = || unsafe { unsf() }; //~ ERROR: unnecessary `unsafe` block let _ = || unsafe { unsf() }; //~ ERROR: unnecessary `unsafe` block @@ -838,8 +838,8 @@ mod item_likes { #[allow(unsafe_op_in_unsafe_fn)] unsafe fn granular_disallow_op_in_unsafe_fn_3() { - let _ = || unsafe { //~ ERROR: unnecessary `unsafe` block - let _ = || unsafe { + let _ = || unsafe { + let _ = || unsafe { //~ ERROR: unnecessary `unsafe` block #[deny(unsafe_op_in_unsafe_fn)] { unsf(); @@ -939,15 +939,15 @@ mod item_likes { #[allow(unsafe_op_in_unsafe_fn)] unsafe fn granularity_2() { let _ = || unsafe { //~ ERROR: unnecessary `unsafe` block - let _ = || unsafe { unsf() }; //~ ERROR: unnecessary `unsafe` block - let _ = || unsafe { unsf() }; //~ ERROR: unnecessary `unsafe` block - let _ = || unsafe { unsf() }; //~ ERROR: unnecessary `unsafe` block + let _ = || unsafe { unsf() }; + let _ = || unsafe { unsf() }; + let _ = || unsafe { unsf() }; }; } #[allow(unsafe_op_in_unsafe_fn)] unsafe fn top_level_used_2() { - let _ = || unsafe { //~ ERROR: unnecessary `unsafe` block + let _ = || unsafe { unsf(); let _ = || unsafe { unsf() }; //~ ERROR: unnecessary `unsafe` block let _ = || unsafe { unsf() }; //~ ERROR: unnecessary `unsafe` block @@ -996,8 +996,8 @@ mod item_likes { #[allow(unsafe_op_in_unsafe_fn)] unsafe fn granular_disallow_op_in_unsafe_fn_3() { - let _ = || unsafe { //~ ERROR: unnecessary `unsafe` block - let _ = || unsafe { + let _ = || unsafe { + let _ = || unsafe { //~ ERROR: unnecessary `unsafe` block #[deny(unsafe_op_in_unsafe_fn)] { unsf(); @@ -1041,7 +1041,7 @@ mod additional_tests_extra { #[warn(unsafe_op_in_unsafe_fn)] unsafe fn multiple_unsafe_op_in_unsafe_fn_allows() { - unsafe { //~ ERROR: unnecessary `unsafe` block + unsafe { #[allow(unsafe_op_in_unsafe_fn)] { unsf(); @@ -1071,11 +1071,11 @@ mod additional_tests_extra { #[allow(unsafe_op_in_unsafe_fn)] { let _ = async { unsafe { //~ ERROR: unnecessary `unsafe` block - let _ = async { unsafe { let _ = async { unsf() }; }}; //~ ERROR: unnecessary `unsafe` block - let _ = async { unsafe { let _ = async { unsf() }; }}; //~ ERROR: unnecessary `unsafe` block - let _ = async { unsafe { let _ = async { unsf() }; }}; //~ ERROR: unnecessary `unsafe` block + let _ = async { unsafe { let _ = async { unsf() }; }}; + let _ = async { unsafe { let _ = async { unsf() }; }}; + let _ = async { unsafe { let _ = async { unsf() }; }}; }}; - let _ = async { unsafe { //~ ERROR: unnecessary `unsafe` block + let _ = async { unsafe { let _ = async { unsf() }; let _ = async { unsafe { let _ = async { unsf() }; }}; //~ ERROR: unnecessary `unsafe` block let _ = async { unsafe { let _ = async { unsf() }; }}; //~ ERROR: unnecessary `unsafe` block diff --git a/src/test/ui/unsafe/rfc-2585-unsafe_op_in_unsafe_fn.mir.stderr b/src/test/ui/unsafe/rfc-2585-unsafe_op_in_unsafe_fn.mir.stderr index fd58e1b1ebe37..b968174dd2d7e 100644 --- a/src/test/ui/unsafe/rfc-2585-unsafe_op_in_unsafe_fn.mir.stderr +++ b/src/test/ui/unsafe/rfc-2585-unsafe_op_in_unsafe_fn.mir.stderr @@ -81,40 +81,8 @@ error: unnecessary `unsafe` block LL | unsafe { unsafe { unsf() } } | ^^^^^^ unnecessary `unsafe` block -error: unnecessary `unsafe` block - --> $DIR/rfc-2585-unsafe_op_in_unsafe_fn.rs:60:5 - | -LL | unsafe fn allow_level() { - | ----------------------- because it's nested under this `unsafe` fn -... -LL | unsafe { unsf() } - | ^^^^^^ unnecessary `unsafe` block - | - = note: this `unsafe` block does contain unsafe operations, but those are already allowed in an `unsafe fn` -note: the lint level is defined here - --> $DIR/rfc-2585-unsafe_op_in_unsafe_fn.rs:53:9 - | -LL | #[allow(unsafe_op_in_unsafe_fn)] - | ^^^^^^^^^^^^^^^^^^^^^^ - -error: unnecessary `unsafe` block - --> $DIR/rfc-2585-unsafe_op_in_unsafe_fn.rs:72:9 - | -LL | unsafe fn nested_allow_level() { - | ------------------------------ because it's nested under this `unsafe` fn -... -LL | unsafe { unsf() } - | ^^^^^^ unnecessary `unsafe` block - | - = note: this `unsafe` block does contain unsafe operations, but those are already allowed in an `unsafe fn` -note: the lint level is defined here - --> $DIR/rfc-2585-unsafe_op_in_unsafe_fn.rs:65:13 - | -LL | #[allow(unsafe_op_in_unsafe_fn)] - | ^^^^^^^^^^^^^^^^^^^^^^ - error[E0133]: call to unsafe function is unsafe and requires unsafe block - --> $DIR/rfc-2585-unsafe_op_in_unsafe_fn.rs:78:5 + --> $DIR/rfc-2585-unsafe_op_in_unsafe_fn.rs:76:5 | LL | unsf(); | ^^^^^^ call to unsafe function @@ -122,13 +90,13 @@ LL | unsf(); = note: consult the function's documentation for information on how to avoid undefined behavior error[E0133]: call to unsafe function is unsafe and requires unsafe function or block - --> $DIR/rfc-2585-unsafe_op_in_unsafe_fn.rs:83:9 + --> $DIR/rfc-2585-unsafe_op_in_unsafe_fn.rs:81:9 | LL | unsf(); | ^^^^^^ call to unsafe function | = note: consult the function's documentation for information on how to avoid undefined behavior -error: aborting due to 13 previous errors +error: aborting due to 11 previous errors For more information about this error, try `rustc --explain E0133`. diff --git a/src/test/ui/unsafe/rfc-2585-unsafe_op_in_unsafe_fn.rs b/src/test/ui/unsafe/rfc-2585-unsafe_op_in_unsafe_fn.rs index 30b072340341b..db1e916a36c1f 100644 --- a/src/test/ui/unsafe/rfc-2585-unsafe_op_in_unsafe_fn.rs +++ b/src/test/ui/unsafe/rfc-2585-unsafe_op_in_unsafe_fn.rs @@ -58,7 +58,6 @@ unsafe fn allow_level() { VOID = (); unsafe { unsf() } - //~^ ERROR unnecessary `unsafe` block } unsafe fn nested_allow_level() { @@ -70,7 +69,6 @@ unsafe fn nested_allow_level() { VOID = (); unsafe { unsf() } - //~^ ERROR unnecessary `unsafe` block } } diff --git a/src/test/ui/unsafe/rfc-2585-unsafe_op_in_unsafe_fn.thir.stderr b/src/test/ui/unsafe/rfc-2585-unsafe_op_in_unsafe_fn.thir.stderr index 2ba6a72930df8..e365293657e30 100644 --- a/src/test/ui/unsafe/rfc-2585-unsafe_op_in_unsafe_fn.thir.stderr +++ b/src/test/ui/unsafe/rfc-2585-unsafe_op_in_unsafe_fn.thir.stderr @@ -83,26 +83,8 @@ LL | unsafe { unsafe { unsf() } } | | | because it's nested under this `unsafe` block -error: unnecessary `unsafe` block - --> $DIR/rfc-2585-unsafe_op_in_unsafe_fn.rs:60:5 - | -LL | unsafe fn allow_level() { - | ----------------------- because it's nested under this `unsafe` fn -... -LL | unsafe { unsf() } - | ^^^^^^ unnecessary `unsafe` block - -error: unnecessary `unsafe` block - --> $DIR/rfc-2585-unsafe_op_in_unsafe_fn.rs:72:9 - | -LL | unsafe fn nested_allow_level() { - | ------------------------------ because it's nested under this `unsafe` fn -... -LL | unsafe { unsf() } - | ^^^^^^ unnecessary `unsafe` block - error[E0133]: call to unsafe function `unsf` is unsafe and requires unsafe block - --> $DIR/rfc-2585-unsafe_op_in_unsafe_fn.rs:78:5 + --> $DIR/rfc-2585-unsafe_op_in_unsafe_fn.rs:76:5 | LL | unsf(); | ^^^^^^ call to unsafe function @@ -110,13 +92,13 @@ LL | unsf(); = note: consult the function's documentation for information on how to avoid undefined behavior error[E0133]: call to unsafe function `unsf` is unsafe and requires unsafe function or block - --> $DIR/rfc-2585-unsafe_op_in_unsafe_fn.rs:83:9 + --> $DIR/rfc-2585-unsafe_op_in_unsafe_fn.rs:81:9 | LL | unsf(); | ^^^^^^ call to unsafe function | = note: consult the function's documentation for information on how to avoid undefined behavior -error: aborting due to 13 previous errors +error: aborting due to 11 previous errors For more information about this error, try `rustc --explain E0133`. From 3e44ca95dd844863eeb9bba44209e43d2afb81a4 Mon Sep 17 00:00:00 2001 From: Ralf Jung Date: Tue, 2 Aug 2022 17:04:52 -0400 Subject: [PATCH 2/4] remove some unused code and types --- compiler/rustc_middle/src/mir/query.rs | 22 +------ .../rustc_mir_transform/src/check_unsafety.rs | 64 +++++-------------- 2 files changed, 17 insertions(+), 69 deletions(-) diff --git a/compiler/rustc_middle/src/mir/query.rs b/compiler/rustc_middle/src/mir/query.rs index dd9f8795f94ff..594c14a642ded 100644 --- a/compiler/rustc_middle/src/mir/query.rs +++ b/compiler/rustc_middle/src/mir/query.rs @@ -2,7 +2,7 @@ use crate::mir::{Body, ConstantKind, Promoted}; use crate::ty::{self, OpaqueHiddenType, Ty, TyCtxt}; -use rustc_data_structures::fx::FxHashMap; +use rustc_data_structures::fx::FxHashSet; use rustc_data_structures::vec_map::VecMap; use rustc_errors::ErrorGuaranteed; use rustc_hir as hir; @@ -115,21 +115,6 @@ pub enum UnusedUnsafe { /// `unsafe` block nested under another (used) `unsafe` block /// > ``… because it's nested under this `unsafe` block`` InUnsafeBlock(hir::HirId), - /// `unsafe` block nested under `unsafe fn` - /// > ``… because it's nested under this `unsafe fn` `` - /// - /// the second HirId here indicates the first usage of the `unsafe` block, - /// which allows retrieval of the LintLevelSource for why that operation would - /// have been permitted without the block - InUnsafeFn(hir::HirId, hir::HirId), -} - -#[derive(Copy, Clone, PartialEq, TyEncodable, TyDecodable, HashStable, Debug)] -pub enum UsedUnsafeBlockData { - SomeDisallowedInUnsafeFn, - // the HirId here indicates the first usage of the `unsafe` block - // (i.e. the one that's first encountered in the MIR traversal of the unsafety check) - AllAllowedInUnsafeFn(hir::HirId), } #[derive(TyEncodable, TyDecodable, HashStable, Debug)] @@ -138,10 +123,7 @@ pub struct UnsafetyCheckResult { pub violations: Vec, /// Used `unsafe` blocks in this function. This is used for the "unused_unsafe" lint. - /// - /// The keys are the used `unsafe` blocks, the UnusedUnsafeKind indicates whether - /// or not any of the usages happen at a place that doesn't allow `unsafe_op_in_unsafe_fn`. - pub used_unsafe_blocks: FxHashMap, + pub used_unsafe_blocks: FxHashSet, /// This is `Some` iff the item is not a closure. pub unused_unsafes: Option>, diff --git a/compiler/rustc_mir_transform/src/check_unsafety.rs b/compiler/rustc_mir_transform/src/check_unsafety.rs index 9ea87245d034a..a9eb60c7a15ad 100644 --- a/compiler/rustc_mir_transform/src/check_unsafety.rs +++ b/compiler/rustc_mir_transform/src/check_unsafety.rs @@ -1,4 +1,4 @@ -use rustc_data_structures::fx::FxHashMap; +use rustc_data_structures::fx::FxHashSet; use rustc_errors::struct_span_err; use rustc_hir as hir; use rustc_hir::def_id::{DefId, LocalDefId}; @@ -11,7 +11,6 @@ use rustc_middle::ty::{self, TyCtxt}; use rustc_session::lint::builtin::{UNSAFE_OP_IN_UNSAFE_FN, UNUSED_UNSAFE}; use rustc_session::lint::Level; -use std::collections::hash_map; use std::ops::Bound; pub struct UnsafetyChecker<'a, 'tcx> { @@ -26,7 +25,7 @@ pub struct UnsafetyChecker<'a, 'tcx> { /// /// The keys are the used `unsafe` blocks, the UnusedUnsafeKind indicates whether /// or not any of the usages happen at a place that doesn't allow `unsafe_op_in_unsafe_fn`. - used_unsafe_blocks: FxHashMap, + used_unsafe_blocks: FxHashSet, } impl<'a, 'tcx> UnsafetyChecker<'a, 'tcx> { @@ -130,10 +129,7 @@ impl<'tcx> Visitor<'tcx> for UnsafetyChecker<'_, 'tcx> { &AggregateKind::Closure(def_id, _) | &AggregateKind::Generator(def_id, _, _) => { let UnsafetyCheckResult { violations, used_unsafe_blocks, .. } = self.tcx.unsafety_check_result(def_id); - self.register_violations( - violations, - used_unsafe_blocks.iter().map(|(&h, &d)| (h, d)), - ); + self.register_violations(violations, used_unsafe_blocks.iter().copied()); } }, _ => {} @@ -257,22 +253,8 @@ impl<'tcx> UnsafetyChecker<'_, 'tcx> { fn register_violations<'a>( &mut self, violations: impl IntoIterator, - new_used_unsafe_blocks: impl IntoIterator, + new_used_unsafe_blocks: impl IntoIterator, ) { - use UsedUnsafeBlockData::*; - - let update_entry = |this: &mut Self, hir_id, new_usage| { - match this.used_unsafe_blocks.entry(hir_id) { - hash_map::Entry::Occupied(mut entry) => { - if new_usage == SomeDisallowedInUnsafeFn { - *entry.get_mut() = SomeDisallowedInUnsafeFn; - } - } - hash_map::Entry::Vacant(entry) => { - entry.insert(new_usage); - } - }; - }; let safety = self.body.source_scopes[self.source_info.scope] .local_data .as_ref() @@ -300,17 +282,13 @@ impl<'tcx> UnsafetyChecker<'_, 'tcx> { }), Safety::BuiltinUnsafe => {} Safety::ExplicitUnsafe(hir_id) => violations.into_iter().for_each(|_violation| { - update_entry( - self, - hir_id, - SomeDisallowedInUnsafeFn, - ) + self.used_unsafe_blocks.insert(hir_id); }), }; - new_used_unsafe_blocks - .into_iter() - .for_each(|(hir_id, usage_data)| update_entry(self, hir_id, usage_data)); + new_used_unsafe_blocks.into_iter().for_each(|hir_id| { + self.used_unsafe_blocks.insert(hir_id); + }); } fn check_mut_borrowing_layout_constrained_field( &mut self, @@ -407,34 +385,28 @@ enum Context { struct UnusedUnsafeVisitor<'a, 'tcx> { tcx: TyCtxt<'tcx>, - used_unsafe_blocks: &'a FxHashMap, + used_unsafe_blocks: &'a FxHashSet, context: Context, unused_unsafes: &'a mut Vec<(HirId, UnusedUnsafe)>, } impl<'tcx> intravisit::Visitor<'tcx> for UnusedUnsafeVisitor<'_, 'tcx> { fn visit_block(&mut self, block: &'tcx hir::Block<'tcx>) { - use UsedUnsafeBlockData::{AllAllowedInUnsafeFn, SomeDisallowedInUnsafeFn}; - if let hir::BlockCheckMode::UnsafeBlock(hir::UnsafeSource::UserProvided) = block.rules { let used = match self.tcx.lint_level_at_node(UNUSED_UNSAFE, block.hir_id) { - (Level::Allow, _) => Some(SomeDisallowedInUnsafeFn), - _ => self.used_unsafe_blocks.get(&block.hir_id).copied(), + (Level::Allow, _) => true, + _ => self.used_unsafe_blocks.contains(&block.hir_id), }; let unused_unsafe = match (self.context, used) { - (_, None) => UnusedUnsafe::Unused, - (Context::Safe, Some(_)) - | (Context::UnsafeFn(_), Some(SomeDisallowedInUnsafeFn)) => { + (_, false) => UnusedUnsafe::Unused, + (Context::Safe, true) | (Context::UnsafeFn(_), true) => { let previous_context = self.context; self.context = Context::UnsafeBlock(block.hir_id); intravisit::walk_block(self, block); self.context = previous_context; return; } - (Context::UnsafeFn(hir_id), Some(AllAllowedInUnsafeFn(lint_root))) => { - UnusedUnsafe::InUnsafeFn(hir_id, lint_root) - } - (Context::UnsafeBlock(hir_id), Some(_)) => UnusedUnsafe::InUnsafeBlock(hir_id), + (Context::UnsafeBlock(hir_id), true) => UnusedUnsafe::InUnsafeBlock(hir_id), }; self.unused_unsafes.push((block.hir_id, unused_unsafe)); } @@ -458,7 +430,7 @@ impl<'tcx> intravisit::Visitor<'tcx> for UnusedUnsafeVisitor<'_, 'tcx> { fn check_unused_unsafe( tcx: TyCtxt<'_>, def_id: LocalDefId, - used_unsafe_blocks: &FxHashMap, + used_unsafe_blocks: &FxHashSet, ) -> Vec<(HirId, UnusedUnsafe)> { let body_id = tcx.hir().maybe_body_owned_by(def_id); @@ -518,11 +490,6 @@ fn unsafety_check_result<'tcx>( } fn report_unused_unsafe(tcx: TyCtxt<'_>, kind: UnusedUnsafe, id: HirId) { - if matches!(kind, UnusedUnsafe::InUnsafeFn(..)) { - // We do *not* warn here, these unsafe blocks are actually required when - // `unsafe_op_in_unsafe_fn` is warn or higher. - return; - } let span = tcx.sess.source_map().guess_head_span(tcx.hir().span(id)); tcx.struct_span_lint_hir(UNUSED_UNSAFE, id, span, |lint| { let msg = "unnecessary `unsafe` block"; @@ -536,7 +503,6 @@ fn report_unused_unsafe(tcx: TyCtxt<'_>, kind: UnusedUnsafe, id: HirId) { "because it's nested under this `unsafe` block", ); } - UnusedUnsafe::InUnsafeFn(_id, _usage_lint_root) => unreachable!(), } db.emit(); From 35a35d86fdca99b22b07ed7d998ce05032e800d4 Mon Sep 17 00:00:00 2001 From: Ralf Jung Date: Tue, 2 Aug 2022 18:55:43 -0400 Subject: [PATCH 3/4] update comment MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Léo Lanteri Thauvin --- compiler/rustc_mir_transform/src/check_unsafety.rs | 3 --- 1 file changed, 3 deletions(-) diff --git a/compiler/rustc_mir_transform/src/check_unsafety.rs b/compiler/rustc_mir_transform/src/check_unsafety.rs index a9eb60c7a15ad..0f5fd77f7ab16 100644 --- a/compiler/rustc_mir_transform/src/check_unsafety.rs +++ b/compiler/rustc_mir_transform/src/check_unsafety.rs @@ -22,9 +22,6 @@ pub struct UnsafetyChecker<'a, 'tcx> { param_env: ty::ParamEnv<'tcx>, /// Used `unsafe` blocks in this function. This is used for the "unused_unsafe" lint. - /// - /// The keys are the used `unsafe` blocks, the UnusedUnsafeKind indicates whether - /// or not any of the usages happen at a place that doesn't allow `unsafe_op_in_unsafe_fn`. used_unsafe_blocks: FxHashSet, } From 86e2ca30880a06cb9dbcef1db4a20266e54cf862 Mon Sep 17 00:00:00 2001 From: Ralf Jung Date: Tue, 2 Aug 2022 18:57:22 -0400 Subject: [PATCH 4/4] add link to discussion --- compiler/rustc_mir_build/src/check_unsafety.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/compiler/rustc_mir_build/src/check_unsafety.rs b/compiler/rustc_mir_build/src/check_unsafety.rs index e3157a4300195..f0b0456c4b961 100644 --- a/compiler/rustc_mir_build/src/check_unsafety.rs +++ b/compiler/rustc_mir_build/src/check_unsafety.rs @@ -77,7 +77,8 @@ impl<'tcx> UnsafetyVisitor<'_, 'tcx> { SafetyContext::UnsafeBlock { ref mut used, .. } => { // Mark this block as useful (even inside `unsafe fn`, where it is technically // redundant -- but we want to eventually enable `unsafe_op_in_unsafe_fn` by - // default which will require those blocks). + // default which will require those blocks: + // https://github.com/rust-lang/rust/issues/71668#issuecomment-1203075594). *used = true; } SafetyContext::UnsafeFn if unsafe_op_in_unsafe_fn_allowed => {}