1 // Copyright 2014-2018 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution.
4 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
5 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
6 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
7 // option. This file may not be copied, modified, or distributed
8 // except according to those terms.
11 //! Checks for if expressions that contain only an if expression.
13 //! For example, the lint would catch:
18 //! println!("Hello world");
23 //! This lint is **warn** by default
25 use crate::rustc::lint::{EarlyContext, EarlyLintPass, LintArray, LintPass};
26 use crate::rustc::{declare_tool_lint, lint_array};
27 use if_chain::if_chain;
28 use crate::syntax::ast;
30 use crate::utils::{in_macro, snippet_block, span_lint_and_sugg, span_lint_and_then};
31 use crate::utils::sugg::Sugg;
32 use crate::rustc_errors::Applicability;
34 /// **What it does:** Checks for nested `if` statements which can be collapsed
35 /// by `&&`-combining their conditions and for `else { if ... }` expressions
37 /// can be collapsed to `else if ...`.
39 /// **Why is this bad?** Each `if`-statement adds one level of nesting, which
40 /// makes code look more complex than it really is.
42 /// **Known problems:** None.
63 /// Should be written:
78 declare_clippy_lint! {
81 "`if`s that can be collapsed (e.g. `if x { if y { ... } }` and `else { if x { ... } }`)"
84 #[derive(Copy, Clone)]
85 pub struct CollapsibleIf;
87 impl LintPass for CollapsibleIf {
88 fn get_lints(&self) -> LintArray {
89 lint_array!(COLLAPSIBLE_IF)
93 impl EarlyLintPass for CollapsibleIf {
94 fn check_expr(&mut self, cx: &EarlyContext<'_>, expr: &ast::Expr) {
95 if !in_macro(expr.span) {
101 fn check_if(cx: &EarlyContext<'_>, expr: &ast::Expr) {
103 ast::ExprKind::If(ref check, ref then, ref else_) => if let Some(ref else_) = *else_ {
104 check_collapsible_maybe_if_let(cx, else_);
106 check_collapsible_no_if_let(cx, expr, check, then);
108 ast::ExprKind::IfLet(_, _, _, Some(ref else_)) => {
109 check_collapsible_maybe_if_let(cx, else_);
115 fn check_collapsible_maybe_if_let(cx: &EarlyContext<'_>, else_: &ast::Expr) {
117 if let ast::ExprKind::Block(ref block, _) = else_.node;
118 if let Some(else_) = expr_block(block);
119 if !in_macro(else_.span);
122 ast::ExprKind::If(..) | ast::ExprKind::IfLet(..) => {
123 span_lint_and_sugg(cx,
126 "this `else { if .. }` block can be collapsed",
128 snippet_block(cx, else_.span, "..").into_owned());
136 fn check_collapsible_no_if_let(cx: &EarlyContext<'_>, expr: &ast::Expr, check: &ast::Expr, then: &ast::Block) {
138 if let Some(inner) = expr_block(then);
139 if let ast::ExprKind::If(ref check_inner, ref content, None) = inner.node;
141 if expr.span.ctxt() != inner.span.ctxt() {
144 span_lint_and_then(cx, COLLAPSIBLE_IF, expr.span, "this if statement can be collapsed", |db| {
145 let lhs = Sugg::ast(cx, check, "..");
146 let rhs = Sugg::ast(cx, check_inner, "..");
147 db.span_suggestion_with_applicability(
153 snippet_block(cx, content.span, ".."),
155 Applicability::MachineApplicable, // snippet
162 /// If the block contains only one expression, return it.
163 fn expr_block(block: &ast::Block) -> Option<&ast::Expr> {
164 let mut it = block.stmts.iter();
166 if let (Some(stmt), None) = (it.next(), it.next()) {
168 ast::StmtKind::Expr(ref expr) | ast::StmtKind::Semi(ref expr) => Some(expr),