]> git.lizzy.rs Git - rust.git/blob - tests/compile-fail/while_loop.rs
Fix FP with `WHILE_LET_LOOP` and break expressions
[rust.git] / tests / compile-fail / while_loop.rs
1 #![feature(plugin)]
2 #![plugin(clippy)]
3
4 #![deny(while_let_loop, empty_loop, while_let_on_iterator)]
5 #![allow(dead_code, unused, cyclomatic_complexity)]
6
7 fn main() {
8     let y = Some(true);
9     loop {
10     //~^ERROR this loop could be written as a `while let` loop
11     //~|HELP try
12     //~|SUGGESTION while let Some(_x) = y {
13         if let Some(_x) = y {
14             let _v = 1;
15         } else {
16             break
17         }
18     }
19     loop { // no error, break is not in else clause
20         if let Some(_x) = y {
21             let _v = 1;
22         }
23         break;
24     }
25     loop {
26     //~^ERROR this loop could be written as a `while let` loop
27     //~|HELP try
28     //~|SUGGESTION while let Some(_x) = y {
29         match y {
30             Some(_x) => true,
31             None => break
32         };
33     }
34     loop {
35     //~^ERROR this loop could be written as a `while let` loop
36     //~|HELP try
37     //~|SUGGESTION while let Some(x) = y {
38         let x = match y {
39             Some(x) => x,
40             None => break
41         };
42         let _x = x;
43         let _str = "foo";
44     }
45     loop {
46     //~^ERROR this loop could be written as a `while let` loop
47     //~|HELP try
48     //~|SUGGESTION while let Some(x) = y {
49         let x = match y {
50             Some(x) => x,
51             None => break,
52         };
53         { let _a = "bar"; };
54         { let _b = "foobar"; }
55     }
56     loop { // no error, else branch does something other than break
57         match y {
58             Some(_x) => true,
59             _ => {
60                 let _z = 1;
61                 break;
62             }
63         };
64     }
65     while let Some(x) = y { // no error, obviously
66         println!("{}", x);
67     }
68
69     // #675, this used to have a wrong suggestion
70     loop {
71     //~^ERROR this loop could be written as a `while let` loop
72     //~|HELP try
73     //~|SUGGESTION while let Some(word) = "".split_whitespace().next() { .. }
74         let (e, l) = match "".split_whitespace().next() {
75             Some(word) => (word.is_empty(), word.len()),
76             None => break
77         };
78
79         let _ = (e, l);
80     }
81
82     let mut iter = 1..20;
83     while let Option::Some(x) = iter.next() {
84     //~^ ERROR this loop could be written as a `for` loop
85     //~| HELP try
86     //~| SUGGESTION for x in iter {
87         println!("{}", x);
88     }
89
90     let mut iter = 1..20;
91     while let Some(x) = iter.next() {
92     //~^ ERROR this loop could be written as a `for` loop
93     //~| HELP try
94     //~| SUGGESTION for x in iter {
95         println!("{}", x);
96     }
97
98     let mut iter = 1..20;
99     while let Some(_) = iter.next() {}
100     //~^ ERROR this loop could be written as a `for` loop
101     //~| HELP try
102     //~| SUGGESTION for _ in iter {
103
104     let mut iter = 1..20;
105     while let None = iter.next() {} // this is fine (if nonsensical)
106
107     let mut iter = 1..20;
108     if let Some(x) = iter.next() { // also fine
109         println!("{}", x)
110     }
111
112     // the following shouldn't warn because it can't be written with a for loop
113     let mut iter = 1u32..20;
114     while let Some(x) = iter.next() {
115         println!("next: {:?}", iter.next())
116     }
117
118     // neither can this
119     let mut iter = 1u32..20;
120     while let Some(x) = iter.next() {
121         println!("next: {:?}", iter.next());
122     }
123
124     // or this
125     let mut iter = 1u32..20;
126     while let Some(x) = iter.next() {break;}
127     println!("Remaining iter {:?}", iter);
128
129     // or this
130     let mut iter = 1u32..20;
131     while let Some(x) = iter.next() {
132         iter = 1..20;
133     }
134 }
135
136 // regression test (#360)
137 // this should not panic
138 // it's okay if further iterations of the lint
139 // cause this function to trigger it
140 fn no_panic<T>(slice: &[T]) {
141     let mut iter = slice.iter();
142     loop {
143     //~^ ERROR
144     //~| HELP try
145     //~| SUGGESTION while let Some(ele) = iter.next() { .. }
146         let _ = match iter.next() {
147             Some(ele) => ele,
148             None => break
149         };
150         loop {} //~ERROR empty `loop {}` detected.
151     }
152 }
153
154 fn issue1017() {
155     let r: Result<u32, u32> = Ok(42);
156     let mut len = 1337;
157
158     loop {
159         match r {
160             Err(_) => len = 0,
161             Ok(length) => {
162                 len = length;
163                 break
164             }
165         }
166     }
167 }