1 use rustc::lint::{LateLintPass, LateContext, LintArray, LintPass};
3 use rustc::hir::intravisit::{Visitor, walk_expr};
6 /// **What it does:** This lint checks for `if` conditions that use blocks to contain an expression.
8 /// **Why is this bad?** It isn't really rust style, same as using parentheses to contain expressions.
10 /// **Known problems:** None
12 /// **Example:** `if { true } ..`
14 pub BLOCK_IN_IF_CONDITION_EXPR, Warn,
15 "braces can be eliminated in conditions that are expressions, e.g `if { true } ...`"
18 /// **What it does:** This lint checks for `if` conditions that use blocks containing statements, or conditions that use closures with blocks.
20 /// **Why is this bad?** Using blocks in the condition makes it hard to read.
22 /// **Known problems:** None
24 /// **Example:** `if { let x = somefunc(); x } ..` or `if somefunc(|x| { x == 47 }) ..`
26 pub BLOCK_IN_IF_CONDITION_STMT, Warn,
27 "avoid complex blocks in conditions, instead move the block higher and bind it \
28 with 'let'; e.g: `if { let x = true; x } ...`"
32 pub struct BlockInIfCondition;
34 impl LintPass for BlockInIfCondition {
35 fn get_lints(&self) -> LintArray {
36 lint_array!(BLOCK_IN_IF_CONDITION_EXPR, BLOCK_IN_IF_CONDITION_STMT)
40 struct ExVisitor<'v> {
41 found_block: Option<&'v Expr>,
44 impl<'v> Visitor<'v> for ExVisitor<'v> {
45 fn visit_expr(&mut self, expr: &'v Expr) {
46 if let ExprClosure(_, _, ref block, _) = expr.node {
48 if block.stmts.is_empty() {
49 if let Some(ref ex) = block.expr {
62 self.found_block = Some(&expr);
66 walk_expr(self, expr);
70 const BRACED_EXPR_MESSAGE: &'static str = "omit braces around single expression condition";
71 const COMPLEX_BLOCK_MESSAGE: &'static str = "in an 'if' condition, avoid complex blocks or closures with blocks; \
72 instead, move the block or closure higher and bind it with a 'let'";
74 impl LateLintPass for BlockInIfCondition {
75 fn check_expr(&mut self, cx: &LateContext, expr: &Expr) {
76 if let ExprIf(ref check, ref then, _) = expr.node {
77 if let ExprBlock(ref block) = check.node {
78 if block.rules == DefaultBlock {
79 if block.stmts.is_empty() {
80 if let Some(ref ex) = block.expr {
81 // don't dig into the expression here, just suggest that they remove
83 if in_macro(cx, expr.span) || differing_macro_contexts(expr.span, ex.span) {
86 span_help_and_lint(cx,
87 BLOCK_IN_IF_CONDITION_EXPR,
90 &format!("try\nif {} {} ... ",
91 snippet_block(cx, ex.span, ".."),
92 snippet_block(cx, then.span, "..")));
95 let span = block.expr.as_ref().map_or_else(|| block.stmts[0].span, |e| e.span);
96 if in_macro(cx, span) || differing_macro_contexts(expr.span, span) {
100 span_help_and_lint(cx,
101 BLOCK_IN_IF_CONDITION_STMT,
103 COMPLEX_BLOCK_MESSAGE,
104 &format!("try\nlet res = {};\nif res {} ... ",
105 snippet_block(cx, block.span, ".."),
106 snippet_block(cx, then.span, "..")));
110 let mut visitor = ExVisitor { found_block: None };
111 walk_expr(&mut visitor, check);
112 if let Some(ref block) = visitor.found_block {
113 span_help_and_lint(cx, BLOCK_IN_IF_CONDITION_STMT, block.span, COMPLEX_BLOCK_MESSAGE, "");