1 use crate::utils::span_lint;
2 use rustc_hir::{Expr, ExprKind};
3 use rustc_lint::{LateContext, LateLintPass};
5 use rustc_session::{declare_lint_pass, declare_tool_lint};
8 /// **What it does:** Checks for needlessly including a base struct on update
9 /// when all fields are changed anyway.
11 /// **Why is this bad?** This will cost resources (because the base has to be
12 /// somewhere), and make the code less readable.
14 /// **Known problems:** None.
23 /// # let zero_point = Point { x: 0, y: 0, z: 0 };
25 /// # #[non_exhaustive]
26 /// # struct Options {
30 /// # let default_options = Options { a: false, b: 0 };
47 /// // this lint is not applied to structs marked with [non_exhaustive](https://doc.rust-lang.org/reference/attributes/type_system.html)
57 "using `Foo { ..base }` when there are no missing fields"
60 declare_lint_pass!(NeedlessUpdate => [NEEDLESS_UPDATE]);
62 impl<'tcx> LateLintPass<'tcx> for NeedlessUpdate {
63 fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) {
64 if let ExprKind::Struct(_, ref fields, Some(ref base)) = expr.kind {
65 let ty = cx.typeck_results().expr_ty(expr);
66 if let ty::Adt(def, _) = ty.kind() {
67 if fields.len() == def.non_enum_variant().fields.len()
68 && !def.variants[0_usize.into()].is_field_list_non_exhaustive()
74 "struct update has no effect, all the fields in the struct have already been specified",