Skip to content

Commit eaff0fc

Browse files
committed
Add a hint that the expressions produce a value
1 parent f3f8e75 commit eaff0fc

File tree

6 files changed

+125
-30
lines changed

6 files changed

+125
-30
lines changed

compiler/rustc_lint/src/unused.rs

+12-1
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,18 @@ 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.note(&format!("the {} produces a value", must_use_op));
166+
if let Ok(snippet) = cx.sess().source_map().span_to_snippet(expr.span) {
167+
lint.span_suggestion(
168+
expr.span,
169+
"use `let _ = ...` to ignore it",
170+
format!("let _ = {}", snippet),
171+
Applicability::MachineApplicable,
172+
)
173+
.emit()
174+
}
175+
lint.emit();
165176
});
166177
op_warned = true;
167178
}

src/test/ui/lint/fn_must_use.stderr

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

5254
warning: unused comparison that must be used
5355
--> $DIR/fn_must_use.rs:75:5
5456
|
5557
LL | m == n;
56-
| ^^^^^^
58+
| ^^^^^^ help: use `let _ = ...` to ignore it: `let _ = m == n`
59+
|
60+
= note: the comparison produces a value
5761

5862
warning: 8 warnings emitted
5963

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

+62-21
Original file line numberDiff line numberDiff line change
@@ -2,133 +2,174 @@ warning: unused comparison that must be used
22
--> $DIR/must-use-ops.rs:12:5
33
|
44
LL | val == 1;
5-
| ^^^^^^^^
5+
| ^^^^^^^^ help: use `let _ = ...` to ignore it: `let _ = val == 1`
66
|
77
note: the lint level is defined here
88
--> $DIR/must-use-ops.rs:5:9
99
|
1010
LL | #![warn(unused_must_use)]
1111
| ^^^^^^^^^^^^^^^
12+
= note: the comparison produces a value
1213

1314
warning: unused comparison that must be used
1415
--> $DIR/must-use-ops.rs:13:5
1516
|
1617
LL | val < 1;
17-
| ^^^^^^^
18+
| ^^^^^^^ help: use `let _ = ...` to ignore it: `let _ = val < 1`
19+
|
20+
= note: the comparison produces a value
1821

1922
warning: unused comparison that must be used
2023
--> $DIR/must-use-ops.rs:14:5
2124
|
2225
LL | val <= 1;
23-
| ^^^^^^^^
26+
| ^^^^^^^^ help: use `let _ = ...` to ignore it: `let _ = val <= 1`
27+
|
28+
= note: the comparison produces a value
2429

2530
warning: unused comparison that must be used
2631
--> $DIR/must-use-ops.rs:15:5
2732
|
2833
LL | val != 1;
29-
| ^^^^^^^^
34+
| ^^^^^^^^ help: use `let _ = ...` to ignore it: `let _ = val != 1`
35+
|
36+
= note: the comparison produces a value
3037

3138
warning: unused comparison that must be used
3239
--> $DIR/must-use-ops.rs:16:5
3340
|
3441
LL | val >= 1;
35-
| ^^^^^^^^
42+
| ^^^^^^^^ help: use `let _ = ...` to ignore it: `let _ = val >= 1`
43+
|
44+
= note: the comparison produces a value
3645

3746
warning: unused comparison that must be used
3847
--> $DIR/must-use-ops.rs:17:5
3948
|
4049
LL | val > 1;
41-
| ^^^^^^^
50+
| ^^^^^^^ help: use `let _ = ...` to ignore it: `let _ = val > 1`
51+
|
52+
= note: the comparison produces a value
4253

4354
warning: unused arithmetic operation that must be used
4455
--> $DIR/must-use-ops.rs:20:5
4556
|
4657
LL | val + 2;
47-
| ^^^^^^^
58+
| ^^^^^^^ help: use `let _ = ...` to ignore it: `let _ = val + 2`
59+
|
60+
= note: the arithmetic operation produces a value
4861

4962
warning: unused arithmetic operation that must be used
5063
--> $DIR/must-use-ops.rs:21:5
5164
|
5265
LL | val - 2;
53-
| ^^^^^^^
66+
| ^^^^^^^ help: use `let _ = ...` to ignore it: `let _ = val - 2`
67+
|
68+
= note: the arithmetic operation produces a value
5469

5570
warning: unused arithmetic operation that must be used
5671
--> $DIR/must-use-ops.rs:22:5
5772
|
5873
LL | val / 2;
59-
| ^^^^^^^
74+
| ^^^^^^^ help: use `let _ = ...` to ignore it: `let _ = val / 2`
75+
|
76+
= note: the arithmetic operation produces a value
6077

6178
warning: unused arithmetic operation that must be used
6279
--> $DIR/must-use-ops.rs:23:5
6380
|
6481
LL | val * 2;
65-
| ^^^^^^^
82+
| ^^^^^^^ help: use `let _ = ...` to ignore it: `let _ = val * 2`
83+
|
84+
= note: the arithmetic operation produces a value
6685

6786
warning: unused arithmetic operation that must be used
6887
--> $DIR/must-use-ops.rs:24:5
6988
|
7089
LL | val % 2;
71-
| ^^^^^^^
90+
| ^^^^^^^ help: use `let _ = ...` to ignore it: `let _ = val % 2`
91+
|
92+
= note: the arithmetic operation produces a value
7293

7394
warning: unused logical operation that must be used
7495
--> $DIR/must-use-ops.rs:27:5
7596
|
7697
LL | true && true;
77-
| ^^^^^^^^^^^^
98+
| ^^^^^^^^^^^^ help: use `let _ = ...` to ignore it: `let _ = true && true`
99+
|
100+
= note: the logical operation produces a value
78101

79102
warning: unused logical operation that must be used
80103
--> $DIR/must-use-ops.rs:28:5
81104
|
82105
LL | false || true;
83-
| ^^^^^^^^^^^^^
106+
| ^^^^^^^^^^^^^ help: use `let _ = ...` to ignore it: `let _ = false || true`
107+
|
108+
= note: the logical operation produces a value
84109

85110
warning: unused bitwise operation that must be used
86111
--> $DIR/must-use-ops.rs:31:5
87112
|
88113
LL | 5 ^ val;
89-
| ^^^^^^^
114+
| ^^^^^^^ help: use `let _ = ...` to ignore it: `let _ = 5 ^ val`
115+
|
116+
= note: the bitwise operation produces a value
90117

91118
warning: unused bitwise operation that must be used
92119
--> $DIR/must-use-ops.rs:32:5
93120
|
94121
LL | 5 & val;
95-
| ^^^^^^^
122+
| ^^^^^^^ help: use `let _ = ...` to ignore it: `let _ = 5 & val`
123+
|
124+
= note: the bitwise operation produces a value
96125

97126
warning: unused bitwise operation that must be used
98127
--> $DIR/must-use-ops.rs:33:5
99128
|
100129
LL | 5 | val;
101-
| ^^^^^^^
130+
| ^^^^^^^ help: use `let _ = ...` to ignore it: `let _ = 5 | val`
131+
|
132+
= note: the bitwise operation produces a value
102133

103134
warning: unused bitwise operation that must be used
104135
--> $DIR/must-use-ops.rs:34:5
105136
|
106137
LL | 5 << val;
107-
| ^^^^^^^^
138+
| ^^^^^^^^ help: use `let _ = ...` to ignore it: `let _ = 5 << val`
139+
|
140+
= note: the bitwise operation produces a value
108141

109142
warning: unused bitwise operation that must be used
110143
--> $DIR/must-use-ops.rs:35:5
111144
|
112145
LL | 5 >> val;
113-
| ^^^^^^^^
146+
| ^^^^^^^^ help: use `let _ = ...` to ignore it: `let _ = 5 >> val`
147+
|
148+
= note: the bitwise operation produces a value
114149

115150
warning: unused unary operation that must be used
116151
--> $DIR/must-use-ops.rs:38:5
117152
|
118153
LL | !val;
119-
| ^^^^
154+
| ^^^^ help: use `let _ = ...` to ignore it: `let _ = !val`
155+
|
156+
= note: the unary operation produces a value
120157

121158
warning: unused unary operation that must be used
122159
--> $DIR/must-use-ops.rs:39:5
123160
|
124161
LL | -val;
125-
| ^^^^
162+
| ^^^^ help: use `let _ = ...` to ignore it: `let _ = -val`
163+
|
164+
= note: the unary operation produces a value
126165

127166
warning: unused unary operation that must be used
128167
--> $DIR/must-use-ops.rs:40:5
129168
|
130169
LL | *val_pointer;
131-
| ^^^^^^^^^^^^
170+
| ^^^^^^^^^^^^ help: use `let _ = ...` to ignore it: `let _ = *val_pointer`
171+
|
172+
= note: the unary operation produces a value
132173

133174
warning: 21 warnings emitted
134175

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

+17-6
Original file line numberDiff line numberDiff line change
@@ -2,43 +2,54 @@ error: unused borrow that must be used
22
--> $DIR/unused-borrows.rs:6:5
33
|
44
LL | &42;
5-
| ^^^
5+
| ^^^ help: use `let _ = ...` to ignore it: `let _ = &42`
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+
= note: the borrow produces a value
1213

1314
error: unused borrow that must be used
1415
--> $DIR/unused-borrows.rs:9:5
1516
|
1617
LL | &mut foo(42);
17-
| ^^^^^^^^^^^^
18+
| ^^^^^^^^^^^^ help: use `let _ = ...` to ignore it: `let _ = &mut foo(42)`
19+
|
20+
= note: the borrow produces a value
1821

1922
error: unused borrow that must be used
2023
--> $DIR/unused-borrows.rs:12:5
2124
|
2225
LL | &&42;
23-
| ^^^^
26+
| ^^^^ help: use `let _ = ...` to ignore it: `let _ = &&42`
27+
|
28+
= note: the borrow produces a value
2429

2530
error: unused borrow that must be used
2631
--> $DIR/unused-borrows.rs:15:5
2732
|
2833
LL | &&mut 42;
29-
| ^^^^^^^^
34+
| ^^^^^^^^ help: use `let _ = ...` to ignore it: `let _ = &&mut 42`
35+
|
36+
= note: the borrow produces a value
3037

3138
error: unused borrow that must be used
3239
--> $DIR/unused-borrows.rs:18:5
3340
|
3441
LL | &mut &42;
35-
| ^^^^^^^^
42+
| ^^^^^^^^ help: use `let _ = ...` to ignore it: `let _ = &mut &42`
43+
|
44+
= note: the borrow produces a value
3645

3746
error: unused borrow that must be used
3847
--> $DIR/unused-borrows.rs:23:5
3948
|
4049
LL | && foo(42);
41-
| ^^^^^^^^^^
50+
| ^^^^^^^^^^ help: use `let _ = ...` to ignore it: `let _ = && foo(42)`
51+
|
52+
= note: the borrow produces a value
4253

4354
error: aborting due to 6 previous errors
4455

src/test/ui/unused/issue-85913.rs

+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() {}

src/test/ui/unused/issue-85913.stderr

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
error: unused logical operation that must be used
2+
--> $DIR/issue-85913.rs:4:5
3+
|
4+
LL | function() && return 1;
5+
| ^^^^^^^^^^^^^^^^^^^^^^ help: use `let _ = ...` to ignore it: `let _ = function() && return 1`
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+
= note: the logical operation produces a value
13+
14+
error: aborting due to previous error
15+

0 commit comments

Comments
 (0)