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