]>
Commit | Line | Data |
---|---|---|
416331ca XL |
1 | // edition:2018 |
2 | ||
970d7e83 LB |
3 | // Exercise the unused_mut attribute in some positive and negative cases |
4 | ||
1a4d82fc | 5 | #![deny(unused_mut)] |
e1599b0c | 6 | #![feature(async_closure)] |
416331ca XL |
7 | |
8 | async fn baz_async( | |
9 | mut a: i32, | |
10 | //~^ ERROR: variable does not need to be mutable | |
11 | #[allow(unused_mut)] mut b: i32, | |
12 | ) {} | |
13 | fn baz( | |
14 | mut a: i32, | |
15 | //~^ ERROR: variable does not need to be mutable | |
16 | #[allow(unused_mut)] mut b: i32, | |
17 | #[allow(unused_mut)] (mut c, d): (i32, i32) | |
18 | ) {} | |
1a4d82fc | 19 | |
416331ca XL |
20 | struct RefStruct {} |
21 | impl RefStruct { | |
22 | async fn baz_async( | |
23 | mut a: i32, | |
24 | //~^ ERROR: 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 | //~^ ERROR: 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 | //~^ ERROR: 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 | //~^ ERROR: 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 | } | |
970d7e83 LB |
54 | |
55 | fn main() { | |
416331ca XL |
56 | let _ = async move | |
57 | mut a: i32, | |
58 | //~^ ERROR: variable does not need to be mutable | |
59 | #[allow(unused_mut)] mut b: i32, | |
60 | | {}; | |
61 | let _ = | | |
62 | mut a: i32, | |
63 | //~^ ERROR: 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 | ||
970d7e83 | 68 | // negative cases |
a1dfa0c6 XL |
69 | let mut a = 3; //~ ERROR: variable does not need to be mutable |
70 | ||
71 | let mut a = 2; //~ ERROR: variable does not need to be mutable | |
72 | ||
73 | let mut b = 3; //~ ERROR: variable does not need to be mutable | |
74 | ||
75 | let mut a = vec![3]; //~ ERROR: variable does not need to be mutable | |
76 | ||
77 | let (mut a, b) = (1, 2); //~ ERROR: variable does not need to be mutable | |
78 | ||
79 | let mut a; //~ ERROR: variable does not need to be mutable | |
80 | ||
c1a9b12d SL |
81 | a = 3; |
82 | ||
a1dfa0c6 XL |
83 | let mut b; //~ ERROR: variable does not need to be mutable |
84 | ||
c1a9b12d SL |
85 | if true { |
86 | b = 3; | |
87 | } else { | |
88 | b = 4; | |
89 | } | |
1a4d82fc | 90 | |
85aaf69f | 91 | match 30 { |
a1dfa0c6 XL |
92 | mut x => {} //~ ERROR: variable does not need to be mutable |
93 | ||
1a4d82fc | 94 | } |
85aaf69f | 95 | match (30, 2) { |
a1dfa0c6 XL |
96 | (mut x, 1) | //~ ERROR: variable does not need to be mutable |
97 | ||
1a4d82fc JJ |
98 | (mut x, 2) | |
99 | (mut x, 3) => { | |
100 | } | |
101 | _ => {} | |
102 | } | |
103 | ||
a1dfa0c6 XL |
104 | let x = |mut y: isize| 10; //~ ERROR: variable does not need to be mutable |
105 | ||
106 | fn what(mut foo: isize) {} //~ ERROR: variable does not need to be mutable | |
107 | ||
108 | ||
109 | let mut a = &mut 5; //~ ERROR: variable does not need to be mutable | |
970d7e83 | 110 | |
3b2f2976 XL |
111 | *a = 4; |
112 | ||
113 | let mut a = 5; | |
a1dfa0c6 XL |
114 | let mut b = (&mut a,); //~ ERROR: variable does not need to be mutable |
115 | *b.0 = 4; | |
116 | ||
117 | let mut x = &mut 1; //~ ERROR: variable does not need to be mutable | |
3b2f2976 | 118 | |
3b2f2976 XL |
119 | let mut f = || { |
120 | *x += 1; | |
121 | }; | |
122 | f(); | |
123 | ||
124 | fn mut_ref_arg(mut arg : &mut [u8]) -> &mut [u8] { | |
a1dfa0c6 XL |
125 | &mut arg[..] //~^ ERROR: variable does not need to be mutable |
126 | ||
3b2f2976 XL |
127 | } |
128 | ||
a1dfa0c6 XL |
129 | let mut v : &mut Vec<()> = &mut vec![]; //~ ERROR: variable does not need to be mutable |
130 | ||
3b2f2976 XL |
131 | v.push(()); |
132 | ||
970d7e83 | 133 | // positive cases |
85aaf69f SL |
134 | let mut a = 2; |
135 | a = 3; | |
1a4d82fc | 136 | let mut a = Vec::new(); |
85aaf69f | 137 | a.push(3); |
1a4d82fc JJ |
138 | let mut a = Vec::new(); |
139 | callback(|| { | |
85aaf69f | 140 | a.push(3); |
1a4d82fc | 141 | }); |
83c7162d XL |
142 | let mut a = Vec::new(); |
143 | callback(|| { | |
144 | callback(|| { | |
145 | a.push(3); | |
146 | }); | |
147 | }); | |
85aaf69f | 148 | let (mut a, b) = (1, 2); |
1a4d82fc JJ |
149 | a = 34; |
150 | ||
85aaf69f | 151 | match 30 { |
1a4d82fc | 152 | mut x => { |
85aaf69f | 153 | x = 21; |
1a4d82fc JJ |
154 | } |
155 | } | |
156 | ||
85aaf69f | 157 | match (30, 2) { |
1a4d82fc JJ |
158 | (mut x, 1) | |
159 | (mut x, 2) | | |
160 | (mut x, 3) => { | |
161 | x = 21 | |
162 | } | |
163 | _ => {} | |
970d7e83 | 164 | } |
1a4d82fc | 165 | |
dc9dc135 XL |
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 | ||
85aaf69f SL |
174 | let x = |mut y: isize| y = 32; |
175 | fn nothing(mut foo: isize) { foo = 37; } | |
1a4d82fc JJ |
176 | |
177 | // leading underscore should avoid the warning, just like the | |
178 | // unused variable lint. | |
85aaf69f | 179 | let mut _allowed = 1; |
970d7e83 LB |
180 | } |
181 | ||
1a4d82fc | 182 | fn callback<F>(f: F) where F: FnOnce() {} |
970d7e83 LB |
183 | |
184 | // make sure the lint attribute can be turned off | |
185 | #[allow(unused_mut)] | |
1a4d82fc | 186 | fn foo(mut a: isize) { |
85aaf69f | 187 | let mut a = 3; |
c30ab7b3 | 188 | let mut b = vec![2]; |
970d7e83 | 189 | } |
ea8adc8c XL |
190 | |
191 | // make sure the lint attribute can be turned off on let statements | |
192 | #[deny(unused_mut)] | |
193 | fn bar() { | |
194 | #[allow(unused_mut)] | |
195 | let mut a = 3; | |
a1dfa0c6 XL |
196 | let mut b = vec![2]; //~ ERROR: variable does not need to be mutable |
197 | ||
ea8adc8c | 198 | } |