Skip to content

Commit f5e64ae

Browse files
committed
Test for mut in ident patterns.
1 parent 99b7662 commit f5e64ae

File tree

3 files changed

+115
-0
lines changed

3 files changed

+115
-0
lines changed

src/test/compile-fail/lint-unused-mut-variables.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,14 @@ fn main() {
2020
let mut a = 2; //~ ERROR: variable does not need to be mutable
2121
let mut b = 3; //~ ERROR: variable does not need to be mutable
2222
let mut a = ~[3]; //~ ERROR: variable does not need to be mutable
23+
let (mut a, b) = (1, 2); //~ ERROR: variable does not need to be mutable
24+
25+
match 30 {
26+
mut x => {} //~ ERROR: variable does not need to be mutable
27+
}
28+
29+
let x = |mut y: int| 10; //~ ERROR: variable does not need to be mutable
30+
fn what(mut foo: int) {} //~ ERROR: variable does not need to be mutable
2331

2432
// positive cases
2533
let mut a = 2;
@@ -30,6 +38,17 @@ fn main() {
3038
do callback {
3139
a.push(3);
3240
}
41+
let (mut a, b) = (1, 2);
42+
a = 34;
43+
44+
match 30 {
45+
mut x => {
46+
x = 21;
47+
}
48+
}
49+
50+
let x = |mut y: int| y = 32;
51+
fn nothing(mut foo: int) { foo = 37; }
3352
}
3453

3554
fn callback(f: &fn()) {}

src/test/compile-fail/mut-patterns.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// Copyright 2013 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+
// Can't put mut in non-ident pattern
12+
13+
pub fn main() {
14+
struct Foo { x: int }
15+
let mut Foo { x: x } = Foo { x: 3 }; //~ ERROR: expected `;` but found `{`
16+
}
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
// Copyright 2013 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+
trait Foo {
12+
fn foo(&self, mut x: int) -> int {
13+
let val = x;
14+
x = 37 * x;
15+
val + x
16+
}
17+
}
18+
19+
struct X;
20+
impl Foo for X {}
21+
22+
pub fn main() {
23+
let (a, mut b) = (23, 4);
24+
assert_eq!(a, 23);
25+
assert_eq!(b, 4);
26+
b = a + b;
27+
assert_eq!(b, 27);
28+
29+
30+
assert_eq!(X.foo(2), 76);
31+
32+
enum Bar {
33+
Foo(int),
34+
Baz(f32, u8)
35+
}
36+
37+
let (x, mut y) = (32, Foo(21));
38+
39+
match x {
40+
mut z @ 32 => {
41+
assert_eq!(z, 32);
42+
z = 34;
43+
assert_eq!(z, 34);
44+
}
45+
_ => {}
46+
}
47+
48+
check_bar(&y);
49+
y = Baz(10.0, 3);
50+
check_bar(&y);
51+
52+
fn check_bar(y: &Bar) {
53+
match y {
54+
&Foo(a) => {
55+
assert_eq!(a, 21);
56+
}
57+
&Baz(a, b) => {
58+
assert_eq!(a, 10.0);
59+
assert_eq!(b, 3);
60+
}
61+
}
62+
}
63+
64+
fn foo1((x, mut y): (f64, int), mut z: int) -> int {
65+
y = 2 * 6;
66+
z = y + (x as int);
67+
y - z
68+
}
69+
70+
struct A {
71+
x: int
72+
}
73+
let A { x: mut x } = A { x: 10 };
74+
assert_eq!(x, 10);
75+
x = 30;
76+
assert_eq!(x, 30);
77+
78+
(|A { x: mut t }: A| { t = t+1; t })(A { x: 34 });
79+
80+
}

0 commit comments

Comments
 (0)