@@ -2,14 +2,14 @@ use crate::utils::{
22 get_trait_def_id, implements_trait, in_macro, in_macro_or_desugar, match_type, paths, snippet_opt,
33 span_lint_and_then, SpanlessEq ,
44} ;
5+ use if_chain:: if_chain;
56use rustc:: hir:: intravisit:: * ;
67use rustc:: hir:: * ;
78use rustc:: lint:: { LateContext , LateLintPass , LintArray , LintPass } ;
89use rustc:: { declare_lint_pass, declare_tool_lint} ;
9- use rustc_data_structures:: thin_vec:: ThinVec ;
1010use rustc_errors:: Applicability ;
1111use syntax:: ast:: LitKind ;
12- use syntax:: source_map:: { dummy_spanned , Span , DUMMY_SP } ;
12+ use syntax:: source_map:: Span ;
1313
1414declare_clippy_lint ! {
1515 /// **What it does:** Checks for boolean expressions that can be written more
@@ -93,6 +93,18 @@ impl<'a, 'tcx, 'v> Hir2Qmm<'a, 'tcx, 'v> {
9393 }
9494
9595 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+
96108 // prevent folding of `cfg!` macros and the like
97109 if !in_macro_or_desugar ( e. span ) {
98110 match & e. node {
@@ -115,33 +127,18 @@ impl<'a, 'tcx, 'v> Hir2Qmm<'a, 'tcx, 'v> {
115127 #[ allow( clippy:: cast_possible_truncation) ]
116128 return Ok ( Bool :: Term ( n as u8 ) ) ;
117129 }
118- let negated = match & e. node {
119- ExprKind :: Binary ( binop, lhs, rhs) => {
120- if !implements_ord ( self . cx , lhs) {
121- continue ;
122- }
123130
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+ }
145142 }
146143 }
147144 let n = self . terminals . len ( ) ;
0 commit comments