@@ -59,87 +59,63 @@ impl IntPlusOne {
59
59
false
60
60
}
61
61
62
- fn check_binop ( & self , cx : & EarlyContext , block : & Expr , binop : BinOpKind , lhs : & Expr , rhs : & Expr ) {
62
+ fn check_binop ( & self , cx : & EarlyContext , binop : BinOpKind , lhs : & Expr , rhs : & Expr ) -> Option < ( bool , Option < String > ) > {
63
63
match ( binop, & lhs. node , & rhs. node ) {
64
64
// case where `x - 1 >= ...` or `-1 + x >= ...`
65
65
( BinOpKind :: Ge , & ExprKind :: Binary ( ref lhskind, ref lhslhs, ref lhsrhs) , _) => {
66
66
match ( lhskind. node , & lhslhs. node , & lhsrhs. node ) {
67
67
// `-1 + x`
68
68
( BinOpKind :: Add , & ExprKind :: Lit ( ref lit) , _) => {
69
- let recommendation = self . generate_recommendation ( cx, binop, lhsrhs, rhs, Side :: LHS ) ;
70
- if self . check_lit ( lit, -1 ) {
71
- self . emit_warning ( cx, block, recommendation)
72
- }
69
+ Some ( ( self . check_lit ( lit, -1 ) , self . generate_recommendation ( cx, binop, lhsrhs, rhs, Side :: LHS ) ) )
73
70
} ,
74
71
// `x - 1`
75
72
( BinOpKind :: Sub , _, & ExprKind :: Lit ( ref lit) ) => {
76
- let recommendation = self . generate_recommendation ( cx, binop, lhslhs, rhs, Side :: LHS ) ;
77
- if self . check_lit ( lit, 1 ) {
78
- self . emit_warning ( cx, block, recommendation)
79
- }
73
+ Some ( ( self . check_lit ( lit, 1 ) , self . generate_recommendation ( cx, binop, lhslhs, rhs, Side :: LHS ) ) )
80
74
}
81
- _ => ( )
75
+ _ => None
82
76
}
83
77
} ,
84
78
// case where `... >= y + 1` or `... >= 1 + y`
85
79
( BinOpKind :: Ge , _, & ExprKind :: Binary ( ref rhskind, ref rhslhs, ref rhsrhs) ) if rhskind. node == BinOpKind :: Add => {
86
80
match ( & rhslhs. node , & rhsrhs. node ) {
87
81
// `y + 1` and `1 + y`
88
82
( & ExprKind :: Lit ( ref lit) , _) => {
89
- let recommendation = self . generate_recommendation ( cx, binop, rhsrhs, lhs, Side :: RHS ) ;
90
- if self . check_lit ( lit, 1 ) {
91
- self . emit_warning ( cx, block, recommendation)
92
- }
83
+ Some ( ( self . check_lit ( lit, 1 ) , self . generate_recommendation ( cx, binop, rhsrhs, lhs, Side :: RHS ) ) )
93
84
} ,
94
85
( _, & ExprKind :: Lit ( ref lit) ) => {
95
- let recommendation = self . generate_recommendation ( cx, binop, rhslhs, lhs, Side :: RHS ) ;
96
- if self . check_lit ( lit, 1 ) {
97
- self . emit_warning ( cx, block, recommendation)
98
- }
86
+ Some ( ( self . check_lit ( lit, 1 ) , self . generate_recommendation ( cx, binop, rhslhs, lhs, Side :: RHS ) ) )
99
87
} ,
100
- _ => ( )
88
+ _ => None
101
89
}
102
90
} ,
103
91
// case where `x + 1 <= ...` or `1 + x <= ...`
104
92
( BinOpKind :: Le , & ExprKind :: Binary ( ref lhskind, ref lhslhs, ref lhsrhs) , _) if lhskind. node == BinOpKind :: Add => {
105
93
match ( & lhslhs. node , & lhsrhs. node ) {
106
94
// `1 + x` and `x + 1`
107
95
( & ExprKind :: Lit ( ref lit) , _) => {
108
- let recommendation = self . generate_recommendation ( cx, binop, lhsrhs, rhs, Side :: LHS ) ;
109
- if self . check_lit ( lit, 1 ) {
110
- self . emit_warning ( cx, block, recommendation)
111
- }
96
+ Some ( ( self . check_lit ( lit, 1 ) , self . generate_recommendation ( cx, binop, lhsrhs, rhs, Side :: LHS ) ) )
112
97
} ,
113
98
( _, & ExprKind :: Lit ( ref lit) ) => {
114
- let recommendation = self . generate_recommendation ( cx, binop, lhslhs, rhs, Side :: LHS ) ;
115
- if self . check_lit ( lit, 1 ) {
116
- self . emit_warning ( cx, block, recommendation)
117
- }
99
+ Some ( ( self . check_lit ( lit, 1 ) , self . generate_recommendation ( cx, binop, lhslhs, rhs, Side :: LHS ) ) )
118
100
} ,
119
- _ => ( )
101
+ _ => None
120
102
}
121
103
} ,
122
104
// case where `... >= y - 1` or `... >= -1 + y`
123
105
( BinOpKind :: Le , _, & ExprKind :: Binary ( ref rhskind, ref rhslhs, ref rhsrhs) ) => {
124
106
match ( rhskind. node , & rhslhs. node , & rhsrhs. node ) {
125
107
// `-1 + y`
126
108
( BinOpKind :: Add , & ExprKind :: Lit ( ref lit) , _) => {
127
- let recommendation = self . generate_recommendation ( cx, binop, rhsrhs, lhs, Side :: RHS ) ;
128
- if self . check_lit ( lit, -1 ) {
129
- self . emit_warning ( cx, block, recommendation)
130
- }
109
+ Some ( ( self . check_lit ( lit, -1 ) , self . generate_recommendation ( cx, binop, rhsrhs, lhs, Side :: RHS ) ) )
131
110
} ,
132
111
// `y - 1`
133
112
( BinOpKind :: Sub , _, & ExprKind :: Lit ( ref lit) ) => {
134
- let recommendation = self . generate_recommendation ( cx, binop, rhslhs, lhs, Side :: RHS ) ;
135
- if self . check_lit ( lit, 1 ) {
136
- self . emit_warning ( cx, block, recommendation)
137
- }
113
+ Some ( ( self . check_lit ( lit, 1 ) , self . generate_recommendation ( cx, binop, rhslhs, lhs, Side :: RHS ) ) )
138
114
} ,
139
- _ => ( )
115
+ _ => None
140
116
}
141
117
} ,
142
- _ => ( )
118
+ _ => None
143
119
}
144
120
}
145
121
@@ -161,23 +137,24 @@ impl IntPlusOne {
161
137
None
162
138
}
163
139
164
- fn emit_warning ( & self , cx : & EarlyContext , block : & Expr , recommendation : Option < String > ) {
165
- if let Some ( rec) = recommendation {
166
- span_lint_and_then ( cx,
167
- INT_PLUS_ONE ,
168
- block. span ,
169
- "Unnecessary `>= y + 1` or `x - 1 >=`" ,
170
- |db| {
171
- db. span_suggestion ( block. span , "change `>= y + 1` to `> y` as shown" , rec) ;
172
- } ) ;
173
- }
140
+ fn emit_warning ( & self , cx : & EarlyContext , block : & Expr , recommendation : String ) {
141
+ span_lint_and_then ( cx,
142
+ INT_PLUS_ONE ,
143
+ block. span ,
144
+ "Unnecessary `>= y + 1` or `x - 1 >=`" ,
145
+ |db| {
146
+ db. span_suggestion ( block. span , "change `>= y + 1` to `> y` as shown" , recommendation) ;
147
+ } ) ;
174
148
}
175
149
}
176
150
177
151
impl EarlyLintPass for IntPlusOne {
178
152
fn check_expr ( & mut self , cx : & EarlyContext , item : & Expr ) {
179
153
if let ExprKind :: Binary ( ref kind, ref lhs, ref rhs) = item. node {
180
- self . check_binop ( cx, item, kind. node , lhs, rhs) ;
154
+ match self . check_binop ( cx, kind. node , lhs, rhs) {
155
+ Some ( ( should_emit, Some ( ref rec) ) ) if should_emit => self . emit_warning ( cx, item, rec. clone ( ) ) ,
156
+ _ => ( )
157
+ }
181
158
}
182
159
}
183
160
}
0 commit comments