Skip to content

Commit 07127ce

Browse files
Rollup merge of #112529 - jieyouxu:block-expr-unused-must-use, r=oli-obk
Extend `unused_must_use` to cover block exprs Given code like ```rust #[must_use] fn foo() -> i32 { 42 } fn warns() { { foo(); } } fn does_not_warn() { { foo() }; } fn main() { warns(); does_not_warn(); } ``` ### Before This PR ``` warning: unused return value of `foo` that must be used --> test.rs:8:9 | 8 | foo(); | ^^^^^ | = note: `#[warn(unused_must_use)]` on by default help: use `let _ = ...` to ignore the resulting value | 8 | let _ = foo(); | +++++++ warning: 1 warning emitted ``` ### After This PR ``` warning: unused return value of `foo` that must be used --> test.rs:8:9 | 8 | foo(); | ^^^^^ | = note: `#[warn(unused_must_use)]` on by default help: use `let _ = ...` to ignore the resulting value | 8 | let _ = foo(); | +++++++ warning: unused return value of `foo` that must be used --> test.rs:14:9 | 14 | foo() | ^^^^^ | help: use `let _ = ...` to ignore the resulting value | 14 | let _ = foo(); | +++++++ + warning: 2 warnings emitted ``` Fixes #104253.
2 parents 3b3acc2 + 12b5557 commit 07127ce

File tree

6 files changed

+9
-9
lines changed

6 files changed

+9
-9
lines changed

tests/fail/dangling_pointers/storage_dead_dangling.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ fn fill(v: &mut i32) {
1010
}
1111

1212
fn evil() {
13-
unsafe { &mut *(LEAK as *mut i32) }; //~ ERROR: is a dangling pointer
13+
let _ = unsafe { &mut *(LEAK as *mut i32) }; //~ ERROR: is a dangling pointer
1414
}
1515

1616
fn main() {

tests/fail/dangling_pointers/storage_dead_dangling.stderr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
error: Undefined Behavior: dereferencing pointer failed: $HEX[noalloc] is a dangling pointer (it has no provenance)
22
--> $DIR/storage_dead_dangling.rs:LL:CC
33
|
4-
LL | unsafe { &mut *(LEAK as *mut i32) };
5-
| ^^^^^^^^^^^^^^^^^^^^^^^^ dereferencing pointer failed: $HEX[noalloc] is a dangling pointer (it has no provenance)
4+
LL | let _ = unsafe { &mut *(LEAK as *mut i32) };
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^ dereferencing pointer failed: $HEX[noalloc] is a dangling pointer (it has no provenance)
66
|
77
= help: this indicates a bug in the program: it performed an invalid operation, and caused Undefined Behavior
88
= help: see https://doc.rust-lang.org/nightly/reference/behavior-considered-undefined.html for further information

tests/fail/intrinsics/uninit_uninhabited_type.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@
22

33
#[allow(deprecated, invalid_value)]
44
fn main() {
5-
unsafe { std::mem::uninitialized::<!>() };
5+
let _ = unsafe { std::mem::uninitialized::<!>() };
66
//~^ ERROR: attempted to instantiate uninhabited type `!`
77
}

tests/fail/intrinsics/uninit_uninhabited_type.stderr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
error: abnormal termination: aborted execution: attempted to instantiate uninhabited type `!`
22
--> $DIR/uninit_uninhabited_type.rs:LL:CC
33
|
4-
LL | unsafe { std::mem::uninitialized::<!>() };
5-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ aborted execution: attempted to instantiate uninhabited type `!`
4+
LL | let _ = unsafe { std::mem::uninitialized::<!>() };
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ aborted execution: attempted to instantiate uninhabited type `!`
66
|
77
= note: inside `main` at $DIR/uninit_uninhabited_type.rs:LL:CC
88

tests/fail/intrinsics/zero_fn_ptr.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#[allow(deprecated, invalid_value)]
22
fn main() {
3-
unsafe { std::mem::zeroed::<fn()>() };
3+
let _ = unsafe { std::mem::zeroed::<fn()>() };
44
//~^ ERROR: attempted to zero-initialize type `fn()`, which is invalid
55
}

tests/fail/intrinsics/zero_fn_ptr.stderr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
error: abnormal termination: aborted execution: attempted to zero-initialize type `fn()`, which is invalid
22
--> $DIR/zero_fn_ptr.rs:LL:CC
33
|
4-
LL | unsafe { std::mem::zeroed::<fn()>() };
5-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ aborted execution: attempted to zero-initialize type `fn()`, which is invalid
4+
LL | let _ = unsafe { std::mem::zeroed::<fn()>() };
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ aborted execution: attempted to zero-initialize type `fn()`, which is invalid
66
|
77
= note: inside `main` at $DIR/zero_fn_ptr.rs:LL:CC
88

0 commit comments

Comments
 (0)