]> git.proxmox.com Git - rustc.git/blob - src/test/compile-fail/lint-unused-mut-variables.rs
dcc82b8920ff5b51245b6fb52880011da181e6fb
[rustc.git] / src / test / compile-fail / lint-unused-mut-variables.rs
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 // Exercise the unused_mut attribute in some positive and negative cases
12
13 #![allow(unused_assignments)]
14 #![allow(unused_variables)]
15 #![allow(dead_code)]
16 #![deny(unused_mut)]
17
18
19 fn main() {
20 // negative cases
21 let mut a = 3; //~ ERROR: variable does not need to be mutable
22 let mut a = 2; //~ ERROR: variable does not need to be mutable
23 let mut b = 3; //~ ERROR: variable does not need to be mutable
24 let mut a = vec!(3); //~ ERROR: variable does not need to be mutable
25 let (mut a, b) = (1, 2); //~ ERROR: variable does not need to be mutable
26
27 match 30 {
28 mut x => {} //~ ERROR: variable does not need to be mutable
29 }
30 match (30, 2) {
31 (mut x, 1) | //~ ERROR: variable does not need to be mutable
32 (mut x, 2) |
33 (mut x, 3) => {
34 }
35 _ => {}
36 }
37
38 let x = |mut y: isize| 10; //~ ERROR: variable does not need to be mutable
39 fn what(mut foo: isize) {} //~ ERROR: variable does not need to be mutable
40
41 // positive cases
42 let mut a = 2;
43 a = 3;
44 let mut a = Vec::new();
45 a.push(3);
46 let mut a = Vec::new();
47 callback(|| {
48 a.push(3);
49 });
50 let (mut a, b) = (1, 2);
51 a = 34;
52
53 match 30 {
54 mut x => {
55 x = 21;
56 }
57 }
58
59 match (30, 2) {
60 (mut x, 1) |
61 (mut x, 2) |
62 (mut x, 3) => {
63 x = 21
64 }
65 _ => {}
66 }
67
68 let x = |mut y: isize| y = 32;
69 fn nothing(mut foo: isize) { foo = 37; }
70
71 // leading underscore should avoid the warning, just like the
72 // unused variable lint.
73 let mut _allowed = 1;
74 }
75
76 fn callback<F>(f: F) where F: FnOnce() {}
77
78 // make sure the lint attribute can be turned off
79 #[allow(unused_mut)]
80 fn foo(mut a: isize) {
81 let mut a = 3;
82 let mut b = vec!(2);
83 }