]> git.lizzy.rs Git - rust.git/blob - src/copies.rs
Fix typo
[rust.git] / src / copies.rs
1 use rustc::lint::*;
2 use rustc_front::hir::*;
3 use utils::{get_parent_expr, in_macro, is_block_equal, is_exp_equal, 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             is_block_equal(cx, &then_block, &else_block, false)
59         }
60         else {
61             false
62         };
63
64         if must_lint {
65             span_lint(cx, IF_SAME_THEN_ELSE, expr.span, "this if has the same then and else blocks");
66         }
67     }
68 }
69
70 /// Implementation of `IFS_SAME_COND`.
71 fn lint_same_cond(cx: &LateContext, expr: &Expr) {
72     // skip ifs directly in else, it will be checked in the parent if
73     if let Some(&Expr{node: ExprIf(_, _, Some(ref else_expr)), ..}) = get_parent_expr(cx, expr) {
74         if else_expr.id == expr.id {
75             return;
76         }
77     }
78
79     let conds = condition_sequence(expr);
80
81     for (n, i) in conds.iter().enumerate() {
82         for j in conds.iter().skip(n+1) {
83             if is_exp_equal(cx, i, j, true) {
84                 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");
85             }
86         }
87     }
88 }
89
90 /// Return the list of condition expressions in a sequence of `if/else`.
91 /// Eg. would return `[a, b]` for the expression `if a {..} else if b {..}`.
92 fn condition_sequence(mut expr: &Expr) -> Vec<&Expr> {
93     let mut result = vec![];
94
95     while let ExprIf(ref cond, _, ref else_expr) = expr.node {
96         result.push(&**cond);
97
98         if let Some(ref else_expr) = *else_expr {
99             expr = else_expr;
100         }
101         else {
102             break;
103         }
104     }
105
106     result
107 }