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.
14 #![warn(clippy::while_let_loop, clippy::empty_loop, clippy::while_let_on_iterator)]
15 #![allow(dead_code, clippy::never_loop, unused, clippy::cyclomatic_complexity)]
26 loop { // no error, break is not in else clause
52 { let _b = "foobar"; }
54 loop { // no error, else branch does something other than break
63 while let Some(x) = y { // no error, obviously
67 // #675, this used to have a wrong suggestion
69 let (e, l) = match "".split_whitespace().next() {
70 Some(word) => (word.is_empty(), word.len()),
78 while let Option::Some(x) = iter.next() {
83 while let Some(x) = iter.next() {
88 while let Some(_) = iter.next() {}
91 while let None = iter.next() {} // this is fine (if nonsensical)
94 if let Some(x) = iter.next() { // also fine
98 // the following shouldn't warn because it can't be written with a for loop
99 let mut iter = 1u32..20;
100 while let Some(x) = iter.next() {
101 println!("next: {:?}", iter.next())
105 let mut iter = 1u32..20;
106 while let Some(x) = iter.next() {
107 println!("next: {:?}", iter.next());
111 let mut iter = 1u32..20;
112 while let Some(x) = iter.next() {break;}
113 println!("Remaining iter {:?}", iter);
116 let mut iter = 1u32..20;
117 while let Some(x) = iter.next() {
122 // regression test (#360)
123 // this should not panic
124 // it's okay if further iterations of the lint
125 // cause this function to trigger it
126 fn no_panic<T>(slice: &[T]) {
127 let mut iter = slice.iter();
129 let _ = match iter.next() {
138 let r: Result<u32, u32> = Ok(42);
155 let mut b = a.iter();
157 // consume all the 42s
158 while let Some(&42) = b.next() {
162 let mut b = a.iter();
164 while let Some(&(1, 2, 3)) = b.next() {
168 let mut b = a.iter();
170 while let Some(&None) = b.next() {
173 /* This gives “refutable pattern in `for` loop binding: `&_` not covered”
175 for &(1, 2, 3) in b {}
176 for &Option::None in b.next() {}
179 let mut y = a.iter();
180 loop { // x is reused, so don't lint here
181 while let Some(v) = y.next() {
185 let mut y = a.iter();
187 while let Some(v) = y.next() { // y is reused, don't lint
192 let mut y = a.iter();
193 while let Some(v) = y.next() { // use a for loop here
197 // should not trigger clippy::while_let_loop lint because break passes an expression
207 use std::collections::HashSet;
208 let mut values = HashSet::new();
211 while let Some(&value) = values.iter().next() {
212 values.remove(&value);
215 // This should not cause an ICE and suggest:
217 // for _ in values.iter() {}
220 while let Some(..) = values.iter().next() {