Skip to content

Commit 23a0751

Browse files
committed
unused_must_use: Suppress warnings for ControlFlow<Uninhabited, ()> too
1 parent 049b0ba commit 23a0751

File tree

3 files changed

+45
-5
lines changed

3 files changed

+45
-5
lines changed

compiler/rustc_lint/src/unused.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -303,6 +303,18 @@ impl<'tcx> LateLintPass<'tcx> for UnusedResults {
303303
{
304304
Some(MustUsePath::Suppressed)
305305
}
306+
// Suppress warnings on `ControlFlow<Uninhabited, ()>` (e.g. `ControlFlow<!, ()>`).
307+
ty::Adt(def, args)
308+
if cx.tcx.is_diagnostic_item(sym::ControlFlow, def.did())
309+
&& args.type_at(1).is_unit()
310+
&& !args.type_at(0).is_inhabited_from(
311+
cx.tcx,
312+
parent_mod_did,
313+
cx.typing_env(),
314+
) =>
315+
{
316+
Some(MustUsePath::Suppressed)
317+
}
306318
ty::Adt(def, _) => is_def_must_use(cx, def.did(), span),
307319
ty::Alias(ty::Opaque | ty::Projection, ty::AliasTy { def_id: def, .. }) => {
308320
elaborate(cx.tcx, cx.tcx.explicit_item_self_bounds(def).iter_identity_copied())

tests/ui/lint/unused/must_use-result-unit-uninhabited.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#![deny(unused_must_use)]
55
#![feature(never_type)]
66

7+
use core::ops::{ControlFlow, ControlFlow::Continue};
78
use dep::{MyUninhabited, MyUninhabitedNonexhaustive};
89

910
fn f1() -> Result<(), ()> {
@@ -63,6 +64,18 @@ impl UsesAssocType for S2 {
6364
}
6465
}
6566

67+
fn c1() -> ControlFlow<()> {
68+
Continue(())
69+
}
70+
71+
fn c2() -> ControlFlow<core::convert::Infallible, ()> {
72+
Continue(())
73+
}
74+
75+
fn c3() -> ControlFlow<!> {
76+
Continue(())
77+
}
78+
6679
fn main() {
6780
f1(); //~ ERROR: unused `Result` that must be used
6881
f2();
@@ -73,4 +86,8 @@ fn main() {
7386
f6(S2); //~ ERROR: unused `Result` that must be used
7487
S1.method();
7588
S2.method(); //~ ERROR: unused `Result` that must be used
89+
90+
c1(); //~ ERROR: unused `ControlFlow` that must be used
91+
c2();
92+
c3();
7693
}

tests/ui/lint/unused/must_use-result-unit-uninhabited.stderr

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error: unused `Result` that must be used
2-
--> $DIR/must_use-result-unit-uninhabited.rs:67:5
2+
--> $DIR/must_use-result-unit-uninhabited.rs:80:5
33
|
44
LL | f1();
55
| ^^^^
@@ -16,7 +16,7 @@ LL | let _ = f1();
1616
| +++++++
1717

1818
error: unused `Result` that must be used
19-
--> $DIR/must_use-result-unit-uninhabited.rs:71:5
19+
--> $DIR/must_use-result-unit-uninhabited.rs:84:5
2020
|
2121
LL | f5();
2222
| ^^^^
@@ -28,7 +28,7 @@ LL | let _ = f5();
2828
| +++++++
2929

3030
error: unused `Result` that must be used
31-
--> $DIR/must_use-result-unit-uninhabited.rs:73:5
31+
--> $DIR/must_use-result-unit-uninhabited.rs:86:5
3232
|
3333
LL | f6(S2);
3434
| ^^^^^^
@@ -40,7 +40,7 @@ LL | let _ = f6(S2);
4040
| +++++++
4141

4242
error: unused `Result` that must be used
43-
--> $DIR/must_use-result-unit-uninhabited.rs:75:5
43+
--> $DIR/must_use-result-unit-uninhabited.rs:88:5
4444
|
4545
LL | S2.method();
4646
| ^^^^^^^^^^^
@@ -51,5 +51,16 @@ help: use `let _ = ...` to ignore the resulting value
5151
LL | let _ = S2.method();
5252
| +++++++
5353

54-
error: aborting due to 4 previous errors
54+
error: unused `ControlFlow` that must be used
55+
--> $DIR/must_use-result-unit-uninhabited.rs:90:5
56+
|
57+
LL | c1();
58+
| ^^^^
59+
|
60+
help: use `let _ = ...` to ignore the resulting value
61+
|
62+
LL | let _ = c1();
63+
| +++++++
64+
65+
error: aborting due to 5 previous errors
5566

0 commit comments

Comments
 (0)