rust-lang / rust-lang/rust-clippy

needless_update false positive when struct definition or struct expression uses `cfg`

Open
#13,076 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

C-bug I-false-positive
Dominant language
Rust
Stars
13.5k
Forks
2.2k
Avg merge
2d 10h
Merged PRs (30d)
32

Description

Summary

There are cases where a struct expression using functional update syntax is the best way to express what I want, but some of the fields in the struct are conditionally compiled, and under some feature combinations it can be possible that the .. part of the struct expression contains no fields.

Example: some of the method implementations in https://github.com/dtolnay/syn/blob/2.0.70/src/fixup.rs.

Lint Name

needless_update

Reproducer

Struct definition uses conditional compilation:

struct Struct {
    #[cfg(feature = "a")]
    a: bool,

    #[cfg(feature = "b")]
    b: bool,
}

impl Struct {
    #[cfg(feature = "a")]
    fn a(self) -> Self {
        Struct { a: true, ..self }
    }
}
$ cargo clippy --features a
warning: struct update has no effect, all the fields in the struct have already been specified
  --> src/main.rs:12:29
   |
12 |         Struct { a: true, ..self }
   |                             ^^^^
   |
   = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#needless_update
   = note: `#[warn(clippy::needless_update)]` on by default

Struct expression uses conditional compilation:

struct Struct {
    a: bool,
}

impl Struct {
    fn a(self) -> Self {
        Struct {
            #[cfg(feature = "a")]
            a: true,
            ..self
        }
    }
}
$ cargo clippy --features a
warning: struct update has no effect, all the fields in the struct have already been specified
  --> src/main.rs:10:15
   |
10 |             ..self
   |               ^^^^
   |
   = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#needless_update
   = note: `#[warn(clippy::needless_update)]` on by default
Version
rustc 1.81.0-nightly (20ae37c18 2024-07-07)
binary: rustc
commit-hash: 20ae37c18df95f9246c019b04957d23b4164bf7a
commit-date: 2024-07-07
host: x86_64-unknown-linux-gnu
release: 1.81.0-nightly
LLVM version: 18.1.7
Additional Labels

No response

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by locating the needless_update lint implementation and reproduce both examples with cargo clippy --features a. Add regression coverage for conditional fields and conditional struct expressions, then verify that the lint no longer reports an update when the .. expression contains no remaining fields.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.