1 use rustc::lint::{LateContext, LateLintPass, LintArray, LintPass};
2 use rustc::middle::ty::TyStruct;
3 use rustc_front::hir::{Expr, ExprStruct};
7 /// **What it does:** This lint `Warn`s on needlessly including a base struct on update when all fields are changed anyway.
9 /// **Why is this bad?** This will cost resources (because the base has to be somewhere), and make the code less readable.
11 /// **Known problems:** None.
13 /// **Example:** `Point { x: 1, y: 0, ..zero_point }`
17 "using `{ ..base }` when there are no missing fields"
20 #[derive(Copy, Clone)]
21 pub struct NeedlessUpdatePass;
23 impl LintPass for NeedlessUpdatePass {
24 fn get_lints(&self) -> LintArray {
25 lint_array!(NEEDLESS_UPDATE)
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");