]> git.lizzy.rs Git - rust.git/blob - src/test/ui/lint/lint-unused-mut-variables.rs
Simplify SaveHandler trait
[rust.git] / src / test / ui / lint / lint-unused-mut-variables.rs
1 // Exercise the unused_mut attribute in some positive and negative cases
2
3 #![allow(unused_assignments)]
4 #![allow(unused_variables)]
5 #![allow(dead_code)]
6 #![deny(unused_mut)]
7
8
9 fn main() {
10     // negative cases
11     let mut a = 3; //~ ERROR: variable does not need to be mutable
12
13     let mut a = 2; //~ ERROR: variable does not need to be mutable
14
15     let mut b = 3; //~ ERROR: variable does not need to be mutable
16
17     let mut a = vec![3]; //~ ERROR: variable does not need to be mutable
18
19     let (mut a, b) = (1, 2); //~ ERROR: variable does not need to be mutable
20
21     let mut a; //~ ERROR: variable does not need to be mutable
22
23     a = 3;
24
25     let mut b; //~ ERROR: variable does not need to be mutable
26
27     if true {
28         b = 3;
29     } else {
30         b = 4;
31     }
32
33     match 30 {
34         mut x => {} //~ ERROR: variable does not need to be mutable
35
36     }
37     match (30, 2) {
38       (mut x, 1) | //~ ERROR: variable does not need to be mutable
39
40       (mut x, 2) |
41       (mut x, 3) => {
42       }
43       _ => {}
44     }
45
46     let x = |mut y: isize| 10; //~ ERROR: variable does not need to be mutable
47
48     fn what(mut foo: isize) {} //~ ERROR: variable does not need to be mutable
49
50
51     let mut a = &mut 5; //~ ERROR: variable does not need to be mutable
52
53     *a = 4;
54
55     let mut a = 5;
56     let mut b = (&mut a,); //~ ERROR: variable does not need to be mutable
57     *b.0 = 4;
58
59     let mut x = &mut 1; //~ ERROR: variable does not need to be mutable
60
61     let mut f = || {
62       *x += 1;
63     };
64     f();
65
66     fn mut_ref_arg(mut arg : &mut [u8]) -> &mut [u8] {
67         &mut arg[..] //~^ ERROR: variable does not need to be mutable
68
69     }
70
71     let mut v : &mut Vec<()> = &mut vec![]; //~ ERROR: variable does not need to be mutable
72
73     v.push(());
74
75     // positive cases
76     let mut a = 2;
77     a = 3;
78     let mut a = Vec::new();
79     a.push(3);
80     let mut a = Vec::new();
81     callback(|| {
82         a.push(3);
83     });
84     let mut a = Vec::new();
85     callback(|| {
86         callback(|| {
87             a.push(3);
88         });
89     });
90     let (mut a, b) = (1, 2);
91     a = 34;
92
93     match 30 {
94         mut x => {
95             x = 21;
96         }
97     }
98
99     match (30, 2) {
100       (mut x, 1) |
101       (mut x, 2) |
102       (mut x, 3) => {
103         x = 21
104       }
105       _ => {}
106     }
107
108     // Attribute should be respected on match arms
109     match 0 {
110         #[allow(unused_mut)]
111         mut x => {
112             let mut y = 1;
113         },
114     }
115
116     let x = |mut y: isize| y = 32;
117     fn nothing(mut foo: isize) { foo = 37; }
118
119     // leading underscore should avoid the warning, just like the
120     // unused variable lint.
121     let mut _allowed = 1;
122 }
123
124 fn callback<F>(f: F) where F: FnOnce() {}
125
126 // make sure the lint attribute can be turned off
127 #[allow(unused_mut)]
128 fn foo(mut a: isize) {
129     let mut a = 3;
130     let mut b = vec![2];
131 }
132
133 // make sure the lint attribute can be turned off on let statements
134 #[deny(unused_mut)]
135 fn bar() {
136     #[allow(unused_mut)]
137     let mut a = 3;
138     let mut b = vec![2]; //~ ERROR: variable does not need to be mutable
139
140 }