rust-lang / rust-lang/rust-clippy
lint: unread_partial_move_field
Nobody has claimed this yet.
- Dominant language
- Rust
- Stars
- 13.5k
- Forks
- 2.2k
- Avg merge
- 2d 10h
- Merged PRs (30d)
- 32
Description
What it does
I'm not sure should this lint belong to clippy or cargo check
it should check code like this, and warn that Captured field i.a is never read, maybe remove ``move`` to capture by (mutable) reference?
#[derive(Debug)]
struct Stat{
a:usize,
b: String,
}
pub fn main() {
let mut i = Stat{
a:0,
b:"abc".to_string()
};
let mut f = move||{i.a+=1}; // if `move` is removed i.a will become 1
f();
dbg!(&i); // i still 0
}
Advantage
It will greatly reduce confusion when someone accidently impl copy on a field and need to update by mutable reference, like this case: https://github.com/rust-lang/rust/issues/108808
Drawbacks
If there is a case when one partial capture a field that impl Copy and never read it intentionally, then there might be a false positive, but I can't found a solid example.
Example
#[derive(Debug)]
struct Stat{
a:usize,
b: String,
}
pub fn main() {
let mut i = Stat{
a:0,
b:"abc".to_string()
};
let mut f = move||{i.a+=1};
f();
}
Could be written as:
#[derive(Debug)]
struct Stat{
a:usize,
b: String,
}
pub fn main() {
let mut i = Stat{
a:0,
b:"abc".to_string()
};
let mut f = ||{i.a+=1};
f();
}
or
#[derive(Debug)]
struct Stat{
a:usize,
b: String,
}
pub fn main() {
let mut i = Stat{
a:0,
b:"abc".to_string()
};
let mut f = move||{
let i = &mut i;
i.a+=1
};
f();
}
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 with the closure examples in this issue and the linked rust-lang/rust#108808 to understand the partial-capture behavior. Determine whether the warning belongs in Clippy or cargo check, then define its scope and acceptable false positives. Done means the behavior, diagnostic, and coverage expectations are agreed before implementation.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- rust
- Domain
- tooling
- Issue type
- Feature
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100