]> git.lizzy.rs Git - rust.git/blob - tests/ui/lint/unused/lint-unused-mut-variables.rs
Rollup merge of #106670 - albertlarsan68:check-docs-in-pr-ci, r=Mark-Simulacrum
[rust.git] / tests / ui / lint / unused / 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
96     match (30, 2) {
97         // FIXME: Here's a false positive,
98         // shouldn't be removed `mut` not to be bound with a different way.
99         (mut x, 1) | //~ WARN: variable does not need to be mutable
100
101         (mut x, 2) |
102         (mut x, 3) => {
103         }
104         _ => {}
105     }
106
107     let x = |mut y: isize| 10; //~ WARN: variable does not need to be mutable
108
109     fn what(mut foo: isize) {} //~ WARN: variable does not need to be mutable
110
111
112     let mut a = &mut 5; //~ WARN: variable does not need to be mutable
113
114     *a = 4;
115
116     let mut a = 5;
117     let mut b = (&mut a,); //~ WARN: variable does not need to be mutable
118     *b.0 = 4;
119
120     let mut x = &mut 1; //~ WARN: variable does not need to be mutable
121
122     let mut f = || {
123       *x += 1;
124     };
125     f();
126
127     fn mut_ref_arg(mut arg : &mut [u8]) -> &mut [u8] {
128         &mut arg[..] //~^ WARN: variable does not need to be mutable
129
130     }
131
132     let mut v : &mut Vec<()> = &mut vec![]; //~ WARN: variable does not need to be mutable
133
134     v.push(());
135
136     // positive cases
137     let mut a = 2;
138     a = 3;
139     let mut a = Vec::new();
140     a.push(3);
141     let mut a = Vec::new();
142     callback(|| {
143         a.push(3);
144     });
145     let mut a = Vec::new();
146     callback(|| {
147         callback(|| {
148             a.push(3);
149         });
150     });
151     let (mut a, b) = (1, 2);
152     a = 34;
153
154     match 30 {
155         mut x => {
156             x = 21;
157         }
158     }
159
160     match (30, 2) {
161       (mut x, 1) |
162       (mut x, 2) |
163       (mut x, 3) => {
164         x = 21
165       }
166       _ => {}
167     }
168
169     // Attribute should be respected on match arms
170     match 0 {
171         #[allow(unused_mut)]
172         mut x => {
173             let mut y = 1;
174         },
175     }
176
177     let x = |mut y: isize| y = 32;
178     fn nothing(mut foo: isize) { foo = 37; }
179
180     // leading underscore should avoid the warning, just like the
181     // unused variable lint.
182     let mut _allowed = 1;
183
184     let mut raw_address_of_mut = 1; // OK
185     let mut_ptr = &raw mut raw_address_of_mut;
186
187     let mut raw_address_of_const = 1; //~ WARN: variable does not need to be mutable
188     let const_ptr = &raw const raw_address_of_const;
189 }
190
191 fn callback<F>(f: F) where F: FnOnce() {}
192
193 // make sure the lint attribute can be turned off
194 #[allow(unused_mut)]
195 fn foo(mut a: isize) {
196     let mut a = 3;
197     let mut b = vec![2];
198 }
199
200 // make sure the lint attribute can be turned off on let statements
201 #[deny(unused_mut)]
202 fn bar() {
203     #[allow(unused_mut)]
204     let mut a = 3;
205     let mut b = vec![2]; //~ ERROR: variable does not need to be mutable
206
207 }