Skip to content

Commit 02d9a8e

Browse files
committed
Collapse nested if-statements.
1 parent 7be3e11 commit 02d9a8e

File tree

1 file changed

+51
-55
lines changed

1 file changed

+51
-55
lines changed

clippy_lints/src/collapsible_if.rs

Lines changed: 51 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -81,69 +81,65 @@ declare_clippy_lint! {
8181
}
8282

8383
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);
9090

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+
}
9694

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(""));
10196

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;
107101

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+
}
124107

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, "..");
143114
}
144-
}
115+
},
116+
ExprKind::Path(_) => {
117+
cond_sugg = make_unop("!", Sugg::hir(cx, cond_expr, ".."));
118+
},
119+
_ => {},
145120
}
121+
} else {
122+
cond_sugg = make_unop("!", Sugg::hir(cx, cond, ".."));
146123
}
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+
);
147143
}
148144
}
149145

0 commit comments

Comments
 (0)