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