]> git.lizzy.rs Git - rust.git/blob - src/temporary_assignment.rs
Remove * dep
[rust.git] / src / temporary_assignment.rs
1 use rustc::lint::{LateContext, LateLintPass, LintArray, LintPass};
2 use rustc_front::hir::{Expr, ExprAssign, ExprField, ExprStruct, ExprTup, ExprTupField};
3
4 use utils::is_adjusted;
5 use utils::span_lint;
6
7 /// **What it does:** This lint `Warn`s on creating a struct or tuple just to assign a value in it.
8 ///
9 /// **Why is this bad?** Readability. If the struct is only created to be updated, why not write the struct you want in the first place?
10 ///
11 /// **Known problems:** None.
12 ///
13 /// **Example:** `(0, 0).0 = 1`
14 declare_lint! {
15     pub TEMPORARY_ASSIGNMENT,
16     Warn,
17     "assignments to temporaries"
18 }
19
20 fn is_temporary(expr: &Expr) -> bool {
21     match expr.node {
22         ExprStruct(..) |
23         ExprTup(..) => true,
24         _ => false,
25     }
26 }
27
28 #[derive(Copy, Clone)]
29 pub struct TemporaryAssignmentPass;
30
31 impl LintPass for TemporaryAssignmentPass {
32     fn get_lints(&self) -> LintArray {
33         lint_array!(TEMPORARY_ASSIGNMENT)
34     }
35 }
36
37 impl LateLintPass for TemporaryAssignmentPass {
38     fn check_expr(&mut self, cx: &LateContext, expr: &Expr) {
39         if let ExprAssign(ref target, _) = expr.node {
40             match target.node {
41                 ExprField(ref base, _) | ExprTupField(ref base, _) => {
42                     if is_temporary(base) && !is_adjusted(cx, base) {
43                         span_lint(cx, TEMPORARY_ASSIGNMENT, expr.span,
44                                   "assignment to temporary");
45                     }
46                 }
47                 _ => ()
48             }
49         }
50     }
51 }