@@ -81,69 +81,65 @@ declare_clippy_lint! {
81
81
}
82
82
83
83
fn check_collapsible_if_nested_if_else ( cx : & LateContext < ' _ > , if_expr : & Expr < ' _ > ) {
84
- if let ExprKind :: If ( cond, then, else_opt) = if_expr. kind {
85
- if let ExprKind :: Block ( then_block, _) = then. kind {
86
- if let Some ( then_expr) = then_block. expr {
87
- if let ExprKind :: If ( inner_cond, inner_then, _) = then_expr. kind {
88
- if let Some ( else_expr) = else_opt {
89
- let mut spanless_eq = SpanlessEq :: new ( cx) ;
84
+ if let ExprKind :: If ( cond, then, else_opt) = if_expr. kind &&
85
+ let ExprKind :: Block ( then_block, _) = then. kind &&
86
+ let Some ( then_expr) = then_block. expr &&
87
+ let ExprKind :: If ( inner_cond, inner_then, _) = then_expr. kind &&
88
+ let Some ( else_expr) = else_opt {
89
+ let mut spanless_eq = SpanlessEq :: new ( cx) ;
90
90
91
- if !spanless_eq. eq_expr ( inner_then, else_expr) {
92
- return ;
93
- }
94
-
95
- let mut cond_sugg = Sugg :: NonParen ( Cow :: Borrowed ( "" ) ) ;
91
+ if !spanless_eq. eq_expr ( inner_then, else_expr) {
92
+ return ;
93
+ }
96
94
97
- if let ExprKind :: DropTemps ( cond_expr) = cond. kind {
98
- match cond_expr. kind {
99
- ExprKind :: Binary ( bin_op, lhs, rhs) => {
100
- let new_bin_op;
95
+ let mut cond_sugg = Sugg :: NonParen ( Cow :: Borrowed ( "" ) ) ;
101
96
102
- if let BinOpKind :: Ne = bin_op. node {
103
- new_bin_op = BinOpKind :: Eq ;
104
- } else {
105
- new_bin_op = BinOpKind :: Ne ;
106
- }
97
+ if let ExprKind :: DropTemps ( cond_expr) = cond. kind {
98
+ match cond_expr. kind {
99
+ ExprKind :: Binary ( bin_op, lhs, rhs) => {
100
+ let new_bin_op;
107
101
108
- cond_sugg =
109
- make_binop ( new_bin_op, & Sugg :: hir ( cx, lhs, ".." ) , & Sugg :: hir ( cx, rhs, ".." ) ) ;
110
- } ,
111
- ExprKind :: Unary ( un_op, expr) => {
112
- if let UnOp :: Not = un_op {
113
- cond_sugg = Sugg :: hir ( cx, expr, ".." ) ;
114
- }
115
- } ,
116
- ExprKind :: Path ( _) => {
117
- cond_sugg = make_unop ( "!" , Sugg :: hir ( cx, cond_expr, ".." ) ) ;
118
- } ,
119
- _ => { } ,
120
- }
121
- } else {
122
- cond_sugg = make_unop ( "!" , Sugg :: hir ( cx, cond, ".." ) ) ;
123
- }
102
+ if let BinOpKind :: Ne = bin_op. node {
103
+ new_bin_op = BinOpKind :: Eq ;
104
+ } else {
105
+ new_bin_op = BinOpKind :: Ne ;
106
+ }
124
107
125
- span_lint_and_then (
126
- cx,
127
- COLLAPSIBLE_IF ,
128
- if_expr. span ,
129
- "this `if` statement can be collapsed." ,
130
- |diag| {
131
- diag. span_suggestion (
132
- if_expr. span ,
133
- "collapse else and nested if blocks" ,
134
- format ! (
135
- "if {} {}" ,
136
- make_binop( BinOpKind :: Or , & cond_sugg, & Sugg :: hir( cx, inner_cond, ".." ) ) ,
137
- Sugg :: hir( cx, else_expr, ".." )
138
- ) ,
139
- Applicability :: MachineApplicable ,
140
- ) ;
141
- } ,
142
- ) ;
108
+ cond_sugg =
109
+ make_binop ( new_bin_op, & Sugg :: hir ( cx, lhs, ".." ) , & Sugg :: hir ( cx, rhs, ".." ) ) ;
110
+ } ,
111
+ ExprKind :: Unary ( un_op, expr) => {
112
+ if let UnOp :: Not = un_op {
113
+ cond_sugg = Sugg :: hir ( cx, expr, ".." ) ;
143
114
}
144
- }
115
+ } ,
116
+ ExprKind :: Path ( _) => {
117
+ cond_sugg = make_unop ( "!" , Sugg :: hir ( cx, cond_expr, ".." ) ) ;
118
+ } ,
119
+ _ => { } ,
145
120
}
121
+ } else {
122
+ cond_sugg = make_unop ( "!" , Sugg :: hir ( cx, cond, ".." ) ) ;
146
123
}
124
+
125
+ span_lint_and_then (
126
+ cx,
127
+ COLLAPSIBLE_IF ,
128
+ if_expr. span ,
129
+ "this `if` statement can be collapsed." ,
130
+ |diag| {
131
+ diag. span_suggestion (
132
+ if_expr. span ,
133
+ "collapse else and nested if blocks" ,
134
+ format ! (
135
+ "if {} {}" ,
136
+ make_binop( BinOpKind :: Or , & cond_sugg, & Sugg :: hir( cx, inner_cond, ".." ) ) ,
137
+ Sugg :: hir( cx, else_expr, ".." )
138
+ ) ,
139
+ Applicability :: MachineApplicable ,
140
+ ) ;
141
+ } ,
142
+ ) ;
147
143
}
148
144
}
149
145
0 commit comments