@@ -63,11 +63,10 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for ImplicitSaturatingSub {
63
63
64
64
// Check if assign operation is done
65
65
if let StmtKind :: Semi ( ref e) = block. stmts[ 0 ] . kind;
66
- if subtracts_one( e) ;
67
- if let ExprKind :: AssignOp ( _, ref target, _) = e. kind;
68
- if let ExprKind :: Path ( ref assign_path) = target. kind;
66
+ if let ( true , Some ( target) ) = subtracts_one( cx, e) ;
69
67
70
68
// Extracting out the variable name
69
+ if let ExprKind :: Path ( ref assign_path) = target. kind;
71
70
if let QPath :: Resolved ( _, ref ares_path) = assign_path;
72
71
73
72
then {
@@ -126,19 +125,39 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for ImplicitSaturatingSub {
126
125
}
127
126
}
128
127
129
- fn subtracts_one ( expr : & Expr < ' _ > ) -> bool {
130
- if_chain ! {
131
- if let ExprKind :: AssignOp ( ref op1, _, ref value) = expr. kind;
132
- if BinOpKind :: Sub == op1. node;
128
+ fn subtracts_one < ' a > ( cx : & LateContext < ' _ , ' _ > , expr : & Expr < ' a > ) -> ( bool , Option < & ' a Expr < ' a > > ) {
129
+ match expr. kind {
130
+ ExprKind :: AssignOp ( ref op1, ref target, ref value) => {
131
+ if_chain ! {
132
+ if BinOpKind :: Sub == op1. node;
133
+ // Check if literal being subtracted is one
134
+ if let ExprKind :: Lit ( ref lit1) = value. kind;
135
+ if let LitKind :: Int ( 1 , _) = lit1. node;
136
+ then {
137
+ ( true , Option :: Some ( target) )
138
+ } else {
139
+ ( false , None )
140
+ }
141
+ }
142
+ } ,
143
+ ExprKind :: Assign ( ref target, ref value, _) => {
144
+ if_chain ! {
145
+ if let ExprKind :: Binary ( ref op1, ref left1, ref right1) = value. kind;
146
+ if BinOpKind :: Sub == op1. node;
147
+
148
+ if SpanlessEq :: new( cx) . eq_expr( left1, target) ;
133
149
134
- // Check if literal being subtracted is one
135
- if let ExprKind :: Lit ( ref lit1) = value. kind;
136
- if let LitKind :: Int ( 1 , _) = lit1. node;
137
- then {
138
- return true ;
139
- }
150
+ if let ExprKind :: Lit ( ref lit1) = right1. kind;
151
+ if let LitKind :: Int ( 1 , _) = lit1. node;
152
+ then {
153
+ ( true , Some ( target) )
154
+ } else {
155
+ ( false , None )
156
+ }
157
+ }
158
+ } ,
159
+ _ => ( false , None ) ,
140
160
}
141
- false
142
161
}
143
162
144
163
fn print_lint_and_sugg ( cx : & LateContext < ' _ , ' _ > , var_name : & str , expr : & Expr < ' _ > ) {
0 commit comments