File tree Expand file tree Collapse file tree 3 files changed +41
-15
lines changed Expand file tree Collapse file tree 3 files changed +41
-15
lines changed Original file line number Diff line number Diff line change @@ -119,10 +119,7 @@ fn issue2965() {
119119 let mut values = HashSet::new();
120120 values.insert(1);
121121
122- for _ in values.iter() {
123- // FIXME(flip1995): Linting this with the following line uncommented is a FP, see #1654
124- // values.remove(&1);
125- }
122+ while let Some(..) = values.iter().next() {}
126123}
127124
128125fn issue3670() {
@@ -134,11 +131,30 @@ fn issue3670() {
134131 }
135132}
136133
134+ fn issue1654() {
135+ // should not lint if the iterator is generated on every iteration
136+ use std::collections::HashSet;
137+ let mut values = HashSet::new();
138+ values.insert(1);
139+
140+ while let Some(..) = values.iter().next() {
141+ values.remove(&1);
142+ }
143+
144+ while let Some(..) = values.iter().map(|x| x + 1).next() {}
145+
146+ let chars = "Hello, World!".char_indices();
147+ while let Some((i, ch)) = chars.clone().next() {
148+ println!("{}: {}", i, ch);
149+ }
150+ }
151+
137152fn main() {
138153 base();
139154 refutable();
140155 nested_loops();
141156 issue1121();
142157 issue2965();
143158 issue3670();
159+ issue1654();
144160}
Original file line number Diff line number Diff line change @@ -119,10 +119,7 @@ fn issue2965() {
119119 let mut values = HashSet :: new ( ) ;
120120 values. insert ( 1 ) ;
121121
122- while let Some ( ..) = values. iter ( ) . next ( ) {
123- // FIXME(flip1995): Linting this with the following line uncommented is a FP, see #1654
124- // values.remove(&1);
125- }
122+ while let Some ( ..) = values. iter ( ) . next ( ) { }
126123}
127124
128125fn issue3670 ( ) {
@@ -134,11 +131,30 @@ fn issue3670() {
134131 }
135132}
136133
134+ fn issue1654 ( ) {
135+ // should not lint if the iterator is generated on every iteration
136+ use std:: collections:: HashSet ;
137+ let mut values = HashSet :: new ( ) ;
138+ values. insert ( 1 ) ;
139+
140+ while let Some ( ..) = values. iter ( ) . next ( ) {
141+ values. remove ( & 1 ) ;
142+ }
143+
144+ while let Some ( ..) = values. iter ( ) . map ( |x| x + 1 ) . next ( ) { }
145+
146+ let chars = "Hello, World!" . char_indices ( ) ;
147+ while let Some ( ( i, ch) ) = chars. clone ( ) . next ( ) {
148+ println ! ( "{}: {}" , i, ch) ;
149+ }
150+ }
151+
137152fn main ( ) {
138153 base ( ) ;
139154 refutable ( ) ;
140155 nested_loops ( ) ;
141156 issue1121 ( ) ;
142157 issue2965 ( ) ;
143158 issue3670 ( ) ;
159+ issue1654 ( ) ;
144160}
Original file line number Diff line number Diff line change @@ -24,11 +24,5 @@ error: this loop could be written as a `for` loop
2424LL | while let Some(_) = y.next() {
2525 | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `for _ in y`
2626
27- error: this loop could be written as a `for` loop
28- --> $DIR/while_let_on_iterator.rs:122:5
29- |
30- LL | while let Some(..) = values.iter().next() {
31- | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `for _ in values.iter()`
32-
33- error: aborting due to 5 previous errors
27+ error: aborting due to 4 previous errors
3428
You can’t perform that action at this time.
0 commit comments