Skip to content

redundant_field_names triggered on derived code #17525

Closed
rust-lang/rust
#164816

Description

@nazar-pc

Summary

This lint should not be triggered on code generated by macros, but right now it is

Lint Name

redundant_field_names

Reproducer

I tried this code:

#[derive(Debug, thiserror::Error)]
pub enum SegmentDownloadingError {
    // ...

    /// Segment reconstruction error
    #[error("Segment reconstruction error: {source}")]
    SegmentReconstruction {
        #[from]
        source: ReconstructorError,
    },
}

Internally it is expanded to things like this:

#[allow(deprecated, unused_qualifications, )]
#[automatically_derived]
impl ::core::convert::From<ReconstructorError> for SegmentDownloadingError {
    fn from(source: ReconstructorError) -> Self {
        SegmentDownloadingError::SegmentReconstruction {
            source: source,
        }
    }
}

I saw this happen:

warning: redundant field names in struct initialization
  --> crates/shared/ab-data-retrieval/src/segment_downloading.rs:48:9
   |
48 | /         #[from]
49 | |         source: ReconstructorError,
   | |______________^ help: replace it with: `source`
   |
   = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#redundant_field_names

I expected to see this happen:
No lint should be triggered since it is outside of developer's control and generated by proc macro.

Version

rustc 1.99.0-nightly (1a98b1e13 2026-08-07)
binary: rustc
commit-hash: 1a98b1e135b254f209c67d447b6d8bcd56a859e0
commit-date: 2026-08-07
host: x86_64-unknown-linux-gnu
release: 1.99.0-nightly
LLVM version: 23.1.0

Additional Labels

No response

Activity

  1. added
    C-bug
    I-false-positive
    on Aug 8, 2026
  2. nazar-pc commented on Aug 8, 2026

    @nazar-pc
    Author

    This was addressed by thiserror in dtolnay/thiserror#454, but I think it is still beneficial to resolve in clippy itself

  3. added
    T-macros
    on Aug 9, 2026
  4. added
    good first issue
    on Aug 10, 2026
  5. nightcityblade commented on Aug 10, 2026

    @nightcityblade

    Hi, I'd like to work on this. I'll submit a PR shortly.

  6. nightcityblade commented on Aug 10, 2026

    @nightcityblade

    @rustbot claim

  7. added a commit that references this issue on Aug 10, 2026
  8. azdle commented on Aug 12, 2026

    @azdle

    I believe this is a bug introduced by @Jarcho's change in #17294, here:

    https://github.com/rust-lang/rust-clippy/pull/17294/changes#diff-93cf0002fc1d8e941310800dda2bf3ff1c9cc7aa1a2f7437b38abe05b3132128

    They replaced an early return check on expr.span.in_external_macro(cx.sess().source_map()) with a per-field check on field.span.in_external_macro(cx.sess().source_map()).

    In my testing, the expr.span points at the #[derive(...)] line, but the field.spans point at the original struct definition field line. So the expr as a whole is in_external_macro, but each individual field isn't. I'm not sure exactly why, but suspect the derive macro's output AST keeps the original span info when an Ident is used to construct the StructExpr's fields.

    I believe the solution is to just revert to doing the early return on expr.span.in_external_macro(cx.sess().source_map()), but I don't really understand why that particular change was made in the first place.

    An example showing the misplaced spans. For this `lib.rs`:
    use derive_more::Constructor;
    
    #[derive(Constructor)]
    pub struct S {
        pub f: bool,
    }
    pub fn ext_new_s(f: bool) -> S {
        S { f: f }
    }
    

    And with a dbg!(field); and dbg!(expr) added just before span_lint_and_sugg this is the log showing both the unintentional warning in the derive and the intentional warning in the ext_new_s function.:

    [clippy_lints/src/redundant_field_names.rs:64:21] field = ExprField {
        attrs: [],
        id: NodeId(46),
        span: src/lib.rs:5:9: 5:10 (#0),
        ident: f#0,
        expr: Expr {
            id: NodeId(47),
            kind: Path(
                None,
                Path {
                    span: src/lib.rs:5:9: 5:10 (#0),
                    segments: [
                        PathSegment {
                            ident: f#0,
                            id: NodeId(48),
                            args: None,
                        },
                    ],
                },
            ),
            span: src/lib.rs:5:9: 5:10 (#0),
            attrs: [],
            tokens: None,
        },
        is_shorthand: false,
        is_placeholder: false,
    }
    [clippy_lints/src/redundant_field_names.rs:65:21] expr = Expr {
        id: NodeId(44),
        kind: Struct(
            StructExpr {
                qself: None,
                path: Path {
                    span: src/lib.rs:4:12: 4:13 (#0),
                    segments: [
                        PathSegment {
                            ident: S#0,
                            id: NodeId(45),
                            args: None,
                        },
                    ],
                },
                fields: [
                    ExprField {
                        attrs: [],
                        id: NodeId(46),
                        span: src/lib.rs:5:9: 5:10 (#0),
                        ident: f#0,
                        expr: Expr {
                            id: NodeId(47),
                            kind: Path(
                                None,
                                Path {
                                    span: src/lib.rs:5:9: 5:10 (#0),
                                    segments: [
                                        PathSegment {
                                            ident: f#0,
                                            id: NodeId(48),
                                            args: None,
                                        },
                                    ],
                                },
                            ),
                            span: src/lib.rs:5:9: 5:10 (#0),
                            attrs: [],
                            tokens: None,
                        },
                        is_shorthand: false,
                        is_placeholder: false,
                    },
                ],
                rest: None,
            },
        ),
        span: src/lib.rs:3:10: 3:21 (#6),
        attrs: [],
        tokens: None,
    }
    warning: redundant field names in struct initialization
     --> src/lib.rs:5:9
      |
    5 |     pub f: bool,
      |         ^ help: replace it with: `f`
      |
      = help: for further information visit https://rust-lang.github.io/rust-clippy/main/index.html#redundant_field_names
      = note: `#[warn(clippy::redundant_field_names)]` on by default
    
    [clippy_lints/src/redundant_field_names.rs:64:21] field = ExprField {
        attrs: [],
        id: NodeId(20),
        span: src/lib.rs:8:9: 8:13 (#0),
        ident: f#0,
        expr: Expr {
            id: NodeId(21),
            kind: Path(
                None,
                Path {
                    span: src/lib.rs:8:12: 8:13 (#0),
                    segments: [
                        PathSegment {
                            ident: f#0,
                            id: NodeId(22),
                            args: None,
                        },
                    ],
                },
            ),
            span: src/lib.rs:8:12: 8:13 (#0),
            attrs: [],
            tokens: None,
        },
        is_shorthand: false,
        is_placeholder: false,
    }
    [clippy_lints/src/redundant_field_names.rs:65:21] expr = Expr {
        id: NodeId(18),
        kind: Struct(
            StructExpr {
                qself: None,
                path: Path {
                    span: src/lib.rs:8:5: 8:6 (#0),
                    segments: [
                        PathSegment {
                            ident: S#0,
                            id: NodeId(19),
                            args: None,
                        },
                    ],
                },
                fields: [
                    ExprField {
                        attrs: [],
                        id: NodeId(20),
                        span: src/lib.rs:8:9: 8:13 (#0),
                        ident: f#0,
                        expr: Expr {
                            id: NodeId(21),
                            kind: Path(
                                None,
                                Path {
                                    span: src/lib.rs:8:12: 8:13 (#0),
                                    segments: [
                                        PathSegment {
                                            ident: f#0,
                                            id: NodeId(22),
                                            args: None,
                                        },
                                    ],
                                },
                            ),
                            span: src/lib.rs:8:12: 8:13 (#0),
                            attrs: [],
                            tokens: None,
                        },
                        is_shorthand: false,
                        is_placeholder: false,
                    },
                ],
                rest: None,
            },
        ),
        span: src/lib.rs:8:5: 8:15 (#0),
        attrs: [],
        tokens: None,
    }
    warning: redundant field names in struct initialization
     --> src/lib.rs:8:9
      |
    8 |     S { f: f }
      |         ^^^^ help: replace it with: `f`
      |
      = help: for further information visit https://rust-lang.github.io/rust-clippy/main/index.html#redundant_field_names
    
  9. kiana1kaslana commented on Sep 7, 2026

    @kiana1kaslana

    I also want to work on it. @rustbot claim

  10. added a commit that references this issue on Oct 25, 2026
    4c358b7
  11. added a commit that references this issue on Oct 25, 2026
    48de507
  12. Jarcho commented on Oct 25, 2026

    @Jarcho
    Contributor

    Fixed upstream: rust-lang/rust#164816

  13. added 2 commits that reference this issue on Oct 30, 2026
    eb892e1
    831eded
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

C-bugI-false-positiveT-macrosgood first issue

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions