]> git.lizzy.rs Git - rust.git/blob - src/needless_update.rs
Remove * dep
[rust.git] / src / needless_update.rs
1 use rustc::lint::{LateContext, LateLintPass, LintArray, LintPass};
2 use rustc::middle::ty::TyStruct;
3 use rustc_front::hir::{Expr, ExprStruct};
4
5 use utils::span_lint;
6
7 /// **What it does:** This lint `Warn`s on needlessly including a base struct on update when all fields are changed anyway.
8 ///
9 /// **Why is this bad?** This will cost resources (because the base has to be somewhere), and make the code less readable.
10 ///
11 /// **Known problems:** None.
12 ///
13 /// **Example:** `Point { x: 1, y: 0, ..zero_point }`
14 declare_lint! {
15     pub NEEDLESS_UPDATE,
16     Warn,
17     "using `{ ..base }` when there are no missing fields"
18 }
19
20 #[derive(Copy, Clone)]
21 pub struct NeedlessUpdatePass;
22
23 impl LintPass for NeedlessUpdatePass {
24     fn get_lints(&self) -> LintArray {
25         lint_array!(NEEDLESS_UPDATE)
26     }
27 }
28
29 impl LateLintPass for NeedlessUpdatePass {
30     fn check_expr(&mut self, cx: &LateContext, expr: &Expr) {
31         if let ExprStruct(_, ref fields, Some(ref base)) = expr.node {
32             let ty = cx.tcx.expr_ty(expr);
33             if let TyStruct(def, _) = ty.sty {
34                 if fields.len() == def.struct_variant().fields.len() {
35                     span_lint(cx, NEEDLESS_UPDATE, base.span,
36                               "struct update has no effect, all the fields \
37                               in the struct have already been specified");
38                 }
39             }
40         }
41     }
42 }