]> git.lizzy.rs Git - rust.git/blob - src/block_in_if_condition.rs
doc markdown lint's span shows the line instead of the item
[rust.git] / src / block_in_if_condition.rs
1 use rustc::lint::{LateLintPass, LateContext, LintArray, LintPass};
2 use rustc::hir::*;
3 use rustc::hir::intravisit::{Visitor, walk_expr};
4 use utils::*;
5
6 /// **What it does:** This lint checks for `if` conditions that use blocks to contain an expression.
7 ///
8 /// **Why is this bad?** It isn't really rust style, same as using parentheses to contain expressions.
9 ///
10 /// **Known problems:** None
11 ///
12 /// **Example:** `if { true } ..`
13 declare_lint! {
14     pub BLOCK_IN_IF_CONDITION_EXPR, Warn,
15     "braces can be eliminated in conditions that are expressions, e.g `if { true } ...`"
16 }
17
18 /// **What it does:** This lint checks for `if` conditions that use blocks containing statements, or conditions that use closures with blocks.
19 ///
20 /// **Why is this bad?** Using blocks in the condition makes it hard to read.
21 ///
22 /// **Known problems:** None
23 ///
24 /// **Example:** `if { let x = somefunc(); x } ..` or `if somefunc(|x| { x == 47 }) ..`
25 declare_lint! {
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 } ...`"
29 }
30
31 #[derive(Copy,Clone)]
32 pub struct BlockInIfCondition;
33
34 impl LintPass for BlockInIfCondition {
35     fn get_lints(&self) -> LintArray {
36         lint_array!(BLOCK_IN_IF_CONDITION_EXPR, BLOCK_IN_IF_CONDITION_STMT)
37     }
38 }
39
40 struct ExVisitor<'v> {
41     found_block: Option<&'v Expr>,
42 }
43
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 {
47             let complex = {
48                 if block.stmts.is_empty() {
49                     if let Some(ref ex) = block.expr {
50                         match ex.node {
51                             ExprBlock(_) => true,
52                             _ => false,
53                         }
54                     } else {
55                         false
56                     }
57                 } else {
58                     true
59                 }
60             };
61             if complex {
62                 self.found_block = Some(&expr);
63                 return;
64             }
65         }
66         walk_expr(self, expr);
67     }
68 }
69
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'";
73
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
82                             // the block
83                             if in_macro(cx, expr.span) || differing_macro_contexts(expr.span, ex.span) {
84                                 return;
85                             }
86                             span_help_and_lint(cx,
87                                                BLOCK_IN_IF_CONDITION_EXPR,
88                                                check.span,
89                                                BRACED_EXPR_MESSAGE,
90                                                &format!("try\nif {} {} ... ",
91                                                         snippet_block(cx, ex.span, ".."),
92                                                         snippet_block(cx, then.span, "..")));
93                         }
94                     } else {
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) {
97                             return;
98                         }
99                         // move block higher
100                         span_help_and_lint(cx,
101                                            BLOCK_IN_IF_CONDITION_STMT,
102                                            check.span,
103                                            COMPLEX_BLOCK_MESSAGE,
104                                            &format!("try\nlet res = {};\nif res {} ... ",
105                                                     snippet_block(cx, block.span, ".."),
106                                                     snippet_block(cx, then.span, "..")));
107                     }
108                 }
109             } else {
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, "");
114                 }
115             }
116         }
117     }
118 }