Skip to content

Commit 621c007

Browse files
committed
don't emit unused_results lint for tuples of trivial types
1 parent d351448 commit 621c007

File tree

3 files changed

+22
-33
lines changed

3 files changed

+22
-33
lines changed

compiler/rustc_lint/src/unused/must_use.rs

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -108,13 +108,6 @@ impl IsTyMustUse {
108108
_ => self,
109109
}
110110
}
111-
112-
fn yes(self) -> Option<MustUsePath> {
113-
match self {
114-
Self::Yes(must_use_path) => Some(must_use_path),
115-
_ => None,
116-
}
117-
}
118111
}
119112

120113
/// A path through a type to a `must_use` source. Contains useful info for the lint.
@@ -254,16 +247,23 @@ pub fn is_ty_must_use<'tcx>(
254247
// Default to `expr`.
255248
let elem_exprs = elem_exprs.iter().chain(iter::repeat(expr));
256249

257-
let nested_must_use = tys
258-
.iter()
259-
.zip(elem_exprs)
260-
.enumerate()
261-
.filter_map(|(i, (ty, expr))| {
262-
is_ty_must_use(cx, ty, expr, simplify_uninhabited).yes().map(|path| (i, path))
263-
})
264-
.collect::<Vec<_>>();
250+
let mut nested_must_use = Vec::new();
251+
let all_trivial = tys.iter().zip(elem_exprs).enumerate().all(|(i, (ty, expr))| {
252+
let must_use = is_ty_must_use(cx, ty, expr, simplify_uninhabited);
253+
254+
let trivial = matches!(must_use, IsTyMustUse::Trivial);
255+
if let IsTyMustUse::Yes(path) = must_use {
256+
nested_must_use.push((i, path));
257+
}
258+
259+
trivial
260+
});
265261

266-
if !nested_must_use.is_empty() {
262+
if all_trivial {
263+
// If all tuple elements are trivial, mark the whole tuple as such.
264+
// i.e. don't emit `unused_results` for types such as `((), ())`
265+
IsTyMustUse::Trivial
266+
} else if !nested_must_use.is_empty() {
267267
IsTyMustUse::Yes(MustUsePath::TupleElement(nested_must_use))
268268
} else {
269269
IsTyMustUse::No

tests/ui/lint/unused/unused-result.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,11 +44,12 @@ fn main() {
4444
let _ = foo::<MustUse>();
4545
let _ = foo::<MustUseMsg>();
4646

47+
// "trivial" types
4748
();
48-
((), ()); //~ ERROR: unused result of type
49+
((), ());
4950
Ok::<(), Nothing>(());
5051
ControlFlow::<Nothing>::Continue(());
51-
((), Ok::<(), Nothing>(()), ((((), ())), ((),))); //~ ERROR: unused result of type
52+
((), Ok::<(), Nothing>(()), ((((), ())), ((),)));
5253
foo::<Nothing>();
5354

5455
((), 1); //~ ERROR: unused result of type `((), i32)`

tests/ui/lint/unused/unused-result.stderr

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -61,29 +61,17 @@ help: use `let _ = ...` to ignore the resulting value
6161
LL | let _ = foo::<MustUseMsg>();
6262
| +++++++
6363

64-
error: unused result of type `((), ())`
65-
--> $DIR/unused-result.rs:48:5
66-
|
67-
LL | ((), ());
68-
| ^^^^^^^^^
69-
70-
error: unused result of type `((), Result<(), Nothing>, (((), ()), ((),)))`
71-
--> $DIR/unused-result.rs:51:5
72-
|
73-
LL | ((), Ok::<(), Nothing>(()), ((((), ())), ((),)));
74-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
75-
7664
error: unused result of type `((), i32)`
77-
--> $DIR/unused-result.rs:54:5
65+
--> $DIR/unused-result.rs:55:5
7866
|
7967
LL | ((), 1);
8068
| ^^^^^^^^
8169

8270
error: unused result of type `(i32, ())`
83-
--> $DIR/unused-result.rs:55:5
71+
--> $DIR/unused-result.rs:56:5
8472
|
8573
LL | (1, ());
8674
| ^^^^^^^^
8775

88-
error: aborting due to 9 previous errors
76+
error: aborting due to 7 previous errors
8977

0 commit comments

Comments
 (0)