@@ -3,7 +3,7 @@ use clippy_utils::higher::PanicExpn;
33use clippy_utils:: is_expn_of;
44use clippy_utils:: source:: snippet_with_applicability;
55use rustc_errors:: Applicability ;
6- use rustc_hir:: { Block , Expr , ExprKind , StmtKind , UnOp } ;
6+ use rustc_hir:: { BinOpKind , Block , Expr , ExprKind , StmtKind , UnOp } ;
77use rustc_lint:: { LateContext , LateLintPass } ;
88use rustc_session:: { declare_lint_pass, declare_tool_lint} ;
99
@@ -74,10 +74,38 @@ impl LateLintPass<'_> for IfThenPanic {
7474 } ;
7575 let mut applicability = Applicability :: MachineApplicable ;
7676 let sugg = snippet_with_applicability( cx, span, ".." , & mut applicability) ;
77-
78- let cond_sugg =
79- if let ExprKind :: DropTemps ( Expr { kind: ExprKind :: Unary ( UnOp :: Not , not_expr) , ..} ) = cond. kind {
80- snippet_with_applicability( cx, not_expr. span, ".." , & mut applicability) . to_string( )
77+ //let mut cond_sugg = format!("!{}", snippet_with_applicability(cx, cond.span, "..", &mut applicability));
78+ let cond_sugg = if let ExprKind :: DropTemps ( e, ..) = cond. kind {
79+ if let Expr { kind: ExprKind :: Unary ( UnOp :: Not , not_expr) , ..} = e {
80+ snippet_with_applicability( cx, not_expr. span, ".." , & mut applicability) . to_string( )
81+ } else if let Expr { kind: ExprKind :: Binary ( op, left, right) , ..} = e { //BinOp{BinOpKind::And, ..}
82+ match op. node {
83+ BinOpKind :: And | BinOpKind :: Or => {
84+ let left_span = {
85+ if let Expr { kind: ExprKind :: Unary ( UnOp :: Not , not_expr) , ..} = left {
86+ snippet_with_applicability( cx, not_expr. span, ".." , & mut applicability) . to_string( )
87+ } else {
88+ format!( "!{}" , snippet_with_applicability( cx, left. span, ".." , & mut applicability) )
89+ }
90+ } ;
91+ let right_span = {
92+ if let Expr { kind: ExprKind :: Unary ( UnOp :: Not , not_expr) , ..} = right {
93+ snippet_with_applicability( cx, not_expr. span, ".." , & mut applicability) . to_string( )
94+ } else {
95+ format!( "!{}" , snippet_with_applicability( cx, right. span, ".." , & mut applicability) )
96+ }
97+ } ;
98+ if op. node == BinOpKind :: And {
99+ format!( "{} || {}" , left_span, right_span)
100+ } else {
101+ format!( "{} && {}" , left_span, right_span)
102+ }
103+ }
104+ _ => format!( "!({})" , snippet_with_applicability( cx, cond. span, ".." , & mut applicability) )
105+ }
106+ } else {
107+ format!( "!{}" , snippet_with_applicability( cx, cond. span, ".." , & mut applicability) )
108+ }
81109 } else {
82110 format!( "!{}" , snippet_with_applicability( cx, cond. span, ".." , & mut applicability) )
83111 } ;
0 commit comments