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.
12 use crate::rustc::lint::{LateContext, LateLintPass, LintArray, LintPass};
13 use crate::rustc::{declare_tool_lint, lint_array};
14 use crate::rustc::hir::*;
15 use crate::rustc::hir::intravisit::{walk_expr, NestedVisitorMap, Visitor};
18 /// **What it does:** Checks for `if` conditions that use blocks to contain an
21 /// **Why is this bad?** It isn't really Rust style, same as using parentheses
22 /// to contain expressions.
24 /// **Known problems:** None.
30 declare_clippy_lint! {
31 pub BLOCK_IN_IF_CONDITION_EXPR,
33 "braces that can be eliminated in conditions, e.g. `if { true } ...`"
36 /// **What it does:** Checks for `if` conditions that use blocks containing
37 /// statements, or conditions that use closures with blocks.
39 /// **Why is this bad?** Using blocks in the condition makes it hard to read.
41 /// **Known problems:** None.
45 /// if { let x = somefunc(); x } ..
47 /// if somefunc(|x| { x == 47 }) ..
49 declare_clippy_lint! {
50 pub BLOCK_IN_IF_CONDITION_STMT,
52 "complex blocks in conditions, e.g. `if { let x = true; x } ...`"
55 #[derive(Copy, Clone)]
56 pub struct BlockInIfCondition;
58 impl LintPass for BlockInIfCondition {
59 fn get_lints(&self) -> LintArray {
60 lint_array!(BLOCK_IN_IF_CONDITION_EXPR, BLOCK_IN_IF_CONDITION_STMT)
64 struct ExVisitor<'a, 'tcx: 'a> {
65 found_block: Option<&'tcx Expr>,
66 cx: &'a LateContext<'a, 'tcx>,
69 impl<'a, 'tcx: 'a> Visitor<'tcx> for ExVisitor<'a, 'tcx> {
70 fn visit_expr(&mut self, expr: &'tcx Expr) {
71 if let ExprKind::Closure(_, _, eid, _, _) = expr.node {
72 let body = self.cx.tcx.hir.body(eid);
74 if matches!(ex.node, ExprKind::Block(_, _)) {
75 self.found_block = Some(ex);
79 walk_expr(self, expr);
81 fn nested_visit_map<'this>(&'this mut self) -> NestedVisitorMap<'this, 'tcx> {
82 NestedVisitorMap::None
86 const BRACED_EXPR_MESSAGE: &str = "omit braces around single expression condition";
87 const COMPLEX_BLOCK_MESSAGE: &str = "in an 'if' condition, avoid complex blocks or closures with blocks; \
88 instead, move the block or closure higher and bind it with a 'let'";
90 impl<'a, 'tcx> LateLintPass<'a, 'tcx> for BlockInIfCondition {
91 fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, expr: &'tcx Expr) {
92 if let ExprKind::If(ref check, ref then, _) = expr.node {
93 if let ExprKind::Block(ref block, _) = check.node {
94 if block.rules == DefaultBlock {
95 if block.stmts.is_empty() {
96 if let Some(ref ex) = block.expr {
97 // don't dig into the expression here, just suggest that they remove
99 if in_macro(expr.span) || differing_macro_contexts(expr.span, ex.span) {
104 BLOCK_IN_IF_CONDITION_EXPR,
108 "try\nif {} {} ... ",
109 snippet_block(cx, ex.span, ".."),
110 snippet_block(cx, then.span, "..")
118 .map_or_else(|| block.stmts[0].span, |e| e.span);
119 if in_macro(span) || differing_macro_contexts(expr.span, span) {
125 BLOCK_IN_IF_CONDITION_STMT,
127 COMPLEX_BLOCK_MESSAGE,
129 "try\nlet res = {};\nif res {} ... ",
130 snippet_block(cx, block.span, ".."),
131 snippet_block(cx, then.span, "..")
137 let mut visitor = ExVisitor {
141 walk_expr(&mut visitor, check);
142 if let Some(block) = visitor.found_block {
143 span_lint(cx, BLOCK_IN_IF_CONDITION_STMT, block.span, COMPLEX_BLOCK_MESSAGE);