]> git.lizzy.rs Git - rust.git/blob - src/test/compile-fail/lint-unused-mut-variables.rs
rustdoc: pretty-print Unevaluated expressions in types.
[rust.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     let mut a; //~ ERROR: variable does not need to be mutable
27     a = 3;
28
29     let mut b; //~ ERROR: variable does not need to be mutable
30     if true {
31         b = 3;
32     } else {
33         b = 4;
34     }
35
36     match 30 {
37         mut x => {} //~ ERROR: variable does not need to be mutable
38     }
39     match (30, 2) {
40       (mut x, 1) | //~ ERROR: variable does not need to be mutable
41       (mut x, 2) |
42       (mut x, 3) => {
43       }
44       _ => {}
45     }
46
47     let x = |mut y: isize| 10; //~ ERROR: variable does not need to be mutable
48     fn what(mut foo: isize) {} //~ ERROR: variable does not need to be mutable
49
50     let mut a = &mut 5; //~ ERROR: variable does not need to be mutable
51     *a = 4;
52
53     let mut a = 5;
54     let mut b = (&mut a,);
55     *b.0 = 4; //~^ ERROR: variable does not need to be mutable
56
57     let mut x = &mut 1; //~ ERROR: variable does not need to be mutable
58     let mut f = || {
59       *x += 1;
60     };
61     f();
62
63     fn mut_ref_arg(mut arg : &mut [u8]) -> &mut [u8] {
64         &mut arg[..] //~^ ERROR: variable does not need to be mutable
65     }
66
67     let mut v : &mut Vec<()> = &mut vec![]; //~ ERROR: variable does not need to be mutable
68     v.push(());
69
70     // positive cases
71     let mut a = 2;
72     a = 3;
73     let mut a = Vec::new();
74     a.push(3);
75     let mut a = Vec::new();
76     callback(|| {
77         a.push(3);
78     });
79     let (mut a, b) = (1, 2);
80     a = 34;
81
82     match 30 {
83         mut x => {
84             x = 21;
85         }
86     }
87
88     match (30, 2) {
89       (mut x, 1) |
90       (mut x, 2) |
91       (mut x, 3) => {
92         x = 21
93       }
94       _ => {}
95     }
96
97     let x = |mut y: isize| y = 32;
98     fn nothing(mut foo: isize) { foo = 37; }
99
100     // leading underscore should avoid the warning, just like the
101     // unused variable lint.
102     let mut _allowed = 1;
103 }
104
105 fn callback<F>(f: F) where F: FnOnce() {}
106
107 // make sure the lint attribute can be turned off
108 #[allow(unused_mut)]
109 fn foo(mut a: isize) {
110     let mut a = 3;
111     let mut b = vec![2];
112 }