@@ -2,14 +2,14 @@ use crate::utils::{
2
2
get_trait_def_id, implements_trait, in_macro, in_macro_or_desugar, match_type, paths, snippet_opt,
3
3
span_lint_and_then, SpanlessEq ,
4
4
} ;
5
+ use if_chain:: if_chain;
5
6
use rustc:: hir:: intravisit:: * ;
6
7
use rustc:: hir:: * ;
7
8
use rustc:: lint:: { LateContext , LateLintPass , LintArray , LintPass } ;
8
9
use rustc:: { declare_lint_pass, declare_tool_lint} ;
9
- use rustc_data_structures:: thin_vec:: ThinVec ;
10
10
use rustc_errors:: Applicability ;
11
11
use syntax:: ast:: LitKind ;
12
- use syntax:: source_map:: { dummy_spanned , Span , DUMMY_SP } ;
12
+ use syntax:: source_map:: Span ;
13
13
14
14
declare_clippy_lint ! {
15
15
/// **What it does:** Checks for boolean expressions that can be written more
@@ -93,6 +93,18 @@ impl<'a, 'tcx, 'v> Hir2Qmm<'a, 'tcx, 'v> {
93
93
}
94
94
95
95
fn run ( & mut self , e : & ' v Expr ) -> Result < Bool , String > {
96
+ fn negate ( bin_op_kind : BinOpKind ) -> Option < BinOpKind > {
97
+ match bin_op_kind {
98
+ BinOpKind :: Eq => Some ( BinOpKind :: Ne ) ,
99
+ BinOpKind :: Ne => Some ( BinOpKind :: Eq ) ,
100
+ BinOpKind :: Gt => Some ( BinOpKind :: Le ) ,
101
+ BinOpKind :: Ge => Some ( BinOpKind :: Lt ) ,
102
+ BinOpKind :: Lt => Some ( BinOpKind :: Ge ) ,
103
+ BinOpKind :: Le => Some ( BinOpKind :: Gt ) ,
104
+ _ => None ,
105
+ }
106
+ }
107
+
96
108
// prevent folding of `cfg!` macros and the like
97
109
if !in_macro_or_desugar ( e. span ) {
98
110
match & e. node {
@@ -115,33 +127,18 @@ impl<'a, 'tcx, 'v> Hir2Qmm<'a, 'tcx, 'v> {
115
127
#[ allow( clippy:: cast_possible_truncation) ]
116
128
return Ok ( Bool :: Term ( n as u8 ) ) ;
117
129
}
118
- let negated = match & e. node {
119
- ExprKind :: Binary ( binop, lhs, rhs) => {
120
- if !implements_ord ( self . cx , lhs) {
121
- continue ;
122
- }
123
130
124
- let mk_expr = |op| Expr {
125
- hir_id : DUMMY_HIR_ID ,
126
- span : DUMMY_SP ,
127
- attrs : ThinVec :: new ( ) ,
128
- node : ExprKind :: Binary ( dummy_spanned ( op) , lhs. clone ( ) , rhs. clone ( ) ) ,
129
- } ;
130
- match binop. node {
131
- BinOpKind :: Eq => mk_expr ( BinOpKind :: Ne ) ,
132
- BinOpKind :: Ne => mk_expr ( BinOpKind :: Eq ) ,
133
- BinOpKind :: Gt => mk_expr ( BinOpKind :: Le ) ,
134
- BinOpKind :: Ge => mk_expr ( BinOpKind :: Lt ) ,
135
- BinOpKind :: Lt => mk_expr ( BinOpKind :: Ge ) ,
136
- BinOpKind :: Le => mk_expr ( BinOpKind :: Gt ) ,
137
- _ => continue ,
138
- }
139
- } ,
140
- _ => continue ,
141
- } ;
142
- if SpanlessEq :: new ( self . cx ) . ignore_fn ( ) . eq_expr ( & negated, expr) {
143
- #[ allow( clippy:: cast_possible_truncation) ]
144
- return Ok ( Bool :: Not ( Box :: new ( Bool :: Term ( n as u8 ) ) ) ) ;
131
+ if_chain ! {
132
+ if let ExprKind :: Binary ( e_binop, e_lhs, e_rhs) = & e. node;
133
+ if implements_ord( self . cx, e_lhs) ;
134
+ if let ExprKind :: Binary ( expr_binop, expr_lhs, expr_rhs) = & expr. node;
135
+ if negate( e_binop. node) == Some ( expr_binop. node) ;
136
+ if SpanlessEq :: new( self . cx) . ignore_fn( ) . eq_expr( e_lhs, expr_lhs) ;
137
+ if SpanlessEq :: new( self . cx) . ignore_fn( ) . eq_expr( e_rhs, expr_rhs) ;
138
+ then {
139
+ #[ allow( clippy:: cast_possible_truncation) ]
140
+ return Ok ( Bool :: Not ( Box :: new( Bool :: Term ( n as u8 ) ) ) ) ;
141
+ }
145
142
}
146
143
}
147
144
let n = self . terminals . len ( ) ;
0 commit comments