Skip to content

Commit 4230f96

Browse files
authored
Rollup merge of #74869 - tmiasko:must-use-closures, r=ecstatic-morse
Make closures and generators a must use types Warn about unused expressions with closure or generator type. This follows existing precedence of must use annotations present on `FnOnce`, `FnMut`, `Fn` traits, which already indirectly apply to closures in some cases, e.g.,: ```rust fn f() -> impl FnOnce() { || {} } fn main() { // an existing warning: unused implementer of `std::ops::FnOnce` that must be used: f(); // a new warning: unused closure that must be used: || {}; } ``` Closes #74691.
2 parents c07998e + 821d50a commit 4230f96

36 files changed

+355
-31
lines changed

src/librustc_lint/unused.rs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,28 @@ impl<'tcx> LateLintPass<'tcx> for UnusedResults {
203203
// Otherwise, we don't lint, to avoid false positives.
204204
_ => false,
205205
},
206+
ty::Closure(..) => {
207+
cx.struct_span_lint(UNUSED_MUST_USE, span, |lint| {
208+
let mut err = lint.build(&format!(
209+
"unused {}closure{}{} that must be used",
210+
descr_pre, plural_suffix, descr_post,
211+
));
212+
err.note("closures are lazy and do nothing unless called");
213+
err.emit();
214+
});
215+
true
216+
}
217+
ty::Generator(..) => {
218+
cx.struct_span_lint(UNUSED_MUST_USE, span, |lint| {
219+
let mut err = lint.build(&format!(
220+
"unused {}generator{}{} that must be used",
221+
descr_pre, plural_suffix, descr_post,
222+
));
223+
err.note("generators are lazy and do nothing unless resumed");
224+
err.emit();
225+
});
226+
true
227+
}
206228
_ => false,
207229
}
208230
}

src/test/ui/generator/issue-52398.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,14 @@ impl A {
1414
fn main() {
1515
// Test that the MIR local with type &A created for the auto-borrow adjustment
1616
// is caught by typeck
17-
move || {
17+
move || { //~ WARN unused generator that must be used
1818
A.test(yield);
1919
};
2020

2121
// Test that the std::cell::Ref temporary returned from the `borrow` call
2222
// is caught by typeck
2323
let y = RefCell::new(true);
24-
static move || {
24+
static move || { //~ WARN unused generator that must be used
2525
yield *y.borrow();
2626
return "Done";
2727
};
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
warning: unused generator that must be used
2+
--> $DIR/issue-52398.rs:17:5
3+
|
4+
LL | / move || {
5+
LL | | A.test(yield);
6+
LL | | };
7+
| |______^
8+
|
9+
= note: `#[warn(unused_must_use)]` on by default
10+
= note: generators are lazy and do nothing unless resumed
11+
12+
warning: unused generator that must be used
13+
--> $DIR/issue-52398.rs:24:5
14+
|
15+
LL | / static move || {
16+
LL | | yield *y.borrow();
17+
LL | | return "Done";
18+
LL | | };
19+
| |______^
20+
|
21+
= note: generators are lazy and do nothing unless resumed
22+
23+
warning: 2 warnings emitted
24+

src/test/ui/generator/issue-57084.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ where F: Fn() -> ()
1919

2020
fn main() {
2121
let data = &vec![1];
22-
|| {
22+
|| { //~ WARN unused generator that must be used
2323
let _to_pin = with(move || println!("{:p}", data));
2424
loop {
2525
yield
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
warning: unused generator that must be used
2+
--> $DIR/issue-57084.rs:22:5
3+
|
4+
LL | / || {
5+
LL | | let _to_pin = with(move || println!("{:p}", data));
6+
LL | | loop {
7+
LL | | yield
8+
LL | | }
9+
LL | | };
10+
| |______^
11+
|
12+
= note: `#[warn(unused_must_use)]` on by default
13+
= note: generators are lazy and do nothing unless resumed
14+
15+
warning: 1 warning emitted
16+

src/test/ui/generator/match-bindings.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ enum Enum {
99
}
1010

1111
fn main() {
12-
|| {
12+
|| { //~ WARN unused generator that must be used
1313
loop {
1414
if let true = true {
1515
match Enum::A(String::new()) {
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
warning: unused generator that must be used
2+
--> $DIR/match-bindings.rs:12:5
3+
|
4+
LL | / || {
5+
LL | | loop {
6+
LL | | if let true = true {
7+
LL | | match Enum::A(String::new()) {
8+
... |
9+
LL | | }
10+
LL | | };
11+
| |______^
12+
|
13+
= note: `#[warn(unused_must_use)]` on by default
14+
= note: generators are lazy and do nothing unless resumed
15+
16+
warning: 1 warning emitted
17+

src/test/ui/generator/reborrow-mut-upvar.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
#![feature(generators)]
44

55
fn _run(bar: &mut i32) {
6-
|| {
6+
|| { //~ WARN unused generator that must be used
77
{
88
let _baz = &*bar;
99
yield;
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
warning: unused generator that must be used
2+
--> $DIR/reborrow-mut-upvar.rs:6:5
3+
|
4+
LL | / || {
5+
LL | | {
6+
LL | | let _baz = &*bar;
7+
LL | | yield;
8+
... |
9+
LL | | *bar = 2;
10+
LL | | };
11+
| |______^
12+
|
13+
= note: `#[warn(unused_must_use)]` on by default
14+
= note: generators are lazy and do nothing unless resumed
15+
16+
warning: 1 warning emitted
17+

src/test/ui/generator/too-live-local-in-immovable-gen.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
fn main() {
77
unsafe {
8-
static move || {
8+
static move || { //~ WARN unused generator that must be used
99
// Tests that the generator transformation finds out that `a` is not live
1010
// during the yield expression. Type checking will also compute liveness
1111
// and it should also find out that `a` is not live.

0 commit comments

Comments
 (0)