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;
16 use rustc::lint::{EarlyContext, EarlyLintPass, LintArray, LintPass};
17 use rustc::{declare_lint_pass, declare_tool_lint};
20 use crate::utils::sugg::Sugg;
22 in_macro_or_desugar, snippet_block, snippet_block_with_applicability, span_lint_and_sugg, span_lint_and_then,
24 use rustc_errors::Applicability;
26 declare_clippy_lint! {
27 /// **What it does:** Checks for nested `if` statements which can be collapsed
28 /// by `&&`-combining their conditions and for `else { if ... }` expressions
30 /// can be collapsed to `else if ...`.
32 /// **Why is this bad?** Each `if`-statement adds one level of nesting, which
33 /// makes code look more complex than it really is.
35 /// **Known problems:** None.
56 /// Should be written:
73 "`if`s that can be collapsed (e.g., `if x { if y { ... } }` and `else { if x { ... } }`)"
76 declare_lint_pass!(CollapsibleIf => [COLLAPSIBLE_IF]);
78 impl EarlyLintPass for CollapsibleIf {
79 fn check_expr(&mut self, cx: &EarlyContext<'_>, expr: &ast::Expr) {
80 if !in_macro_or_desugar(expr.span) {
86 fn check_if(cx: &EarlyContext<'_>, expr: &ast::Expr) {
88 ast::ExprKind::If(ref check, ref then, ref else_) => {
89 if let Some(ref else_) = *else_ {
90 check_collapsible_maybe_if_let(cx, else_);
92 check_collapsible_no_if_let(cx, expr, check, then);
95 ast::ExprKind::IfLet(_, _, _, Some(ref else_)) => {
96 check_collapsible_maybe_if_let(cx, else_);
102 fn block_starts_with_comment(cx: &EarlyContext<'_>, expr: &ast::Block) -> bool {
103 // We trim all opening braces and whitespaces and then check if the next string is a comment.
104 let trimmed_block_text = snippet_block(cx, expr.span, "..")
105 .trim_start_matches(|c: char| c.is_whitespace() || c == '{')
107 trimmed_block_text.starts_with("//") || trimmed_block_text.starts_with("/*")
110 fn check_collapsible_maybe_if_let(cx: &EarlyContext<'_>, else_: &ast::Expr) {
112 if let ast::ExprKind::Block(ref block, _) = else_.node;
113 if !block_starts_with_comment(cx, block);
114 if let Some(else_) = expr_block(block);
115 if !in_macro_or_desugar(else_.span);
118 ast::ExprKind::If(..) | ast::ExprKind::IfLet(..) => {
119 let mut applicability = Applicability::MachineApplicable;
124 "this `else { if .. }` block can be collapsed",
126 snippet_block_with_applicability(cx, else_.span, "..", &mut applicability).into_owned(),
136 fn check_collapsible_no_if_let(cx: &EarlyContext<'_>, expr: &ast::Expr, check: &ast::Expr, then: &ast::Block) {
138 if !block_starts_with_comment(cx, then);
139 if let Some(inner) = expr_block(then);
140 if let ast::ExprKind::If(ref check_inner, ref content, None) = inner.node;
142 if expr.span.ctxt() != inner.span.ctxt() {
145 span_lint_and_then(cx, COLLAPSIBLE_IF, expr.span, "this if statement can be collapsed", |db| {
146 let lhs = Sugg::ast(cx, check, "..");
147 let rhs = Sugg::ast(cx, check_inner, "..");
154 snippet_block(cx, content.span, ".."),
156 Applicability::MachineApplicable, // snippet
163 /// If the block contains only one expression, return it.
164 fn expr_block(block: &ast::Block) -> Option<&ast::Expr> {
165 let mut it = block.stmts.iter();
167 if let (Some(stmt), None) = (it.next(), it.next()) {
169 ast::StmtKind::Expr(ref expr) | ast::StmtKind::Semi(ref expr) => Some(expr),