-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Closed
Labels
A-lintArea: New lintsArea: New lintsL-styleLint: Belongs in the style lint groupLint: Belongs in the style lint groupgood first issueThese issues are a good way to get started with ClippyThese issues are a good way to get started with Clippy
Description
What it does
A thing I see really often is beginners attempting to embrace functional style programming by replacing all for-loops with collection.iter().for_each(|item| ...). Because this happens so often, it might be beneficial to introduce a clippy lint to reassure those people that there's nothing wrong with the traditional for-loop.
This lint would be triggered when the following criteria match:
- a method is called on a binding (e.g.
foo.method()matches,make_foo().method()doesn't. That's because long iterator chains ending with for_each should not trigger this lint) - the methods returns an Iterator object
- for_each is called on the Iterator object, with a closure parameter (e.g.
vec.iter().for_each(process)should not trigger because the alternative is (arguably) inferior:for item in &vec { process(item) })
Categories (optional)
- Kind:
clippy::complexity
What is the advantage of the recommended code over the original code
The code is easier to digest than the syntax-heavy for_each with the embedded closure.
Drawbacks
None.
Example
hash_map.keys().for_each(|key| {
// ...
});Could be written as:
for key in hash_map.keys() {
// ...
}OverflowCat and purplesyringa
Metadata
Metadata
Assignees
Labels
A-lintArea: New lintsArea: New lintsL-styleLint: Belongs in the style lint groupLint: Belongs in the style lint groupgood first issueThese issues are a good way to get started with ClippyThese issues are a good way to get started with Clippy