Add iterator reduction coverage to never_loop
Fixes rust-lang/rust-clippy#16061.
Extend never_loop to also lint iterator reduction methods (e.g. for_each, try_for_each, fold, try_fold, reduce, all, any) when the closure’s body diverges (return type !). Add a UI test never_loop_iterator_reduction to cover these cases.
Testing:
- TESTNAME=never_loop_iterator_reduction cargo uitest
changelog: [never_loop]: lint diverging iterator reduction closures like for_each and fold
r? @Jarcho
rustbot has assigned @Jarcho. They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.
Use r? to explicitly pick a reviewer
Reminder, once the PR becomes ready for a review, use @rustbot ready.
I pushed an update to address all 4 comments
- Combined the method check for both
unused_enumerate_indexandnever_loop - Use a more direct check for
Iteratortrait - Use
HasPlaceholdersfor the applicability - Use
snippet_with_contextinstead ofmake_iterator_snippet
@rustbot ready
Addressed the requested changes:
- Switched to a single
matchfor theMethodCallhandling
@rustbot ready
Thanks for the quick review!
I pushed an update to address the comments:
-
is_iterator_method()stays behind the cheap name/arg checks - moved the shared iterator + closure checks into
check_expr - pass the closure into
never_loop/unused_enumerate_indexinstead of re-parsing inside
@rustbot ready
@Jarcho: I’m repeating if let ExprKind::Closure(closure) = arg.kind && is_iterator_method() in all match arms to keep the expensive check last. Do you want me to make a small helper for this, or is it fine as-is?
I’m repeating if let ExprKind::Closure(closure) = arg.kind && is_iterator_method() in all match arms to keep the expensive check last. Do you want me to make a small helper for this, or is it fine as-is?
A helper function would end up harder to read. It's two steps that are, without external context, unrelated to each other. Having to jump to another spot to just to read what it does is also a pain.