Skip to content

Commit 337181e

Browse files
committed
Auto merge of #87607 - JohnTitor:help-to-unused-must-use-op, r=estebank
Add a hint that the expressions produce a value Fixes #85913 The second commit is semi-_unrelated_ but it allows us to run the related tests just on `src/test/ui/lint`.
2 parents 7069a8c + 924eddf commit 337181e

File tree

84 files changed

+325
-143
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

84 files changed

+325
-143
lines changed

compiler/rustc_lint/src/unused.rs

+9-1
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,15 @@ impl<'tcx> LateLintPass<'tcx> for UnusedResults {
161161

162162
if let Some(must_use_op) = must_use_op {
163163
cx.struct_span_lint(UNUSED_MUST_USE, expr.span, |lint| {
164-
lint.build(&format!("unused {} that must be used", must_use_op)).emit()
164+
let mut lint = lint.build(&format!("unused {} that must be used", must_use_op));
165+
lint.span_label(expr.span, &format!("the {} produces a value", must_use_op));
166+
lint.span_suggestion_verbose(
167+
expr.span.shrink_to_lo(),
168+
"use `let _ = ...` to ignore the resulting value",
169+
"let _ = ".to_string(),
170+
Applicability::MachineApplicable,
171+
);
172+
lint.emit();
165173
});
166174
op_warned = true;
167175
}

src/test/ui/lint/fn_must_use.stderr

+12-2
Original file line numberDiff line numberDiff line change
@@ -47,13 +47,23 @@ warning: unused comparison that must be used
4747
--> $DIR/fn_must_use.rs:74:5
4848
|
4949
LL | 2 == 3;
50-
| ^^^^^^
50+
| ^^^^^^ the comparison produces a value
51+
|
52+
help: use `let _ = ...` to ignore the resulting value
53+
|
54+
LL | let _ = 2 == 3;
55+
| ^^^^^^^
5156

5257
warning: unused comparison that must be used
5358
--> $DIR/fn_must_use.rs:75:5
5459
|
5560
LL | m == n;
56-
| ^^^^^^
61+
| ^^^^^^ the comparison produces a value
62+
|
63+
help: use `let _ = ...` to ignore the resulting value
64+
|
65+
LL | let _ = m == n;
66+
| ^^^^^^^
5767

5868
warning: 8 warnings emitted
5969

src/test/ui/lint/must-use-ops.stderr

-134
This file was deleted.

src/test/ui/lint/unused-borrows.stderr

+35-6
Original file line numberDiff line numberDiff line change
@@ -2,43 +2,72 @@ error: unused borrow that must be used
22
--> $DIR/unused-borrows.rs:6:5
33
|
44
LL | &42;
5-
| ^^^
5+
| ^^^ the borrow produces a value
66
|
77
note: the lint level is defined here
88
--> $DIR/unused-borrows.rs:1:9
99
|
1010
LL | #![deny(unused_must_use)]
1111
| ^^^^^^^^^^^^^^^
12+
help: use `let _ = ...` to ignore the resulting value
13+
|
14+
LL | let _ = &42;
15+
| ^^^^^^^
1216

1317
error: unused borrow that must be used
1418
--> $DIR/unused-borrows.rs:9:5
1519
|
1620
LL | &mut foo(42);
17-
| ^^^^^^^^^^^^
21+
| ^^^^^^^^^^^^ the borrow produces a value
22+
|
23+
help: use `let _ = ...` to ignore the resulting value
24+
|
25+
LL | let _ = &mut foo(42);
26+
| ^^^^^^^
1827

1928
error: unused borrow that must be used
2029
--> $DIR/unused-borrows.rs:12:5
2130
|
2231
LL | &&42;
23-
| ^^^^
32+
| ^^^^ the borrow produces a value
33+
|
34+
help: use `let _ = ...` to ignore the resulting value
35+
|
36+
LL | let _ = &&42;
37+
| ^^^^^^^
2438

2539
error: unused borrow that must be used
2640
--> $DIR/unused-borrows.rs:15:5
2741
|
2842
LL | &&mut 42;
29-
| ^^^^^^^^
43+
| ^^^^^^^^ the borrow produces a value
44+
|
45+
help: use `let _ = ...` to ignore the resulting value
46+
|
47+
LL | let _ = &&mut 42;
48+
| ^^^^^^^
3049

3150
error: unused borrow that must be used
3251
--> $DIR/unused-borrows.rs:18:5
3352
|
3453
LL | &mut &42;
35-
| ^^^^^^^^
54+
| ^^^^^^^^ the borrow produces a value
55+
|
56+
help: use `let _ = ...` to ignore the resulting value
57+
|
58+
LL | let _ = &mut &42;
59+
| ^^^^^^^
3660

3761
error: unused borrow that must be used
3862
--> $DIR/unused-borrows.rs:23:5
3963
|
4064
LL | && foo(42);
41-
| ^^^^^^^^^^
65+
| ^^^^^^^^^^ the borrow produces a value
66+
|
67+
help: use `let _ = ...` to ignore the resulting value
68+
|
69+
LL | let _ = && foo(42);
70+
| ^^^^^^^
4271

4372
error: aborting due to 6 previous errors
4473

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#![deny(unused_must_use)]
2+
3+
pub fn fun() -> i32 {
4+
function() && return 1;
5+
//~^ ERROR: unused logical operation that must be used
6+
return 0;
7+
}
8+
9+
fn function() -> bool {
10+
true
11+
}
12+
13+
fn main() {}
+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
error: unused logical operation that must be used
2+
--> $DIR/issue-85913.rs:4:5
3+
|
4+
LL | function() && return 1;
5+
| ^^^^^^^^^^^^^^^^^^^^^^ the logical operation produces a value
6+
|
7+
note: the lint level is defined here
8+
--> $DIR/issue-85913.rs:1:9
9+
|
10+
LL | #![deny(unused_must_use)]
11+
| ^^^^^^^^^^^^^^^
12+
help: use `let _ = ...` to ignore the resulting value
13+
|
14+
LL | let _ = function() && return 1;
15+
| ^^^^^^^
16+
17+
error: aborting due to previous error
18+

0 commit comments

Comments
 (0)