You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
needless_for_each is suggested when for_each actually improves performance over for #17856
There are a bunch if iterators where using for_each removes branching and improves performance: x..=y ranges, VecDeque iterator, .chain(), etc.
However, needless_for_each still triggers there, even though it leads to slower code.
The reproducer example is a little convoluted, since lint is a bit tricky to trigger; for simpler example with asm: (0..=n) godbolt.
P.S. If anything, I think the inverse rule should be true: for loops without control flow should be suggested to be rewritten as for_each.
-Copt-level=s gives same repro and less code on this one ^^
To me this sounds a bit like rustc/LLVM has a hard time reasoning about the inclusive ranges (since issue does not occur with the exclusive range 0..n).
Sorry, I should've explained the problem better. The problem lies in what for x in y desugars into:
for x in y {}// roughly translates to this:letmut $iter = y.into_iter();whileletSome(x) = $iter.next(){}
However, fold and for_each can be optimized better than simply calling next for every item. Compare next() vs fold() in VecDeque. Or next and fold in .chain() iterator. Iterators for ..= ranges are optimized through try_fold() -> Result<B, !>, so it is harder to reason about, but I think two previous examples are good enough.
This is not fixable from compiler side without breaking code that relies on particular for desugaring.
So no, I don't think I will be opening issue with rust-lang/rust.
-Copt-level=s gives same repro and less code on this one ^^ To me this sounds a bit like rustc/LLVM has a hard time reasoning about the inclusive ranges (since issue does not occur with the exclusive range 0..n).
Yeah it's probably better. Although I'm a little cautious of optimizing for binary size over default -Copt-level=3.
Activity
CommanderStorm commentedon Oct 7, 2026
-Copt-level=sgives same repro and less code on this one ^^To me this sounds a bit like rustc/LLVM has a hard time reasoning about the inclusive ranges (since issue does not occur with the exclusive range
0..n).There are a few issues in rust which seem similar -> https://github.com/rust-lang/rust/issues?q=is%3Aissue+state%3Aopen+for_each+label%3AI-slow
Maybe closest issue (but not quite?). This one seems a bit more narrow and better to understand.
-> rust-lang/rust#112911
Would you be open to opening an issue with https://github.com/rust-lang/rust too? ^^
immconcon commentedon Oct 8, 2026
Sorry, I should've explained the problem better. The problem lies in what
for x in ydesugars into:However,
foldandfor_eachcan be optimized better than simply callingnextfor every item. Comparenext()vsfold()inVecDeque. Ornextandfoldin.chain()iterator. Iterators for..=ranges are optimized throughtry_fold() -> Result<B, !>, so it is harder to reason about, but I think two previous examples are good enough.This is not fixable from compiler side without breaking code that relies on particular
fordesugaring.So no, I don't think I will be opening issue with
rust-lang/rust.Yeah it's probably better. Although I'm a little cautious of optimizing for binary size over default
-Copt-level=3.