Skip to content

Commit b5db97b

Browse files
committed
Add tests for while let
1 parent 45fd623 commit b5db97b

File tree

3 files changed

+95
-1
lines changed

3 files changed

+95
-1
lines changed

src/test/compile-fail/lint-unnecessary-parens.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
// except according to those terms.
1010

1111
#![deny(unnecessary_parens)]
12-
#![feature(if_let)]
12+
#![feature(if_let,while_let)]
1313

1414
#[deriving(Eq, PartialEq)]
1515
struct X { y: bool }
@@ -34,6 +34,7 @@ fn main() {
3434
_ => {}
3535
}
3636
if let 1i = (1i) {} //~ ERROR unnecessary parentheses around `if let` head expression
37+
while let 1i = (2i) {} //~ ERROR unnecessary parentheses around `while let` head expression
3738
let v = X { y: false };
3839
// struct lits needs parens, so these shouldn't warn.
3940
if (v == X { y: true }) {}

src/test/compile-fail/while-let.rs

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
#![feature(macro_rules,while_let)]
12+
13+
fn macros() {
14+
macro_rules! foo{
15+
($p:pat, $e:expr, $b:block) => {{
16+
while let $p = $e $b
17+
}}
18+
}
19+
macro_rules! bar{
20+
($p:pat, $e:expr, $b:block) => {{
21+
foo!($p, $e, $b)
22+
}}
23+
}
24+
25+
foo!(a, 1i, { //~ ERROR irrefutable while-let
26+
println!("irrefutable pattern");
27+
});
28+
bar!(a, 1i, { //~ ERROR irrefutable while-let
29+
println!("irrefutable pattern");
30+
});
31+
}
32+
33+
pub fn main() {
34+
while let a = 1i { //~ ERROR irrefutable while-let
35+
println!("irrefutable pattern");
36+
}
37+
}

src/test/run-pass/while-let.rs

+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
#![feature(while_let)]
12+
13+
use std::collections::PriorityQueue;
14+
15+
fn make_pq() -> PriorityQueue<int> {
16+
PriorityQueue::from_vec(vec![1i,2,3])
17+
}
18+
19+
pub fn main() {
20+
let mut pq = make_pq();
21+
let mut sum = 0i;
22+
while let Some(x) = pq.pop() {
23+
sum += x;
24+
}
25+
assert_eq!(sum, 6i);
26+
27+
pq = make_pq();
28+
sum = 0;
29+
'a: while let Some(x) = pq.pop() {
30+
sum += x;
31+
if x == 2 {
32+
break 'a;
33+
}
34+
}
35+
assert_eq!(sum, 5i);
36+
37+
pq = make_pq();
38+
sum = 0;
39+
'a: while let Some(x) = pq.pop() {
40+
if x == 3 {
41+
continue 'a;
42+
}
43+
sum += x;
44+
}
45+
assert_eq!(sum, 3i);
46+
47+
let mut pq1 = make_pq();
48+
sum = 0;
49+
while let Some(x) = pq1.pop() {
50+
let mut pq2 = make_pq();
51+
while let Some(y) = pq2.pop() {
52+
sum += x * y;
53+
}
54+
}
55+
assert_eq!(sum, 6i + 12 + 18);
56+
}

0 commit comments

Comments
 (0)