]> git.lizzy.rs Git - rust.git/blob - src/test/ui/lint/lint-unused-mut-variables.rs
Rollup merge of #70140 - Nemo157:result-flatten, r=Amanieu
[rust.git] / src / test / ui / lint / lint-unused-mut-variables.rs
1 // edition:2018
2
3 // Exercise the unused_mut attribute in some positive and negative cases
4
5 #![warn(unused_mut)]
6 #![feature(async_closure, raw_ref_op)]
7
8 async fn baz_async(
9     mut a: i32,
10     //~^ WARN: variable does not need to be mutable
11     #[allow(unused_mut)] mut b: i32,
12 ) {}
13 fn baz(
14     mut a: i32,
15     //~^ WARN: variable does not need to be mutable
16     #[allow(unused_mut)] mut b: i32,
17     #[allow(unused_mut)] (mut c, d): (i32, i32)
18 ) {}
19
20 struct RefStruct {}
21 impl RefStruct {
22     async fn baz_async(
23         mut a: i32,
24         //~^ WARN: variable does not need to be mutable
25         #[allow(unused_mut)] mut b: i32,
26     ) {}
27     fn baz(
28         &self,
29         mut a: i32,
30         //~^ WARN: variable does not need to be mutable
31         #[allow(unused_mut)] mut b: i32,
32         #[allow(unused_mut)] (mut c, d): (i32, i32)
33     ) {}
34 }
35
36 trait RefTrait {
37     fn baz(
38         &self,
39         mut a: i32,
40         //~^ WARN: variable does not need to be mutable
41         #[allow(unused_mut)] mut b: i32,
42         #[allow(unused_mut)] (mut c, d): (i32, i32)
43     ) {}
44 }
45 impl RefTrait for () {
46     fn baz(
47         &self,
48         mut a: i32,
49         //~^ WARN: variable does not need to be mutable
50         #[allow(unused_mut)] mut b: i32,
51         #[allow(unused_mut)] (mut c, d): (i32, i32)
52     ) {}
53 }
54
55 fn main() {
56     let _ = async move |
57         mut a: i32,
58         //~^ WARN: variable does not need to be mutable
59         #[allow(unused_mut)] mut b: i32,
60     | {};
61     let _ = |
62         mut a: i32,
63         //~^ WARN: variable does not need to be mutable
64         #[allow(unused_mut)] mut b: i32,
65         #[allow(unused_mut)] (mut c, d): (i32, i32)
66     | {};
67
68     // negative cases
69     let mut a = 3; //~ WARN: variable does not need to be mutable
70
71     let mut a = 2; //~ WARN: variable does not need to be mutable
72
73     let mut b = 3; //~ WARN: variable does not need to be mutable
74
75     let mut a = vec![3]; //~ WARN: variable does not need to be mutable
76
77     let (mut a, b) = (1, 2); //~ WARN: variable does not need to be mutable
78
79     let mut a; //~ WARN: variable does not need to be mutable
80
81     a = 3;
82
83     let mut b; //~ WARN: variable does not need to be mutable
84
85     if true {
86         b = 3;
87     } else {
88         b = 4;
89     }
90
91     match 30 {
92         mut x => {} //~ WARN: variable does not need to be mutable
93
94     }
95     match (30, 2) {
96       (mut x, 1) | //~ WARN: variable does not need to be mutable
97
98       (mut x, 2) |
99       (mut x, 3) => {
100       }
101       _ => {}
102     }
103
104     let x = |mut y: isize| 10; //~ WARN: variable does not need to be mutable
105
106     fn what(mut foo: isize) {} //~ WARN: variable does not need to be mutable
107
108
109     let mut a = &mut 5; //~ WARN: variable does not need to be mutable
110
111     *a = 4;
112
113     let mut a = 5;
114     let mut b = (&mut a,); //~ WARN: variable does not need to be mutable
115     *b.0 = 4;
116
117     let mut x = &mut 1; //~ WARN: variable does not need to be mutable
118
119     let mut f = || {
120       *x += 1;
121     };
122     f();
123
124     fn mut_ref_arg(mut arg : &mut [u8]) -> &mut [u8] {
125         &mut arg[..] //~^ WARN: variable does not need to be mutable
126
127     }
128
129     let mut v : &mut Vec<()> = &mut vec![]; //~ WARN: variable does not need to be mutable
130
131     v.push(());
132
133     // positive cases
134     let mut a = 2;
135     a = 3;
136     let mut a = Vec::new();
137     a.push(3);
138     let mut a = Vec::new();
139     callback(|| {
140         a.push(3);
141     });
142     let mut a = Vec::new();
143     callback(|| {
144         callback(|| {
145             a.push(3);
146         });
147     });
148     let (mut a, b) = (1, 2);
149     a = 34;
150
151     match 30 {
152         mut x => {
153             x = 21;
154         }
155     }
156
157     match (30, 2) {
158       (mut x, 1) |
159       (mut x, 2) |
160       (mut x, 3) => {
161         x = 21
162       }
163       _ => {}
164     }
165
166     // Attribute should be respected on match arms
167     match 0 {
168         #[allow(unused_mut)]
169         mut x => {
170             let mut y = 1;
171         },
172     }
173
174     let x = |mut y: isize| y = 32;
175     fn nothing(mut foo: isize) { foo = 37; }
176
177     // leading underscore should avoid the warning, just like the
178     // unused variable lint.
179     let mut _allowed = 1;
180
181     let mut raw_address_of_mut = 1; // OK
182     let mut_ptr = &raw mut raw_address_of_mut;
183
184     let mut raw_address_of_const = 1; //~ WARN: variable does not need to be mutable
185     let const_ptr = &raw const raw_address_of_const;
186 }
187
188 fn callback<F>(f: F) where F: FnOnce() {}
189
190 // make sure the lint attribute can be turned off
191 #[allow(unused_mut)]
192 fn foo(mut a: isize) {
193     let mut a = 3;
194     let mut b = vec![2];
195 }
196
197 // make sure the lint attribute can be turned off on let statements
198 #[deny(unused_mut)]
199 fn bar() {
200     #[allow(unused_mut)]
201     let mut a = 3;
202     let mut b = vec![2]; //~ ERROR: variable does not need to be mutable
203
204 }