1 use crate::utils::span_lint_and_sugg;
2 use rustc::lint::{EarlyContext, EarlyLintPass, LintArray, LintPass};
3 use rustc::{declare_tool_lint, lint_array};
4 use rustc_errors::Applicability;
7 /// **What it does:** Checks for fields in struct literals where shorthands
10 /// **Why is this bad?** If the field and variable names are the same,
11 /// the field name is redundant.
13 /// **Known problems:** None.
17 /// let bar: u8 = 123;
23 /// let foo = Foo{ bar: bar }
25 /// the last line can be simplified to
27 /// let foo = Foo{ bar }
29 declare_clippy_lint! {
30 pub REDUNDANT_FIELD_NAMES,
32 "checks for fields in struct literals where shorthands could be used"
35 pub struct RedundantFieldNames;
37 impl LintPass for RedundantFieldNames {
38 fn get_lints(&self) -> LintArray {
39 lint_array!(REDUNDANT_FIELD_NAMES)
42 fn name(&self) -> &'static str {
47 impl EarlyLintPass for RedundantFieldNames {
48 fn check_expr(&mut self, cx: &EarlyContext<'_>, expr: &Expr) {
49 if let ExprKind::Struct(_, ref fields, _) = expr.node {
51 if field.is_shorthand {
54 if let ExprKind::Path(None, path) = &field.expr.node {
55 if path.segments.len() == 1
56 && path.segments[0].ident == field.ident
57 && path.segments[0].args.is_none()
61 REDUNDANT_FIELD_NAMES,
63 "redundant field names in struct initialization",
65 field.ident.to_string(),
66 Applicability::MachineApplicable,