@@ -59,59 +59,43 @@ impl IntPlusOne {
59
59
false
60
60
}
61
61
62
- fn check_binop ( & self , cx : & EarlyContext , binop : BinOpKind , lhs : & Expr , rhs : & Expr ) -> Option < ( bool , Option < String > ) > {
62
+ fn check_binop ( & self , cx : & EarlyContext , binop : BinOpKind , lhs : & Expr , rhs : & Expr ) -> 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
- ( BinOpKind :: Add , & ExprKind :: Lit ( ref lit) , _) => {
69
- Some ( ( self . check_lit ( lit, -1 ) , self . generate_recommendation ( cx, binop, lhsrhs, rhs, Side :: LHS ) ) )
70
- } ,
68
+ ( BinOpKind :: Add , & ExprKind :: Lit ( ref lit) , _) if self . check_lit ( lit, -1 ) => self . generate_recommendation ( cx, binop, lhsrhs, rhs, Side :: LHS ) ,
71
69
// `x - 1`
72
- ( BinOpKind :: Sub , _, & ExprKind :: Lit ( ref lit) ) => {
73
- Some ( ( self . check_lit ( lit, 1 ) , self . generate_recommendation ( cx, binop, lhslhs, rhs, Side :: LHS ) ) )
74
- }
70
+ ( BinOpKind :: Sub , _, & ExprKind :: Lit ( ref lit) ) if self . check_lit ( lit, 1 ) => self . generate_recommendation ( cx, binop, lhslhs, rhs, Side :: LHS ) ,
75
71
_ => None
76
72
}
77
73
} ,
78
74
// case where `... >= y + 1` or `... >= 1 + y`
79
75
( BinOpKind :: Ge , _, & ExprKind :: Binary ( ref rhskind, ref rhslhs, ref rhsrhs) ) if rhskind. node == BinOpKind :: Add => {
80
76
match ( & rhslhs. node , & rhsrhs. node ) {
81
77
// `y + 1` and `1 + y`
82
- ( & ExprKind :: Lit ( ref lit) , _) => {
83
- Some ( ( self . check_lit ( lit, 1 ) , self . generate_recommendation ( cx, binop, rhsrhs, lhs, Side :: RHS ) ) )
84
- } ,
85
- ( _, & ExprKind :: Lit ( ref lit) ) => {
86
- Some ( ( self . check_lit ( lit, 1 ) , self . generate_recommendation ( cx, binop, rhslhs, lhs, Side :: RHS ) ) )
87
- } ,
78
+ ( & ExprKind :: Lit ( ref lit) , _) if self . check_lit ( lit, 1 ) => self . generate_recommendation ( cx, binop, rhsrhs, lhs, Side :: RHS ) ,
79
+ ( _, & ExprKind :: Lit ( ref lit) ) if self . check_lit ( lit, 1 ) => self . generate_recommendation ( cx, binop, rhslhs, lhs, Side :: RHS ) ,
88
80
_ => None
89
81
}
90
82
} ,
91
83
// case where `x + 1 <= ...` or `1 + x <= ...`
92
84
( BinOpKind :: Le , & ExprKind :: Binary ( ref lhskind, ref lhslhs, ref lhsrhs) , _) if lhskind. node == BinOpKind :: Add => {
93
85
match ( & lhslhs. node , & lhsrhs. node ) {
94
86
// `1 + x` and `x + 1`
95
- ( & ExprKind :: Lit ( ref lit) , _) => {
96
- Some ( ( self . check_lit ( lit, 1 ) , self . generate_recommendation ( cx, binop, lhsrhs, rhs, Side :: LHS ) ) )
97
- } ,
98
- ( _, & ExprKind :: Lit ( ref lit) ) => {
99
- Some ( ( self . check_lit ( lit, 1 ) , self . generate_recommendation ( cx, binop, lhslhs, rhs, Side :: LHS ) ) )
100
- } ,
87
+ ( & ExprKind :: Lit ( ref lit) , _) if self . check_lit ( lit, 1 ) => self . generate_recommendation ( cx, binop, lhsrhs, rhs, Side :: LHS ) ,
88
+ ( _, & ExprKind :: Lit ( ref lit) ) if self . check_lit ( lit, 1 ) => self . generate_recommendation ( cx, binop, lhslhs, rhs, Side :: LHS ) ,
101
89
_ => None
102
90
}
103
91
} ,
104
92
// case where `... >= y - 1` or `... >= -1 + y`
105
93
( BinOpKind :: Le , _, & ExprKind :: Binary ( ref rhskind, ref rhslhs, ref rhsrhs) ) => {
106
94
match ( rhskind. node , & rhslhs. node , & rhsrhs. node ) {
107
95
// `-1 + y`
108
- ( BinOpKind :: Add , & ExprKind :: Lit ( ref lit) , _) => {
109
- Some ( ( self . check_lit ( lit, -1 ) , self . generate_recommendation ( cx, binop, rhsrhs, lhs, Side :: RHS ) ) )
110
- } ,
96
+ ( BinOpKind :: Add , & ExprKind :: Lit ( ref lit) , _) if self . check_lit ( lit, -1 ) => self . generate_recommendation ( cx, binop, rhsrhs, lhs, Side :: RHS ) ,
111
97
// `y - 1`
112
- ( BinOpKind :: Sub , _, & ExprKind :: Lit ( ref lit) ) => {
113
- Some ( ( self . check_lit ( lit, 1 ) , self . generate_recommendation ( cx, binop, rhslhs, lhs, Side :: RHS ) ) )
114
- } ,
98
+ ( BinOpKind :: Sub , _, & ExprKind :: Lit ( ref lit) ) if self . check_lit ( lit, 1 ) => self . generate_recommendation ( cx, binop, rhslhs, lhs, Side :: RHS ) ,
115
99
_ => None
116
100
}
117
101
} ,
@@ -151,9 +135,8 @@ impl IntPlusOne {
151
135
impl EarlyLintPass for IntPlusOne {
152
136
fn check_expr ( & mut self , cx : & EarlyContext , item : & Expr ) {
153
137
if let ExprKind :: Binary ( ref kind, ref lhs, ref rhs) = item. node {
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
- _ => ( )
138
+ if let Some ( ref rec) = self . check_binop ( cx, kind. node , lhs, rhs) {
139
+ self . emit_warning ( cx, item, rec. clone ( ) ) ;
157
140
}
158
141
}
159
142
}
0 commit comments