1 use rustc::lint::{LateContext, LateLintPass, LintArray, LintPass};
3 use rustc::hir::intravisit::{walk_expr, NestedVisitorMap, Visitor};
6 /// **What it does:** Checks for `if` conditions that use blocks to contain an
9 /// **Why is this bad?** It isn't really Rust style, same as using parentheses
10 /// to contain expressions.
12 /// **Known problems:** None.
18 declare_clippy_lint! {
19 pub BLOCK_IN_IF_CONDITION_EXPR,
21 "braces that can be eliminated in conditions, e.g. `if { true } ...`"
24 /// **What it does:** Checks for `if` conditions that use blocks containing
25 /// statements, or conditions that use closures with blocks.
27 /// **Why is this bad?** Using blocks in the condition makes it hard to read.
29 /// **Known problems:** None.
33 /// if { let x = somefunc(); x } ..
35 /// if somefunc(|x| { x == 47 }) ..
37 declare_clippy_lint! {
38 pub BLOCK_IN_IF_CONDITION_STMT,
40 "complex blocks in conditions, e.g. `if { let x = true; x } ...`"
43 #[derive(Copy, Clone)]
44 pub struct BlockInIfCondition;
46 impl LintPass for BlockInIfCondition {
47 fn get_lints(&self) -> LintArray {
48 lint_array!(BLOCK_IN_IF_CONDITION_EXPR, BLOCK_IN_IF_CONDITION_STMT)
52 struct ExVisitor<'a, 'tcx: 'a> {
53 found_block: Option<&'tcx Expr>,
54 cx: &'a LateContext<'a, 'tcx>,
57 impl<'a, 'tcx: 'a> Visitor<'tcx> for ExVisitor<'a, 'tcx> {
58 fn visit_expr(&mut self, expr: &'tcx Expr) {
59 if let ExprKind::Closure(_, _, eid, _, _) = expr.node {
60 let body = self.cx.tcx.hir.body(eid);
62 if matches!(ex.node, ExprKind::Block(_, _)) {
63 self.found_block = Some(ex);
67 walk_expr(self, expr);
69 fn nested_visit_map<'this>(&'this mut self) -> NestedVisitorMap<'this, 'tcx> {
70 NestedVisitorMap::None
74 const BRACED_EXPR_MESSAGE: &str = "omit braces around single expression condition";
75 const COMPLEX_BLOCK_MESSAGE: &str = "in an 'if' condition, avoid complex blocks or closures with blocks; \
76 instead, move the block or closure higher and bind it with a 'let'";
78 impl<'a, 'tcx> LateLintPass<'a, 'tcx> for BlockInIfCondition {
79 fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, expr: &'tcx Expr) {
80 if let ExprKind::If(ref check, ref then, _) = expr.node {
81 if let ExprKind::Block(ref block, _) = check.node {
82 if block.rules == DefaultBlock {
83 if block.stmts.is_empty() {
84 if let Some(ref ex) = block.expr {
85 // don't dig into the expression here, just suggest that they remove
87 if in_macro(expr.span) || differing_macro_contexts(expr.span, ex.span) {
92 BLOCK_IN_IF_CONDITION_EXPR,
97 snippet_block(cx, ex.span, ".."),
98 snippet_block(cx, then.span, "..")
106 .map_or_else(|| block.stmts[0].span, |e| e.span);
107 if in_macro(span) || differing_macro_contexts(expr.span, span) {
113 BLOCK_IN_IF_CONDITION_STMT,
115 COMPLEX_BLOCK_MESSAGE,
117 "try\nlet res = {};\nif res {} ... ",
118 snippet_block(cx, block.span, ".."),
119 snippet_block(cx, then.span, "..")
125 let mut visitor = ExVisitor {
129 walk_expr(&mut visitor, check);
130 if let Some(block) = visitor.found_block {
131 span_lint(cx, BLOCK_IN_IF_CONDITION_STMT, block.span, COMPLEX_BLOCK_MESSAGE);