1 use rustc::lint::{LateLintPass, LateContext, LintArray, LintPass};
3 use rustc::hir::intravisit::{Visitor, walk_expr, NestedVisitorMap};
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.
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 }) ..
38 pub BLOCK_IN_IF_CONDITION_STMT,
40 "complex blocks in conditions, e.g. `if { let x = true; x } ...`"
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 ExprClosure(_, _, eid, _) = expr.node {
60 let expr = self.cx.tcx.map.body(eid).value;
61 if matches!(expr.node, ExprBlock(_)) {
62 self.found_block = Some(&expr);
66 walk_expr(self, expr);
68 fn nested_visit_map<'this>(&'this mut self) -> NestedVisitorMap<'this, 'tcx> {
69 NestedVisitorMap::All(&self.cx.tcx.map)
73 const BRACED_EXPR_MESSAGE: &'static str = "omit braces around single expression condition";
74 const COMPLEX_BLOCK_MESSAGE: &'static str = "in an 'if' condition, avoid complex blocks or closures with blocks; \
75 instead, move the block or closure higher and bind it with a 'let'";
77 impl<'a, 'tcx> LateLintPass<'a, 'tcx> for BlockInIfCondition {
78 fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, expr: &'tcx Expr) {
79 if let ExprIf(ref check, ref then, _) = expr.node {
80 if let ExprBlock(ref block) = check.node {
81 if block.rules == DefaultBlock {
82 if block.stmts.is_empty() {
83 if let Some(ref ex) = block.expr {
84 // don't dig into the expression here, just suggest that they remove
86 if in_macro(cx, expr.span) || differing_macro_contexts(expr.span, ex.span) {
89 span_help_and_lint(cx,
90 BLOCK_IN_IF_CONDITION_EXPR,
93 &format!("try\nif {} {} ... ",
94 snippet_block(cx, ex.span, ".."),
95 snippet_block(cx, then.span, "..")));
98 let span = block.expr.as_ref().map_or_else(|| block.stmts[0].span, |e| e.span);
99 if in_macro(cx, span) || differing_macro_contexts(expr.span, span) {
103 span_help_and_lint(cx,
104 BLOCK_IN_IF_CONDITION_STMT,
106 COMPLEX_BLOCK_MESSAGE,
107 &format!("try\nlet res = {};\nif res {} ... ",
108 snippet_block(cx, block.span, ".."),
109 snippet_block(cx, then.span, "..")));
113 let mut visitor = ExVisitor {
117 walk_expr(&mut visitor, check);
118 if let Some(block) = visitor.found_block {
119 span_lint(cx, BLOCK_IN_IF_CONDITION_STMT, block.span, COMPLEX_BLOCK_MESSAGE);