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
17 use syntax::codemap::Spanned;
20 use utils::{in_macro, snippet, snippet_block, span_lint_and_then};
22 /// **What it does:** This lint checks for nested `if`-statements which can be collapsed by
23 /// `&&`-combining their conditions and for `else { if .. }` expressions that can be collapsed to
26 /// **Why is this bad?** Each `if`-statement adds one level of nesting, which makes code look more complex than it really is.
28 /// **Known problems:** None
30 /// **Example:** `if x { if y { .. } }`
34 "two nested `if`-expressions can be collapsed into one, e.g. `if x { if y { foo() } }` \
35 can be written as `if x && y { foo() }` \
36 and an `else { if .. }` expression can be collapsed to \
41 pub struct CollapsibleIf;
43 impl LintPass for CollapsibleIf {
44 fn get_lints(&self) -> LintArray {
45 lint_array!(COLLAPSIBLE_IF)
49 impl EarlyLintPass for CollapsibleIf {
50 fn check_expr(&mut self, cx: &EarlyContext, expr: &ast::Expr) {
51 if !in_macro(cx, expr.span) {
57 fn check_if(cx: &EarlyContext, expr: &ast::Expr) {
59 ast::ExprKind::If(ref check, ref then, ref else_) => {
60 if let Some(ref else_) = *else_ {
61 check_collapsible_maybe_if_let(cx, else_);
63 check_collapsible_no_if_let(cx, expr, check, then);
66 ast::ExprKind::IfLet(_, _, _, Some(ref else_)) => {
67 check_collapsible_maybe_if_let(cx, else_);
73 fn check_collapsible_maybe_if_let(cx: &EarlyContext, else_: &ast::Expr) {
75 let ast::ExprKind::Block(ref block) = else_.node,
76 let Some(ref else_) = expr_block(block),
77 !in_macro(cx, else_.span),
80 ast::ExprKind::If(..) | ast::ExprKind::IfLet(..) => {
81 span_lint_and_then(cx,
84 "this `else { if .. }` block can be collapsed", |db| {
85 db.span_suggestion(block.span, "try", snippet_block(cx, else_.span, "..").into_owned());
93 fn check_collapsible_no_if_let(
100 let Some(inner) = expr_block(then),
101 let ast::ExprKind::If(ref check_inner, ref content, None) = inner.node,
103 if expr.span.expn_id != inner.span.expn_id {
106 span_lint_and_then(cx, COLLAPSIBLE_IF, expr.span, "this if statement can be collapsed", |db| {
107 db.span_suggestion(expr.span,
109 format!("if {} && {} {}",
110 check_to_string(cx, check),
111 check_to_string(cx, check_inner),
112 snippet_block(cx, content.span, "..")));
117 fn requires_brackets(e: &ast::Expr) -> bool {
119 ast::ExprKind::Binary(Spanned { node: n, .. }, _, _) if n == ast::BinOpKind::Eq => false,
124 fn check_to_string(cx: &EarlyContext, e: &ast::Expr) -> Cow<'static, str> {
125 if requires_brackets(e) {
126 format!("({})", snippet(cx, e.span, "..")).into()
128 snippet(cx, e.span, "..")
132 /// If the block contains only one expression, returns it.
133 fn expr_block(block: &ast::Block) -> Option<&ast::Expr> {
134 let mut it = block.stmts.iter();
136 if let (Some(stmt), None) = (it.next(), it.next()) {
138 ast::StmtKind::Expr(ref expr) | ast::StmtKind::Semi(ref expr) => Some(expr),