Skip to content

Commit

Permalink
Auto merge of #5702 - ebroto:5698_mul_not_comm, r=matthiaskrgr
Browse files Browse the repository at this point in the history
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
  • Loading branch information
bors committed Jun 9, 2020
2 parents f065d4b + 2f74283 commit 7427065
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 8 deletions.
13 changes: 5 additions & 8 deletions clippy_lints/src/utils/hir_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -309,18 +309,15 @@ fn swap_binop<'a>(
rhs: &'a Expr<'a>,
) -> Option<(BinOpKind, &'a Expr<'a>, &'a Expr<'a>)> {
match binop {
BinOpKind::Add
| BinOpKind::Mul
| BinOpKind::Eq
| BinOpKind::Ne
| BinOpKind::BitAnd
| BinOpKind::BitXor
| BinOpKind::BitOr => Some((binop, rhs, lhs)),
BinOpKind::Add | BinOpKind::Eq | BinOpKind::Ne | BinOpKind::BitAnd | BinOpKind::BitXor | BinOpKind::BitOr => {
Some((binop, rhs, lhs))
},
BinOpKind::Lt => Some((BinOpKind::Gt, rhs, lhs)),
BinOpKind::Le => Some((BinOpKind::Ge, rhs, lhs)),
BinOpKind::Ge => Some((BinOpKind::Le, rhs, lhs)),
BinOpKind::Gt => Some((BinOpKind::Lt, rhs, lhs)),
BinOpKind::Shl
BinOpKind::Mul // Not always commutative, e.g. with matrices. See issue #5698
| BinOpKind::Shl
| BinOpKind::Shr
| BinOpKind::Rem
| BinOpKind::Sub
Expand Down
12 changes: 12 additions & 0 deletions tests/ui/if_same_then_else.rs
Original file line number Diff line number Diff line change
Expand Up @@ -142,4 +142,16 @@ fn func() {

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

mod issue_5698 {
fn mul_not_always_commutative(x: i32, y: i32) -> i32 {
if x == 42 {
x * y
} else if x == 21 {
y * x
} else {
0
}
}
}

fn main() {}

0 comments on commit 7427065

Please sign in to comment.