]> git.lizzy.rs Git - rust.git/blob - src/copies.rs
Lint ifs with the same then and else blocks
[rust.git] / src / copies.rs
1 use rustc::lint::*;
2 use rustc_front::hir::*;
3 use utils::{get_parent_expr, in_macro, is_exp_equal, is_stmt_equal, over, span_lint, span_note_and_lint};
4
5 /// **What it does:** This lint checks for consecutive `ifs` with the same condition. This lint is
6 /// `Warn` by default.
7 ///
8 /// **Why is this bad?** This is probably a copy & paste error.
9 ///
10 /// **Known problems:** Hopefully none.
11 ///
12 /// **Example:** `if a == b { .. } else if a == b { .. }`
13 declare_lint! {
14     pub IFS_SAME_COND,
15     Warn,
16     "consecutive `ifs` with the same condition"
17 }
18
19 /// **What it does:** This lint checks for `if/else` with the same body as the *then* part and the
20 /// *else* part. This lint is `Warn` by default.
21 ///
22 /// **Why is this bad?** This is probably a copy & paste error.
23 ///
24 /// **Known problems:** Hopefully none.
25 ///
26 /// **Example:** `if .. { 42 } else { 42 }`
27 declare_lint! {
28     pub IF_SAME_THEN_ELSE,
29     Warn,
30     "if with the same *then* and *else* blocks"
31 }
32
33 #[derive(Copy, Clone, Debug)]
34 pub struct CopyAndPaste;
35
36 impl LintPass for CopyAndPaste {
37     fn get_lints(&self) -> LintArray {
38         lint_array![
39             IFS_SAME_COND,
40             IF_SAME_THEN_ELSE
41         ]
42     }
43 }
44
45 impl LateLintPass for CopyAndPaste {
46     fn check_expr(&mut self, cx: &LateContext, expr: &Expr) {
47         if !in_macro(cx, expr.span) {
48             lint_same_then_else(cx, expr);
49             lint_same_cond(cx, expr);
50         }
51     }
52 }
53
54 /// Implementation of `IF_SAME_THEN_ELSE`.
55 fn lint_same_then_else(cx: &LateContext, expr: &Expr) {
56     if let ExprIf(_, ref then_block, Some(ref else_expr)) = expr.node {
57         let must_lint = if let ExprBlock(ref else_block) = else_expr.node {
58             over(&then_block.stmts, &else_block.stmts, |l, r| is_stmt_equal(cx, l, r)) &&
59                 match (&then_block.expr, &else_block.expr) {
60                     (&Some(ref then_expr), &Some(ref else_expr)) => {
61                         is_exp_equal(cx, &then_expr, &else_expr)
62                     }
63                     (&None, &None) => true,
64                     _ => false,
65                 }
66         }
67         else {
68             false
69         };
70
71         if must_lint {
72             span_lint(cx, IF_SAME_THEN_ELSE, expr.span, "this if has the same then and else blocks");
73         }
74     }
75 }
76
77 /// Implementation of `IFS_SAME_COND`.
78 fn lint_same_cond(cx: &LateContext, expr: &Expr) {
79     // skip ifs directly in else, it will be checked in the parent if
80     if let Some(&Expr{node: ExprIf(_, _, Some(ref else_expr)), ..}) = get_parent_expr(cx, expr) {
81         if else_expr.id == expr.id {
82             return;
83         }
84     }
85
86     let conds = condition_sequence(expr);
87
88     for (n, i) in conds.iter().enumerate() {
89         for j in conds.iter().skip(n+1) {
90             if is_exp_equal(cx, i, j) {
91                 span_note_and_lint(cx, IFS_SAME_COND, j.span, "this if has the same condition as a previous if", i.span, "same as this");
92             }
93         }
94     }
95 }
96
97 /// Return the list of conditions expression in a sequence of `if/else`.
98 /// Eg. would return `[a, b]` for the expression `if a {..} else if b {..}`.
99 fn condition_sequence(mut expr: &Expr) -> Vec<&Expr> {
100     let mut result = vec![];
101
102     while let ExprIf(ref cond, _, ref else_expr) = expr.node {
103         result.push(&**cond);
104
105         if let Some(ref else_expr) = *else_expr {
106             expr = else_expr;
107         }
108         else {
109             break;
110         }
111     }
112
113     result
114 }