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
pubfnmain(){letmut i = 0;let v = vec!["a","b","c"];for x in&v {
i += 1;println!("{}", i)}}
explicit_counter_loop will trigger here and suggest to use .iter().enumerate().
The problem is that we increment BEFORE using (and not at the end of the loop) so the first iteration will print 1 and not 0 as it would with .enumerate().
This newly suggested code is off by one as can be seen here:
pubfnmain(){letmut i = 0;let v = vec!["a","b","c"];for(x, _)in v.iter().enumerate(){
i += 1;println!("i is {} but x is {}", i, x)}}
i is 1 but x is 0
i is 2 but x is 1
i is 3 but x is 2
clippy 0.0.212 (fb1dc34 2020-09-21)
The text was updated successfully, but these errors were encountered:
…iaskrgr
Fix a FP in `explicit_counter_loop`
Fixes#4677 and #6074
Fix a false positive in `explicit_counter_loop` where the loop counter is used after incremented, adjust the test so that counters are incremented at the end of the loop and add the test for this false positive.
---
changelog: Fix a false positive in `explicit_counter_loop` where the loop counter is used after incremented
I tried this code:
explicit_counter_loop
will trigger here and suggest to use.iter().enumerate()
.The problem is that we increment BEFORE using (and not at the end of the loop) so the first iteration will print 1 and not
0
as it would with.enumerate()
.This newly suggested code is off by one as can be seen here:
clippy 0.0.212 (fb1dc34 2020-09-21)
The text was updated successfully, but these errors were encountered: