rust-lang / rust-lang/rust-clippy

clippy::needless_update with Default..default() is not usable

Open
#12,997 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

this is a reprisal of #6323.

In #6323 the recommendation is to use #[non_exhaustive], but that leads to
error[E0639]: cannot create non-exhaustive struct using struct expression

Leaving users with:
a) toil when config structs have fields added
b) having to write and maintain a Builder (or derive with e.g. https://docs.rs/derive_builder/latest/derive_builder/ ) because the native ergonomic code is behind a lint
c) disable the lint and not have places where it would actually apply be linted

So my argument here is that if people will bring in a builder, this lint isn't actually lifting the bar or adding safety vs permitting the idiom with Default::default() specifically.

Lint Name

clippy::needless_update

Reproducer

I tried this code:

#[derive(Default)]
struct S {
    x: i32,
}
fn foo() -> S {
    S {
        x: 123,
        ..S::default()
    }
}

I saw this happen:

warning: struct update has no effect, all the fields in the struct have already been specified

I expected to see this happen:

Version
rustc 1.77.0 (aedd173a2 2024-03-17)
binary: rustc
commit-hash: aedd173a2c086e558c2b66d3743b344f977621a7
commit-date: 2024-03-17
host: x86_64-unknown-linux-gnu
release: 1.77.0
LLVM version: 17.0.6
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 with the clippy::needless_update lint and the Rust reproducer in the issue to understand why the struct update is warned on. Review the lint's existing behavior and tests, then define coverage for the Default::default() pattern. Done means the shown code no longer produces the needless_update warning without disabling the lint generally.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
devtools
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.