1 //! Checks for if expressions that contain only an if expression.
3 //! For example, the lint would catch:
8 //! println!("Hello world");
13 //! This lint is **warn** by default
15 use if_chain::if_chain;
17 use rustc_lint::{EarlyContext, EarlyLintPass};
18 use rustc_session::{declare_lint_pass, declare_tool_lint};
20 use crate::utils::sugg::Sugg;
21 use crate::utils::{snippet_block, snippet_block_with_applicability, span_lint_and_sugg, span_lint_and_then};
22 use rustc_errors::Applicability;
24 declare_clippy_lint! {
25 /// **What it does:** Checks for nested `if` statements which can be collapsed
26 /// by `&&`-combining their conditions and for `else { if ... }` expressions
28 /// can be collapsed to `else if ...`.
30 /// **Why is this bad?** Each `if`-statement adds one level of nesting, which
31 /// makes code look more complex than it really is.
33 /// **Known problems:** None.
54 /// Should be written:
71 "`if`s that can be collapsed (e.g., `if x { if y { ... } }` and `else { if x { ... } }`)"
74 declare_lint_pass!(CollapsibleIf => [COLLAPSIBLE_IF]);
76 impl EarlyLintPass for CollapsibleIf {
77 fn check_expr(&mut self, cx: &EarlyContext<'_>, expr: &ast::Expr) {
78 if !expr.span.from_expansion() {
84 fn check_if(cx: &EarlyContext<'_>, expr: &ast::Expr) {
85 if let ast::ExprKind::If(check, then, else_) = &expr.kind {
86 if let Some(else_) = else_ {
87 check_collapsible_maybe_if_let(cx, else_);
88 } else if let ast::ExprKind::Let(..) = check.kind {
89 // Prevent triggering on `if let a = b { if c { .. } }`.
91 check_collapsible_no_if_let(cx, expr, check, then);
96 fn block_starts_with_comment(cx: &EarlyContext<'_>, expr: &ast::Block) -> bool {
97 // We trim all opening braces and whitespaces and then check if the next string is a comment.
98 let trimmed_block_text = snippet_block(cx, expr.span, "..", None)
99 .trim_start_matches(|c: char| c.is_whitespace() || c == '{')
101 trimmed_block_text.starts_with("//") || trimmed_block_text.starts_with("/*")
104 fn check_collapsible_maybe_if_let(cx: &EarlyContext<'_>, else_: &ast::Expr) {
106 if let ast::ExprKind::Block(ref block, _) = else_.kind;
107 if !block_starts_with_comment(cx, block);
108 if let Some(else_) = expr_block(block);
109 if !else_.span.from_expansion();
110 if let ast::ExprKind::If(..) = else_.kind;
112 let mut applicability = Applicability::MachineApplicable;
117 "this `else { if .. }` block can be collapsed",
119 snippet_block_with_applicability(cx, else_.span, "..", Some(block.span), &mut applicability).into_owned(),
126 fn check_collapsible_no_if_let(cx: &EarlyContext<'_>, expr: &ast::Expr, check: &ast::Expr, then: &ast::Block) {
128 if !block_starts_with_comment(cx, then);
129 if let Some(inner) = expr_block(then);
130 if let ast::ExprKind::If(ref check_inner, ref content, None) = inner.kind;
132 if let ast::ExprKind::Let(..) = check_inner.kind {
133 // Prevent triggering on `if c { if let a = b { .. } }`.
137 if expr.span.ctxt() != inner.span.ctxt() {
140 span_lint_and_then(cx, COLLAPSIBLE_IF, expr.span, "this `if` statement can be collapsed", |diag| {
141 let lhs = Sugg::ast(cx, check, "..");
142 let rhs = Sugg::ast(cx, check_inner, "..");
143 diag.span_suggestion(
149 snippet_block(cx, content.span, "..", Some(expr.span)),
151 Applicability::MachineApplicable, // snippet
158 /// If the block contains only one expression, return it.
159 fn expr_block(block: &ast::Block) -> Option<&ast::Expr> {
160 let mut it = block.stmts.iter();
162 if let (Some(stmt), None) = (it.next(), it.next()) {
164 ast::StmtKind::Expr(ref expr) | ast::StmtKind::Semi(ref expr) => Some(expr),