rust-lang / rust-lang/rust-clippy
field_reassign_with_default triggers inside `macro_rules!` macro
Nobody has claimed this yet.
- Dominant language
- Rust
- Stars
- 13.5k
- Forks
- 2.2k
- Avg merge
- 2d 10h
- Merged PRs (30d)
- 32
Description
Summary
For a macro, it might not always be practical to replace a field reassign after (default) initialization with a struct literal. In the case of this test, the only purpose of the macro being tested is providing struct-update like syntax but using assignments under the hood.
Lint Name
field_reassign_with_default
Reproducer
I tried this code:
use assign::assign; // version 1.1.1
#[derive(Default)]
struct SomeStruct {
a: u32,
b: Option<f32>,
c: Option<u64>,
}
let res = assign!(SomeStruct::default(), {
a: 5,
b: None,
});
I saw this happen (condensed form of full output):
warning: field assignment outside of initializer for an instance created with Default::default()
note: consider initializing the variable with `tests::SomeStruct { b: assign!(SomeStruct::default(), {
b,
c: 1.into(),
}), c: 1.into(), ..Default::default() }` and removing relevant reassignments
I expected to see this happen: No warnings
Version
rustc 1.59.0-nightly (e95e084a1 2021-12-19)
binary: rustc
commit-hash: e95e084a14870a718c712936ab5a8f8cd0159485
commit-date: 2021-12-19
host: x86_64-unknown-linux-gnu
release: 1.59.0-nightly
LLVM version: 13.0.0
Additional Labels
No response
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start at the field_reassign_with_default lint and reproduce the report with the assign crate example from docs.rs (assign/lib.rs lines 142-145). Trace how the macro_rules! expansion is analyzed; done means the reproducer emits no warning.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- rust
- Domain
- tooling
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 38/100