2 use rustc::{declare_lint, lint_array};
4 use crate::utils::{span_lint_and_sugg};
6 /// **What it does:** Checks for fields in struct literals where shorthands
9 /// **Why is this bad?** If the field and variable names are the same,
10 /// the field name is redundant.
12 /// **Known problems:** None.
16 /// let bar: u8 = 123;
22 /// let foo = Foo{ bar: bar }
24 /// the last line can be simplified to
26 /// let foo = Foo{ bar }
28 declare_clippy_lint! {
29 pub REDUNDANT_FIELD_NAMES,
31 "checks for fields in struct literals where shorthands could be used"
34 pub struct RedundantFieldNames;
36 impl LintPass for RedundantFieldNames {
37 fn get_lints(&self) -> LintArray {
38 lint_array!(REDUNDANT_FIELD_NAMES)
42 impl EarlyLintPass for RedundantFieldNames {
43 fn check_expr(&mut self, cx: &EarlyContext<'_>, expr: &Expr) {
44 if let ExprKind::Struct(_, ref fields, _) = expr.node {
46 if field.is_shorthand {
49 if let ExprKind::Path(None, path) = &field.expr.node {
50 if path.segments.len() == 1 && path.segments[0].ident == field.ident {
53 REDUNDANT_FIELD_NAMES,
55 "redundant field names in struct initialization",
57 field.ident.to_string()