1 use crate::utils::span_lint_and_sugg;
2 use rustc_ast::ast::{Expr, ExprKind};
3 use rustc_errors::Applicability;
4 use rustc_lint::{EarlyContext, EarlyLintPass};
5 use rustc_session::{declare_lint_pass, declare_tool_lint};
8 /// **What it does:** Checks for fields in struct literals where shorthands
11 /// **Why is this bad?** If the field and variable names are the same,
12 /// the field name is redundant.
14 /// **Known problems:** None.
18 /// let bar: u8 = 123;
24 /// let foo = Foo { bar: bar };
26 /// the last line can be simplified to
28 /// let foo = Foo { bar };
30 pub REDUNDANT_FIELD_NAMES,
32 "checks for fields in struct literals where shorthands could be used"
35 declare_lint_pass!(RedundantFieldNames => [REDUNDANT_FIELD_NAMES]);
37 impl EarlyLintPass for RedundantFieldNames {
38 fn check_expr(&mut self, cx: &EarlyContext<'_>, expr: &Expr) {
39 if let ExprKind::Struct(_, ref fields, _) = expr.kind {
41 if field.is_shorthand {
44 if let ExprKind::Path(None, path) = &field.expr.kind {
45 if path.segments.len() == 1
46 && path.segments[0].ident == field.ident
47 && path.segments[0].args.is_none()
51 REDUNDANT_FIELD_NAMES,
53 "redundant field names in struct initialization",
55 field.ident.to_string(),
56 Applicability::MachineApplicable,