Skip to content

Commit 67f68a1

Browse files
authored
Rollup merge of rust-lang#115587 - mojave2:issue-115348, r=oli-obk
fix rust-lang#115348 fix rust-lang#115348 It looks that: - In `rustc_mir_build::build`, the body of function will not be built, when the `tcx.check_match(def)` fails due to `non-exhaustive patterns` - In `rustc_mir_transform::check_unsafety`, the `UnsafetyChecker` collects all `used_unsafe_blocks` in the MIR of a function, and the `UnusedUnsafeVisitor` will visit all `UnsafeBlock`s in the HIR and collect `unused_unsafes`, which are not contained in `used_unsafe_blocks`, and report `unnecessary_unsafe`s - So the unsafe block in the issue example code will be reported as `unnecessary_unsafe`.
2 parents 5957f35 + cfa2119 commit 67f68a1

File tree

3 files changed

+38
-1
lines changed

3 files changed

+38
-1
lines changed

Diff for: compiler/rustc_mir_transform/src/check_unsafety.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -483,7 +483,7 @@ fn unsafety_check_result(tcx: TyCtxt<'_>, def: LocalDefId) -> &UnsafetyCheckResu
483483
// `mir_built` force this.
484484
let body = &tcx.mir_built(def).borrow();
485485

486-
if body.is_custom_mir() {
486+
if body.is_custom_mir() || body.tainted_by_errors.is_some() {
487487
return tcx.arena.alloc(UnsafetyCheckResult {
488488
violations: Vec::new(),
489489
used_unsafe_blocks: Default::default(),
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// Regression test for #115348.
2+
3+
unsafe fn uwu() {}
4+
5+
// Tests that the false-positive warning "unnecessary `unsafe` block"
6+
// should not be reported, when the error "non-exhaustive patterns"
7+
// appears.
8+
9+
fn foo(x: Option<u32>) {
10+
match x {
11+
//~^ ERROR non-exhaustive patterns: `None` not covered
12+
Some(_) => unsafe { uwu() },
13+
}
14+
}
15+
16+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
error[E0004]: non-exhaustive patterns: `None` not covered
2+
--> $DIR/issue-115348-false-positive-warning-of-unnecessary-unsafe.rs:10:11
3+
|
4+
LL | match x {
5+
| ^ pattern `None` not covered
6+
|
7+
note: `Option<u32>` defined here
8+
--> $SRC_DIR/core/src/option.rs:LL:COL
9+
::: $SRC_DIR/core/src/option.rs:LL:COL
10+
|
11+
= note: not covered
12+
= note: the matched value is of type `Option<u32>`
13+
help: ensure that all possible cases are being handled by adding a match arm with a wildcard pattern or an explicit pattern as shown
14+
|
15+
LL ~ Some(_) => unsafe { uwu() },
16+
LL ~ None => todo!(),
17+
|
18+
19+
error: aborting due to previous error
20+
21+
For more information about this error, try `rustc --explain E0004`.

0 commit comments

Comments
 (0)