]> git.lizzy.rs Git - rust.git/blob - clippy_lints/src/block_in_if_condition.rs
Rustup to rustc 1.16.0-nightly (468227129 2017-01-03): Body fixes for rustup
[rust.git] / clippy_lints / 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, NestedVisitorMap};
4 use utils::*;
5
6 /// **What it does:** Checks for `if` conditions that use blocks to contain an
7 /// expression.
8 ///
9 /// **Why is this bad?** It isn't really Rust style, same as using parentheses
10 /// to contain expressions.
11 ///
12 /// **Known problems:** None.
13 ///
14 /// **Example:**
15 /// ```rust
16 /// if { true } ..
17 /// ```
18 declare_lint! {
19     pub BLOCK_IN_IF_CONDITION_EXPR,
20     Warn,
21     "braces that can be eliminated in conditions, e.g `if { true } ...`"
22 }
23
24 /// **What it does:** Checks for `if` conditions that use blocks containing
25 /// statements, or conditions that use closures with blocks.
26 ///
27 /// **Why is this bad?** Using blocks in the condition makes it hard to read.
28 ///
29 /// **Known problems:** None.
30 ///
31 /// **Example:**
32 /// ```rust
33 /// if { let x = somefunc(); x } ..
34 /// // or
35 /// if somefunc(|x| { x == 47 }) ..
36 /// ```
37 declare_lint! {
38     pub BLOCK_IN_IF_CONDITION_STMT,
39     Warn,
40     "complex blocks in conditions, e.g. `if { let x = true; x } ...`"
41 }
42
43 #[derive(Copy,Clone)]
44 pub struct BlockInIfCondition;
45
46 impl LintPass for BlockInIfCondition {
47     fn get_lints(&self) -> LintArray {
48         lint_array!(BLOCK_IN_IF_CONDITION_EXPR, BLOCK_IN_IF_CONDITION_STMT)
49     }
50 }
51
52 struct ExVisitor<'a, 'tcx: 'a> {
53     found_block: Option<&'tcx Expr>,
54     cx: &'a LateContext<'a, 'tcx>,
55 }
56
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);
63                 return;
64             }
65         }
66         walk_expr(self, expr);
67     }
68     fn nested_visit_map<'this>(&'this mut self) -> NestedVisitorMap<'this, 'tcx> {
69         NestedVisitorMap::All(&self.cx.tcx.map)
70     }
71 }
72
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'";
76
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
85                             // the block
86                             if in_macro(cx, expr.span) || differing_macro_contexts(expr.span, ex.span) {
87                                 return;
88                             }
89                             span_help_and_lint(cx,
90                                                BLOCK_IN_IF_CONDITION_EXPR,
91                                                check.span,
92                                                BRACED_EXPR_MESSAGE,
93                                                &format!("try\nif {} {} ... ",
94                                                         snippet_block(cx, ex.span, ".."),
95                                                         snippet_block(cx, then.span, "..")));
96                         }
97                     } else {
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) {
100                             return;
101                         }
102                         // move block higher
103                         span_help_and_lint(cx,
104                                            BLOCK_IN_IF_CONDITION_STMT,
105                                            check.span,
106                                            COMPLEX_BLOCK_MESSAGE,
107                                            &format!("try\nlet res = {};\nif res {} ... ",
108                                                     snippet_block(cx, block.span, ".."),
109                                                     snippet_block(cx, then.span, "..")));
110                     }
111                 }
112             } else {
113                 let mut visitor = ExVisitor {
114                     found_block: None,
115                     cx: cx,
116                 };
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);
120                 }
121             }
122         }
123     }
124 }