1 // Copyright 2014-2018 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution.
4 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
5 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
6 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
7 // option. This file may not be copied, modified, or distributed
8 // except according to those terms.
10 #![warn(clippy::while_let_loop, clippy::empty_loop, clippy::while_let_on_iterator)]
11 #![allow(dead_code, clippy::never_loop, unused, clippy::cyclomatic_complexity)]
23 // no error, break is not in else clause
56 // no error, else branch does something other than break
65 while let Some(x) = y {
66 // no error, obviously
70 // #675, this used to have a wrong suggestion
72 let (e, l) = match "".split_whitespace().next() {
73 Some(word) => (word.is_empty(), word.len()),
81 while let Option::Some(x) = iter.next() {
86 while let Some(x) = iter.next() {
91 while let Some(_) = iter.next() {}
94 while let None = iter.next() {} // this is fine (if nonsensical)
97 if let Some(x) = iter.next() {
102 // the following shouldn't warn because it can't be written with a for loop
103 let mut iter = 1u32..20;
104 while let Some(x) = iter.next() {
105 println!("next: {:?}", iter.next())
109 let mut iter = 1u32..20;
110 while let Some(x) = iter.next() {
111 println!("next: {:?}", iter.next());
115 let mut iter = 1u32..20;
116 while let Some(x) = iter.next() {
119 println!("Remaining iter {:?}", iter);
122 let mut iter = 1u32..20;
123 while let Some(x) = iter.next() {
128 // regression test (#360)
129 // this should not panic
130 // it's okay if further iterations of the lint
131 // cause this function to trigger it
132 fn no_panic<T>(slice: &[T]) {
133 let mut iter = slice.iter();
135 let _ = match iter.next() {
144 let r: Result<u32, u32> = Ok(42);
161 let mut b = a.iter();
163 // consume all the 42s
164 while let Some(&42) = b.next() {}
167 let mut b = a.iter();
169 while let Some(&(1, 2, 3)) = b.next() {}
172 let mut b = a.iter();
174 while let Some(&None) = b.next() {}
176 /* This gives “refutable pattern in `for` loop binding: `&_` not covered”
178 for &(1, 2, 3) in b {}
179 for &Option::None in b.next() {}
182 let mut y = a.iter();
184 // x is reused, so don't lint here
185 while let Some(v) = y.next() {}
188 let mut y = a.iter();
190 while let Some(v) = y.next() {
191 // y is reused, don't lint
196 let mut y = a.iter();
197 while let Some(v) = y.next() {
198 // use a for loop here
202 // should not trigger clippy::while_let_loop lint because break passes an expression
212 use std::collections::HashSet;
213 let mut values = HashSet::new();
216 while let Some(&value) = values.iter().next() {
217 values.remove(&value);
220 // This should not cause an ICE and suggest:
222 // for _ in values.iter() {}
225 while let Some(..) = values.iter().next() {