]> git.lizzy.rs Git - rust.git/blob - clippy_lints/src/needless_update.rs
Improve docs
[rust.git] / clippy_lints / 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:**
13 /// ```rust
14 /// Point { x: 1, y: 0, ..zero_point }
15 /// ```
16 declare_lint! {
17     pub NEEDLESS_UPDATE,
18     Warn,
19     "using `Foo { ..base }` when there are no missing fields"
20 }
21
22 #[derive(Copy, Clone)]
23 pub struct Pass;
24
25 impl LintPass for Pass {
26     fn get_lints(&self) -> LintArray {
27         lint_array!(NEEDLESS_UPDATE)
28     }
29 }
30
31 impl LateLintPass for Pass {
32     fn check_expr(&mut self, cx: &LateContext, expr: &Expr) {
33         if let ExprStruct(_, ref fields, Some(ref base)) = expr.node {
34             let ty = cx.tcx.expr_ty(expr);
35             if let TyStruct(def, _) = ty.sty {
36                 if fields.len() == def.struct_variant().fields.len() {
37                     span_lint(cx,
38                               NEEDLESS_UPDATE,
39                               base.span,
40                               "struct update has no effect, all the fields in the struct have already been specified");
41                 }
42             }
43         }
44     }
45 }