Skip to content

Commit 8fc592a

Browse files
authored
Rollup merge of rust-lang#5424 - jpospychala:suspicious_op_assign_impl, r=flip1995
Incorrect suspicious_op_assign_impl fixes rust-lang#5255 changelog: In suspicious_op_assign_impl ignore all operators in expression if it's part of AssignOp
2 parents b42bd19 + 9c9af1d commit 8fc592a

File tree

3 files changed

+33
-6
lines changed

3 files changed

+33
-6
lines changed

clippy_lints/src/suspicious_trait_impl.rs

+6-4
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ declare_lint_pass!(SuspiciousImpl => [SUSPICIOUS_ARITHMETIC_IMPL, SUSPICIOUS_OP_
5454

5555
impl<'a, 'tcx> LateLintPass<'a, 'tcx> for SuspiciousImpl {
5656
fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, expr: &'tcx hir::Expr<'_>) {
57-
if let hir::ExprKind::Binary(binop, _, _) = expr.kind {
57+
if let hir::ExprKind::Binary(binop, _, _) | hir::ExprKind::AssignOp(binop, ..) = expr.kind {
5858
match binop.node {
5959
hir::BinOpKind::Eq
6060
| hir::BinOpKind::Lt
@@ -65,14 +65,15 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for SuspiciousImpl {
6565
_ => {},
6666
}
6767
// Check if the binary expression is part of another bi/unary expression
68-
// as a child node
68+
// or operator assignment as a child node
6969
let mut parent_expr = cx.tcx.hir().get_parent_node(expr.hir_id);
7070
while parent_expr != hir::CRATE_HIR_ID {
7171
if let hir::Node::Expr(e) = cx.tcx.hir().get(parent_expr) {
7272
match e.kind {
7373
hir::ExprKind::Binary(..)
7474
| hir::ExprKind::Unary(hir::UnOp::UnNot, _)
75-
| hir::ExprKind::Unary(hir::UnOp::UnNeg, _) => return,
75+
| hir::ExprKind::Unary(hir::UnOp::UnNeg, _)
76+
| hir::ExprKind::AssignOp(..) => return,
7677
_ => {},
7778
}
7879
}
@@ -191,7 +192,8 @@ impl<'a, 'tcx> Visitor<'tcx> for BinaryExprVisitor {
191192
match expr.kind {
192193
hir::ExprKind::Binary(..)
193194
| hir::ExprKind::Unary(hir::UnOp::UnNot, _)
194-
| hir::ExprKind::Unary(hir::UnOp::UnNeg, _) => self.in_binary_expr = true,
195+
| hir::ExprKind::Unary(hir::UnOp::UnNeg, _)
196+
| hir::ExprKind::AssignOp(..) => self.in_binary_expr = true,
195197
_ => {},
196198
}
197199

tests/ui/suspicious_arithmetic_impl.rs

+20-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#![warn(clippy::suspicious_arithmetic_impl)]
2-
use std::ops::{Add, AddAssign, Div, Mul, Sub};
2+
use std::ops::{Add, AddAssign, BitOrAssign, Div, DivAssign, Mul, MulAssign, Sub};
33

44
#[derive(Copy, Clone)]
55
struct Foo(u32);
@@ -18,6 +18,25 @@ impl AddAssign for Foo {
1818
}
1919
}
2020

21+
impl BitOrAssign for Foo {
22+
fn bitor_assign(&mut self, other: Foo) {
23+
let idx = other.0;
24+
self.0 |= 1 << idx; // OK: BinOpKind::Shl part of AssignOp as child node
25+
}
26+
}
27+
28+
impl MulAssign for Foo {
29+
fn mul_assign(&mut self, other: Foo) {
30+
self.0 /= other.0;
31+
}
32+
}
33+
34+
impl DivAssign for Foo {
35+
fn div_assign(&mut self, other: Foo) {
36+
self.0 /= other.0; // OK: BinOpKind::Div == DivAssign
37+
}
38+
}
39+
2140
impl Mul for Foo {
2241
type Output = Foo;
2342

tests/ui/suspicious_arithmetic_impl.stderr

+7-1
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,11 @@ LL | *self = *self - other;
1414
|
1515
= note: `#[deny(clippy::suspicious_op_assign_impl)]` on by default
1616

17-
error: aborting due to 2 previous errors
17+
error: Suspicious use of binary operator in `MulAssign` impl
18+
--> $DIR/suspicious_arithmetic_impl.rs:30:16
19+
|
20+
LL | self.0 /= other.0;
21+
| ^^
22+
23+
error: aborting due to 3 previous errors
1824

0 commit comments

Comments
 (0)