Skip to content

Commit 1eb0cef

Browse files
committed
Auto merge of #50149 - aaronaaeng:master, r=estebank
Added warning for unused arithmetic expressions The compiler now displays a warning when a binary arithmetic operation is evaluated but not used. This resolves #50124 by following the instructions outlined in the issue. The changes are as follows: - Added new pattern matching for unused arithmetic expressions in `src/librustc_lint/unused.rs` - Added `#[must_use]` attributes to the binary operation methods in `src/libcore/internal_macros.rs` - Added `#[must_use]` attributes to the non-assigning binary operators in `src/libcore/ops/arith.rs`
2 parents ede7f94 + 91aa267 commit 1eb0cef

File tree

6 files changed

+224
-15
lines changed

6 files changed

+224
-15
lines changed

src/libcore/ops/arith.rs

+6
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ pub trait Add<RHS=Self> {
9494
type Output;
9595

9696
/// Performs the `+` operation.
97+
#[must_use]
9798
#[stable(feature = "rust1", since = "1.0.0")]
9899
fn add(self, rhs: RHS) -> Self::Output;
99100
}
@@ -191,6 +192,7 @@ pub trait Sub<RHS=Self> {
191192
type Output;
192193

193194
/// Performs the `-` operation.
195+
#[must_use]
194196
#[stable(feature = "rust1", since = "1.0.0")]
195197
fn sub(self, rhs: RHS) -> Self::Output;
196198
}
@@ -310,6 +312,7 @@ pub trait Mul<RHS=Self> {
310312
type Output;
311313

312314
/// Performs the `*` operation.
315+
#[must_use]
313316
#[stable(feature = "rust1", since = "1.0.0")]
314317
fn mul(self, rhs: RHS) -> Self::Output;
315318
}
@@ -433,6 +436,7 @@ pub trait Div<RHS=Self> {
433436
type Output;
434437

435438
/// Performs the `/` operation.
439+
#[must_use]
436440
#[stable(feature = "rust1", since = "1.0.0")]
437441
fn div(self, rhs: RHS) -> Self::Output;
438442
}
@@ -517,6 +521,7 @@ pub trait Rem<RHS=Self> {
517521
type Output = Self;
518522

519523
/// Performs the `%` operation.
524+
#[must_use]
520525
#[stable(feature = "rust1", since = "1.0.0")]
521526
fn rem(self, rhs: RHS) -> Self::Output;
522527
}
@@ -601,6 +606,7 @@ pub trait Neg {
601606
type Output;
602607

603608
/// Performs the unary `-` operation.
609+
#[must_use]
604610
#[stable(feature = "rust1", since = "1.0.0")]
605611
fn neg(self) -> Self::Output;
606612
}

src/libcore/ops/bit.rs

+6
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ pub trait Not {
4646
type Output;
4747

4848
/// Performs the unary `!` operation.
49+
#[must_use]
4950
#[stable(feature = "rust1", since = "1.0.0")]
5051
fn not(self) -> Self::Output;
5152
}
@@ -129,6 +130,7 @@ pub trait BitAnd<RHS=Self> {
129130
type Output;
130131

131132
/// Performs the `&` operation.
133+
#[must_use]
132134
#[stable(feature = "rust1", since = "1.0.0")]
133135
fn bitand(self, rhs: RHS) -> Self::Output;
134136
}
@@ -212,6 +214,7 @@ pub trait BitOr<RHS=Self> {
212214
type Output;
213215

214216
/// Performs the `|` operation.
217+
#[must_use]
215218
#[stable(feature = "rust1", since = "1.0.0")]
216219
fn bitor(self, rhs: RHS) -> Self::Output;
217220
}
@@ -298,6 +301,7 @@ pub trait BitXor<RHS=Self> {
298301
type Output;
299302

300303
/// Performs the `^` operation.
304+
#[must_use]
301305
#[stable(feature = "rust1", since = "1.0.0")]
302306
fn bitxor(self, rhs: RHS) -> Self::Output;
303307
}
@@ -385,6 +389,7 @@ pub trait Shl<RHS=Self> {
385389
type Output;
386390

387391
/// Performs the `<<` operation.
392+
#[must_use]
388393
#[stable(feature = "rust1", since = "1.0.0")]
389394
fn shl(self, rhs: RHS) -> Self::Output;
390395
}
@@ -493,6 +498,7 @@ pub trait Shr<RHS=Self> {
493498
type Output;
494499

495500
/// Performs the `>>` operation.
501+
#[must_use]
496502
#[stable(feature = "rust1", since = "1.0.0")]
497503
fn shr(self, rhs: RHS) -> Self::Output;
498504
}

src/libcore/ops/deref.rs

+1
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ pub trait Deref {
7777
type Target: ?Sized;
7878

7979
/// Dereferences the value.
80+
#[must_use]
8081
#[stable(feature = "rust1", since = "1.0.0")]
8182
fn deref(&self) -> &Self::Target;
8283
}

src/librustc_lint/unused.rs

+27-15
Original file line numberDiff line numberDiff line change
@@ -91,23 +91,35 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for UnusedResults {
9191
let def_id = def.def_id();
9292
fn_warned = check_must_use(cx, def_id, s.span, "return value of ");
9393
}
94-
95-
if let hir::ExprBinary(bin_op, ..) = expr.node {
96-
match bin_op.node {
97-
// Hardcoding the comparison operators here seemed more
98-
// expedient than the refactoring that would be needed to
99-
// look up the `#[must_use]` attribute which does exist on
100-
// the comparison trait methods
101-
hir::BiEq | hir::BiLt | hir::BiLe | hir::BiNe | hir::BiGe | hir::BiGt => {
102-
let msg = "unused comparison which must be used";
103-
cx.span_lint(UNUSED_MUST_USE, expr.span, msg);
104-
op_warned = true;
105-
},
106-
_ => {},
107-
}
94+
let must_use_op = match expr.node {
95+
// Hardcoding operators here seemed more expedient than the
96+
// refactoring that would be needed to look up the `#[must_use]`
97+
// attribute which does exist on the comparison trait methods
98+
hir::ExprBinary(bin_op, ..) => {
99+
match bin_op.node {
100+
hir::BiEq | hir::BiLt | hir::BiLe | hir::BiNe | hir::BiGe | hir::BiGt => {
101+
Some("comparison")
102+
},
103+
hir::BiAdd | hir::BiSub | hir::BiDiv | hir::BiMul | hir::BiRem => {
104+
Some("arithmetic operation")
105+
},
106+
hir::BiAnd | hir::BiOr => {
107+
Some("logical operation")
108+
},
109+
hir::BiBitXor | hir::BiBitAnd | hir::BiBitOr | hir::BiShl | hir::BiShr => {
110+
Some("bitwise operation")
111+
},
112+
}
113+
},
114+
hir::ExprUnary(..) => Some("unary operation"),
115+
_ => None
116+
};
117+
if let Some(must_use_op) = must_use_op {
118+
cx.span_lint(UNUSED_MUST_USE, expr.span,
119+
&format!("unused {} which must be used", must_use_op));
120+
op_warned = true;
108121
}
109122
}
110-
111123
if !(ty_warned || fn_warned || op_warned) {
112124
cx.span_lint(UNUSED_RESULTS, s.span, "unused result");
113125
}

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

+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
// Copyright 2018 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
// Issue #50124 - Test warning for unused operator expressions
12+
13+
// compile-pass
14+
15+
#![feature(fn_must_use)]
16+
#![warn(unused_must_use)]
17+
18+
fn main() {
19+
let val = 1;
20+
let val_pointer = &val;
21+
22+
// Comparison Operators
23+
val == 1;
24+
val < 1;
25+
val <= 1;
26+
val != 1;
27+
val >= 1;
28+
val > 1;
29+
30+
// Arithmetic Operators
31+
val + 2;
32+
val - 2;
33+
val / 2;
34+
val * 2;
35+
val % 2;
36+
37+
// Logical Operators
38+
true && true;
39+
false || true;
40+
41+
// Bitwise Operators
42+
5 ^ val;
43+
5 & val;
44+
5 | val;
45+
5 << val;
46+
5 >> val;
47+
48+
// Unary Operators
49+
!val;
50+
-val;
51+
*val_pointer;
52+
}

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

+132
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
warning: unused comparison which must be used
2+
--> $DIR/must-use-ops.rs:23:5
3+
|
4+
LL | val == 1;
5+
| ^^^^^^^^
6+
|
7+
note: lint level defined here
8+
--> $DIR/must-use-ops.rs:16:9
9+
|
10+
LL | #![warn(unused_must_use)]
11+
| ^^^^^^^^^^^^^^^
12+
13+
warning: unused comparison which must be used
14+
--> $DIR/must-use-ops.rs:24:5
15+
|
16+
LL | val < 1;
17+
| ^^^^^^^
18+
19+
warning: unused comparison which must be used
20+
--> $DIR/must-use-ops.rs:25:5
21+
|
22+
LL | val <= 1;
23+
| ^^^^^^^^
24+
25+
warning: unused comparison which must be used
26+
--> $DIR/must-use-ops.rs:26:5
27+
|
28+
LL | val != 1;
29+
| ^^^^^^^^
30+
31+
warning: unused comparison which must be used
32+
--> $DIR/must-use-ops.rs:27:5
33+
|
34+
LL | val >= 1;
35+
| ^^^^^^^^
36+
37+
warning: unused comparison which must be used
38+
--> $DIR/must-use-ops.rs:28:5
39+
|
40+
LL | val > 1;
41+
| ^^^^^^^
42+
43+
warning: unused arithmetic operation which must be used
44+
--> $DIR/must-use-ops.rs:31:5
45+
|
46+
LL | val + 2;
47+
| ^^^^^^^
48+
49+
warning: unused arithmetic operation which must be used
50+
--> $DIR/must-use-ops.rs:32:5
51+
|
52+
LL | val - 2;
53+
| ^^^^^^^
54+
55+
warning: unused arithmetic operation which must be used
56+
--> $DIR/must-use-ops.rs:33:5
57+
|
58+
LL | val / 2;
59+
| ^^^^^^^
60+
61+
warning: unused arithmetic operation which must be used
62+
--> $DIR/must-use-ops.rs:34:5
63+
|
64+
LL | val * 2;
65+
| ^^^^^^^
66+
67+
warning: unused arithmetic operation which must be used
68+
--> $DIR/must-use-ops.rs:35:5
69+
|
70+
LL | val % 2;
71+
| ^^^^^^^
72+
73+
warning: unused logical operation which must be used
74+
--> $DIR/must-use-ops.rs:38:5
75+
|
76+
LL | true && true;
77+
| ^^^^^^^^^^^^
78+
79+
warning: unused logical operation which must be used
80+
--> $DIR/must-use-ops.rs:39:5
81+
|
82+
LL | false || true;
83+
| ^^^^^^^^^^^^^
84+
85+
warning: unused bitwise operation which must be used
86+
--> $DIR/must-use-ops.rs:42:5
87+
|
88+
LL | 5 ^ val;
89+
| ^^^^^^^
90+
91+
warning: unused bitwise operation which must be used
92+
--> $DIR/must-use-ops.rs:43:5
93+
|
94+
LL | 5 & val;
95+
| ^^^^^^^
96+
97+
warning: unused bitwise operation which must be used
98+
--> $DIR/must-use-ops.rs:44:5
99+
|
100+
LL | 5 | val;
101+
| ^^^^^^^
102+
103+
warning: unused bitwise operation which must be used
104+
--> $DIR/must-use-ops.rs:45:5
105+
|
106+
LL | 5 << val;
107+
| ^^^^^^^^
108+
109+
warning: unused bitwise operation which must be used
110+
--> $DIR/must-use-ops.rs:46:5
111+
|
112+
LL | 5 >> val;
113+
| ^^^^^^^^
114+
115+
warning: unused unary operation which must be used
116+
--> $DIR/must-use-ops.rs:49:5
117+
|
118+
LL | !val;
119+
| ^^^^
120+
121+
warning: unused unary operation which must be used
122+
--> $DIR/must-use-ops.rs:50:5
123+
|
124+
LL | -val;
125+
| ^^^^
126+
127+
warning: unused unary operation which must be used
128+
--> $DIR/must-use-ops.rs:51:5
129+
|
130+
LL | *val_pointer;
131+
| ^^^^^^^^^^^^
132+

0 commit comments

Comments
 (0)