Skip to content

Commit 439bae6

Browse files
committed
Auto merge of rust-lang#5884 - Ryan1729:patch-1, r=flip1995
Add the other overloadable operations to suspicious_arithmetic_impl In rust-lang#2268 I idly mused that the other user-overloadable operations could be added to this lint. Knowing that the lint was arguably incomplete was gnawing at the back of my mind, so I figured that I might as well make this PR, particularly given the change needed was so small. changelog: Start warning on suspicious implementations of the `BitAnd`, `BitOr`, `BitXor`, `Rem`, `Shl`, and `Shr` traits.
2 parents c73cf9f + 7bd7a46 commit 439bae6

File tree

3 files changed

+100
-6
lines changed

3 files changed

+100
-6
lines changed

clippy_lints/src/suspicious_trait_impl.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,12 +86,20 @@ impl<'tcx> LateLintPass<'tcx> for SuspiciousImpl {
8686
cx,
8787
expr,
8888
binop.node,
89-
&["Add", "Sub", "Mul", "Div"],
89+
&[
90+
"Add", "Sub", "Mul", "Div", "Rem", "BitAnd", "BitOr", "BitXor", "Shl", "Shr",
91+
],
9092
&[
9193
hir::BinOpKind::Add,
9294
hir::BinOpKind::Sub,
9395
hir::BinOpKind::Mul,
9496
hir::BinOpKind::Div,
97+
hir::BinOpKind::Rem,
98+
hir::BinOpKind::BitAnd,
99+
hir::BinOpKind::BitOr,
100+
hir::BinOpKind::BitXor,
101+
hir::BinOpKind::Shl,
102+
hir::BinOpKind::Shr,
95103
],
96104
) {
97105
span_lint(

tests/ui/suspicious_arithmetic_impl.rs

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
#![warn(clippy::suspicious_arithmetic_impl)]
2-
use std::ops::{Add, AddAssign, BitOrAssign, Div, DivAssign, Mul, MulAssign, Sub};
2+
use std::ops::{
3+
Add, AddAssign, BitAnd, BitOr, BitOrAssign, BitXor, Div, DivAssign, Mul, MulAssign, Rem, Shl, Shr, Sub,
4+
};
35

46
#[derive(Copy, Clone)]
57
struct Foo(u32);
@@ -61,6 +63,54 @@ impl Div for Foo {
6163
}
6264
}
6365

66+
impl Rem for Foo {
67+
type Output = Foo;
68+
69+
fn rem(self, other: Self) -> Self {
70+
Foo(self.0 / other.0)
71+
}
72+
}
73+
74+
impl BitAnd for Foo {
75+
type Output = Foo;
76+
77+
fn bitand(self, other: Self) -> Self {
78+
Foo(self.0 | other.0)
79+
}
80+
}
81+
82+
impl BitOr for Foo {
83+
type Output = Foo;
84+
85+
fn bitor(self, other: Self) -> Self {
86+
Foo(self.0 ^ other.0)
87+
}
88+
}
89+
90+
impl BitXor for Foo {
91+
type Output = Foo;
92+
93+
fn bitxor(self, other: Self) -> Self {
94+
Foo(self.0 & other.0)
95+
}
96+
}
97+
98+
impl Shl for Foo {
99+
type Output = Foo;
100+
101+
fn shl(self, other: Self) -> Self {
102+
Foo(self.0 >> other.0)
103+
}
104+
}
105+
106+
impl Shr for Foo {
107+
type Output = Foo;
108+
109+
fn shr(self, other: Self) -> Self {
110+
Foo(self.0 << other.0)
111+
}
112+
}
113+
64114
struct Bar(i32);
65115

66116
impl Add for Bar {
Lines changed: 40 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,60 @@
11
error: suspicious use of binary operator in `Add` impl
2-
--> $DIR/suspicious_arithmetic_impl.rs:11:20
2+
--> $DIR/suspicious_arithmetic_impl.rs:13:20
33
|
44
LL | Foo(self.0 - other.0)
55
| ^
66
|
77
= note: `-D clippy::suspicious-arithmetic-impl` implied by `-D warnings`
88

99
error: suspicious use of binary operator in `AddAssign` impl
10-
--> $DIR/suspicious_arithmetic_impl.rs:17:23
10+
--> $DIR/suspicious_arithmetic_impl.rs:19:23
1111
|
1212
LL | *self = *self - other;
1313
| ^
1414
|
1515
= note: `#[deny(clippy::suspicious_op_assign_impl)]` on by default
1616

1717
error: suspicious use of binary operator in `MulAssign` impl
18-
--> $DIR/suspicious_arithmetic_impl.rs:30:16
18+
--> $DIR/suspicious_arithmetic_impl.rs:32:16
1919
|
2020
LL | self.0 /= other.0;
2121
| ^^
2222

23-
error: aborting due to 3 previous errors
23+
error: suspicious use of binary operator in `Rem` impl
24+
--> $DIR/suspicious_arithmetic_impl.rs:70:20
25+
|
26+
LL | Foo(self.0 / other.0)
27+
| ^
28+
29+
error: suspicious use of binary operator in `BitAnd` impl
30+
--> $DIR/suspicious_arithmetic_impl.rs:78:20
31+
|
32+
LL | Foo(self.0 | other.0)
33+
| ^
34+
35+
error: suspicious use of binary operator in `BitOr` impl
36+
--> $DIR/suspicious_arithmetic_impl.rs:86:20
37+
|
38+
LL | Foo(self.0 ^ other.0)
39+
| ^
40+
41+
error: suspicious use of binary operator in `BitXor` impl
42+
--> $DIR/suspicious_arithmetic_impl.rs:94:20
43+
|
44+
LL | Foo(self.0 & other.0)
45+
| ^
46+
47+
error: suspicious use of binary operator in `Shl` impl
48+
--> $DIR/suspicious_arithmetic_impl.rs:102:20
49+
|
50+
LL | Foo(self.0 >> other.0)
51+
| ^^
52+
53+
error: suspicious use of binary operator in `Shr` impl
54+
--> $DIR/suspicious_arithmetic_impl.rs:110:20
55+
|
56+
LL | Foo(self.0 << other.0)
57+
| ^^
58+
59+
error: aborting due to 9 previous errors
2460

0 commit comments

Comments
 (0)