rust-lang / rust-lang/rust-clippy
Suggest extend over for_each for element insertion
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
The lint should detect/convert code which uses a .for_each containing a single .insert into an .extend call.
Lint Name
manual_extend
Category
style
Advantage
The code becomes much cleaner and easier to understand.
Mentally, I read the first example as follows:
- Get all keys from
self.state_machine_info.det_states - For each key
- Insert it in
states, but firstclonethe key.
- Insert it in
The second example though:
- Extend
states - Using the cloned
keysfromself.state_machine_info.det_states
Drawbacks
I know that not everyone will read it as I will, however the second approach feels much more in-line with "proper" Rust.
Example
In my typestate macro I have the following code:
let mut states = HashSet::new();
self.state_machine_info.det_states.keys().for_each(|k| {
states.insert(k.clone());
});
self.state_machine_info
.non_det_transitions
.keys()
.for_each(|k| {
states.insert(k.clone());
});
Which becomes much cleaner when written like:
let mut states = HashSet::new();
states.extend(
self.state_machine_info.det_states.keys().cloned()
);
states.extend(
self.state_machine_info.non_det_transitions.keys().cloned()
);
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 manual_extend lint name and the Rust example linked in the issue, focusing on the single-insert .for_each pattern. Define the supported transformation from the first example to the .extend form, then verify the lint’s suggested output and behavior with representative cases.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- rust
- Domain
- tooling
- Issue type
- Feature
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100