Skip to content

Commit 7427065

Browse files
committed
Auto merge of #5702 - ebroto:5698_mul_not_comm, r=matthiaskrgr
if_same_then_else: don't assume multiplication is always commutative changelog: Don't assume multiplication is always commutative in [`if_same_then_else`] Fixes #5698
2 parents f065d4b + 2f74283 commit 7427065

File tree

2 files changed

+17
-8
lines changed

2 files changed

+17
-8
lines changed

clippy_lints/src/utils/hir_utils.rs

+5-8
Original file line numberDiff line numberDiff line change
@@ -309,18 +309,15 @@ fn swap_binop<'a>(
309309
rhs: &'a Expr<'a>,
310310
) -> Option<(BinOpKind, &'a Expr<'a>, &'a Expr<'a>)> {
311311
match binop {
312-
BinOpKind::Add
313-
| BinOpKind::Mul
314-
| BinOpKind::Eq
315-
| BinOpKind::Ne
316-
| BinOpKind::BitAnd
317-
| BinOpKind::BitXor
318-
| BinOpKind::BitOr => Some((binop, rhs, lhs)),
312+
BinOpKind::Add | BinOpKind::Eq | BinOpKind::Ne | BinOpKind::BitAnd | BinOpKind::BitXor | BinOpKind::BitOr => {
313+
Some((binop, rhs, lhs))
314+
},
319315
BinOpKind::Lt => Some((BinOpKind::Gt, rhs, lhs)),
320316
BinOpKind::Le => Some((BinOpKind::Ge, rhs, lhs)),
321317
BinOpKind::Ge => Some((BinOpKind::Le, rhs, lhs)),
322318
BinOpKind::Gt => Some((BinOpKind::Lt, rhs, lhs)),
323-
BinOpKind::Shl
319+
BinOpKind::Mul // Not always commutative, e.g. with matrices. See issue #5698
320+
| BinOpKind::Shl
324321
| BinOpKind::Shr
325322
| BinOpKind::Rem
326323
| BinOpKind::Sub

tests/ui/if_same_then_else.rs

+12
Original file line numberDiff line numberDiff line change
@@ -142,4 +142,16 @@ fn func() {
142142

143143
fn f(val: &[u8]) {}
144144

145+
mod issue_5698 {
146+
fn mul_not_always_commutative(x: i32, y: i32) -> i32 {
147+
if x == 42 {
148+
x * y
149+
} else if x == 21 {
150+
y * x
151+
} else {
152+
0
153+
}
154+
}
155+
}
156+
145157
fn main() {}

0 commit comments

Comments
 (0)