Skip to content

Commit ee3fc9d

Browse files
committed
never consider unsafe blocks unused if they would be required with unsafe_op_in_unsafe_fn
1 parent 4493a0f commit ee3fc9d

10 files changed

+124
-631
lines changed

compiler/rustc_errors/src/diagnostic_builder.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -566,7 +566,7 @@ impl Drop for DiagnosticBuilderInner<'_> {
566566
),
567567
));
568568
handler.emit_diagnostic(&mut self.diagnostic);
569-
panic!();
569+
panic!("error was constructed but not emitted");
570570
}
571571
}
572572
// `.emit()` was previously called, or maybe we're during `.cancel()`.

compiler/rustc_mir_build/src/check_unsafety.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -75,10 +75,10 @@ impl<'tcx> UnsafetyVisitor<'_, 'tcx> {
7575
match self.safety_context {
7676
SafetyContext::BuiltinUnsafeBlock => {}
7777
SafetyContext::UnsafeBlock { ref mut used, .. } => {
78-
if !self.body_unsafety.is_unsafe() || !unsafe_op_in_unsafe_fn_allowed {
79-
// Mark this block as useful
80-
*used = true;
81-
}
78+
// Mark this block as useful (even inside `unsafe fn`, where it is technically
79+
// redundant -- but we want to eventually enable `unsafe_op_in_unsafe_fn` by
80+
// default which will require those blocks).
81+
*used = true;
8282
}
8383
SafetyContext::UnsafeFn if unsafe_op_in_unsafe_fn_allowed => {}
8484
SafetyContext::UnsafeFn => {

compiler/rustc_mir_transform/src/check_unsafety.rs

Lines changed: 10 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@ use rustc_hir::def_id::{DefId, LocalDefId};
55
use rustc_hir::hir_id::HirId;
66
use rustc_hir::intravisit;
77
use rustc_middle::mir::visit::{MutatingUseContext, PlaceContext, Visitor};
8+
use rustc_middle::mir::*;
89
use rustc_middle::ty::query::Providers;
910
use rustc_middle::ty::{self, TyCtxt};
10-
use rustc_middle::{lint, mir::*};
1111
use rustc_session::lint::builtin::{UNSAFE_OP_IN_UNSAFE_FN, UNUSED_UNSAFE};
1212
use rustc_session::lint::Level;
1313

@@ -259,7 +259,7 @@ impl<'tcx> UnsafetyChecker<'_, 'tcx> {
259259
violations: impl IntoIterator<Item = &'a UnsafetyViolation>,
260260
new_used_unsafe_blocks: impl IntoIterator<Item = (HirId, UsedUnsafeBlockData)>,
261261
) {
262-
use UsedUnsafeBlockData::{AllAllowedInUnsafeFn, SomeDisallowedInUnsafeFn};
262+
use UsedUnsafeBlockData::*;
263263

264264
let update_entry = |this: &mut Self, hir_id, new_usage| {
265265
match this.used_unsafe_blocks.entry(hir_id) {
@@ -299,15 +299,11 @@ impl<'tcx> UnsafetyChecker<'_, 'tcx> {
299299
}
300300
}),
301301
Safety::BuiltinUnsafe => {}
302-
Safety::ExplicitUnsafe(hir_id) => violations.into_iter().for_each(|violation| {
302+
Safety::ExplicitUnsafe(hir_id) => violations.into_iter().for_each(|_violation| {
303303
update_entry(
304304
self,
305305
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,
311307
)
312308
}),
313309
};
@@ -522,6 +518,11 @@ fn unsafety_check_result<'tcx>(
522518
}
523519

524520
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+
}
525526
let span = tcx.sess.source_map().guess_head_span(tcx.hir().span(id));
526527
tcx.struct_span_lint_hir(UNUSED_UNSAFE, id, span, |lint| {
527528
let msg = "unnecessary `unsafe` block";
@@ -535,25 +536,7 @@ fn report_unused_unsafe(tcx: TyCtxt<'_>, kind: UnusedUnsafe, id: HirId) {
535536
"because it's nested under this `unsafe` block",
536537
);
537538
}
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!(),
557540
}
558541

559542
db.emit();

src/test/ui/span/lint-unused-unsafe-thir.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ fn bad1() { unsafe {} } //~ ERROR: unnecessary `unsafe` block
2222
fn bad2() { unsafe { bad1() } } //~ ERROR: unnecessary `unsafe` block
2323
unsafe fn bad3() { unsafe {} } //~ ERROR: unnecessary `unsafe` block
2424
fn bad4() { unsafe { callback(||{}) } } //~ ERROR: unnecessary `unsafe` block
25-
unsafe fn bad5() { unsafe { unsf() } } //~ ERROR: unnecessary `unsafe` block
25+
unsafe fn bad5() { unsafe { unsf() } }
2626
fn bad6() {
2727
unsafe { // don't put the warning here
2828
unsafe { //~ ERROR: unnecessary `unsafe` block
@@ -31,7 +31,7 @@ fn bad6() {
3131
}
3232
}
3333
unsafe fn bad7() {
34-
unsafe { //~ ERROR: unnecessary `unsafe` block
34+
unsafe {
3535
unsafe { //~ ERROR: unnecessary `unsafe` block
3636
unsf()
3737
}

src/test/ui/span/lint-unused-unsafe-thir.stderr

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,6 @@ error: unnecessary `unsafe` block
3030
LL | fn bad4() { unsafe { callback(||{}) } }
3131
| ^^^^^^ unnecessary `unsafe` block
3232

33-
error: unnecessary `unsafe` block
34-
--> $DIR/lint-unused-unsafe-thir.rs:25:20
35-
|
36-
LL | unsafe fn bad5() { unsafe { unsf() } }
37-
| ---------------- ^^^^^^ unnecessary `unsafe` block
38-
| |
39-
| because it's nested under this `unsafe` fn
40-
4133
error: unnecessary `unsafe` block
4234
--> $DIR/lint-unused-unsafe-thir.rs:28:9
4335
|
@@ -54,13 +46,5 @@ LL | unsafe {
5446
LL | unsafe {
5547
| ^^^^^^ unnecessary `unsafe` block
5648

57-
error: unnecessary `unsafe` block
58-
--> $DIR/lint-unused-unsafe-thir.rs:34:5
59-
|
60-
LL | unsafe fn bad7() {
61-
| ---------------- because it's nested under this `unsafe` fn
62-
LL | unsafe {
63-
| ^^^^^^ unnecessary `unsafe` block
64-
65-
error: aborting due to 8 previous errors
49+
error: aborting due to 6 previous errors
6650

0 commit comments

Comments
 (0)