]> git.lizzy.rs Git - rust.git/blob - src/tools/clippy/clippy_lints/src/redundant_field_names.rs
Auto merge of #105920 - MarcusCalhoun-Lopez:respect_set, r=jyn514
[rust.git] / src / tools / clippy / clippy_lints / src / redundant_field_names.rs
1 use clippy_utils::diagnostics::span_lint_and_sugg;
2 use clippy_utils::msrvs::{self, Msrv};
3 use rustc_ast::ast::{Expr, ExprKind};
4 use rustc_errors::Applicability;
5 use rustc_lint::{EarlyContext, EarlyLintPass, LintContext};
6 use rustc_middle::lint::in_external_macro;
7 use rustc_session::{declare_tool_lint, impl_lint_pass};
8
9 declare_clippy_lint! {
10     /// ### What it does
11     /// Checks for fields in struct literals where shorthands
12     /// could be used.
13     ///
14     /// ### Why is this bad?
15     /// If the field and variable names are the same,
16     /// the field name is redundant.
17     ///
18     /// ### Example
19     /// ```rust
20     /// let bar: u8 = 123;
21     ///
22     /// struct Foo {
23     ///     bar: u8,
24     /// }
25     ///
26     /// let foo = Foo { bar: bar };
27     /// ```
28     /// the last line can be simplified to
29     /// ```ignore
30     /// let foo = Foo { bar };
31     /// ```
32     #[clippy::version = "pre 1.29.0"]
33     pub REDUNDANT_FIELD_NAMES,
34     style,
35     "checks for fields in struct literals where shorthands could be used"
36 }
37
38 pub struct RedundantFieldNames {
39     msrv: Msrv,
40 }
41
42 impl RedundantFieldNames {
43     #[must_use]
44     pub fn new(msrv: Msrv) -> Self {
45         Self { msrv }
46     }
47 }
48
49 impl_lint_pass!(RedundantFieldNames => [REDUNDANT_FIELD_NAMES]);
50
51 impl EarlyLintPass for RedundantFieldNames {
52     fn check_expr(&mut self, cx: &EarlyContext<'_>, expr: &Expr) {
53         if !self.msrv.meets(msrvs::FIELD_INIT_SHORTHAND) {
54             return;
55         }
56
57         if in_external_macro(cx.sess(), expr.span) {
58             return;
59         }
60         if let ExprKind::Struct(ref se) = expr.kind {
61             for field in &se.fields {
62                 if field.is_shorthand {
63                     continue;
64                 }
65                 if let ExprKind::Path(None, path) = &field.expr.kind {
66                     if path.segments.len() == 1
67                         && path.segments[0].ident == field.ident
68                         && path.segments[0].args.is_none()
69                     {
70                         span_lint_and_sugg(
71                             cx,
72                             REDUNDANT_FIELD_NAMES,
73                             field.span,
74                             "redundant field names in struct initialization",
75                             "replace it with",
76                             field.ident.to_string(),
77                             Applicability::MachineApplicable,
78                         );
79                     }
80                 }
81             }
82         }
83     }
84     extract_msrv_attr!(EarlyContext);
85 }