@@ -55,13 +55,13 @@ impl<'tcx> LateLintPass<'tcx> for ManualRemEuclid {
55
55
if_chain ! {
56
56
if let ExprKind :: Binary ( op1, ..) = expr. kind;
57
57
if op1. node == BinOpKind :: Rem ;
58
- if let Some ( ( const1, expr1) ) = check_for_positive_int_constant( cx, expr) ;
58
+ if let Some ( ( const1, expr1) ) = check_for_positive_int_constant( cx, expr, false ) ;
59
59
if let ExprKind :: Binary ( op2, ..) = expr1. kind;
60
60
if op2. node == BinOpKind :: Add ;
61
- if let Some ( ( const2, expr2) ) = check_for_positive_int_constant( cx, expr1) ;
61
+ if let Some ( ( const2, expr2) ) = check_for_positive_int_constant( cx, expr1, true ) ;
62
62
if let ExprKind :: Binary ( op3, ..) = expr2. kind;
63
63
if op3. node == BinOpKind :: Rem ;
64
- if let Some ( ( const3, expr3) ) = check_for_positive_int_constant( cx, expr2) ;
64
+ if let Some ( ( const3, expr3) ) = check_for_positive_int_constant( cx, expr2, false ) ;
65
65
if const1 == const2 && const2 == const3;
66
66
// Only apply if we see an explicit type annotation on the local.
67
67
if let Some ( hir_id) = path_to_local( expr3) ;
@@ -91,13 +91,18 @@ impl<'tcx> LateLintPass<'tcx> for ManualRemEuclid {
91
91
}
92
92
93
93
// Takes a binary expression and separates the operands into the int constant and the other
94
- // operand. Ensures the int constant is positive.
95
- fn check_for_positive_int_constant < ' a > ( cx : & ' a LateContext < ' _ > , expr : & ' a Expr < ' _ > ) -> Option < ( u128 , & ' a Expr < ' a > ) > {
94
+ // operand. Ensures the int constant is positive. Operators that are not commutative must have the
95
+ // constant appear on the right hand side to return a value.
96
+ fn check_for_positive_int_constant < ' a > (
97
+ cx : & ' a LateContext < ' _ > ,
98
+ expr : & ' a Expr < ' _ > ,
99
+ is_commutative : bool ,
100
+ ) -> Option < ( u128 , & ' a Expr < ' a > ) > {
96
101
let ( int_const, other_op) = if let ExprKind :: Binary ( _, left, right) = expr. kind {
97
- if let Some ( int_const) = constant_full_int ( cx, cx. typeck_results ( ) , left) {
98
- ( int_const, right)
99
- } else if let Some ( int_const) = constant_full_int ( cx, cx. typeck_results ( ) , right) {
102
+ if let Some ( int_const) = constant_full_int ( cx, cx. typeck_results ( ) , right) {
100
103
( int_const, left)
104
+ } else if is_commutative && let Some ( int_const) = constant_full_int ( cx, cx. typeck_results ( ) , left) {
105
+ ( int_const, right)
101
106
} else {
102
107
return None ;
103
108
}
0 commit comments