-
Notifications
You must be signed in to change notification settings - Fork 12.1k
/
needless_pass_by_ref_mut.rs
254 lines (215 loc) · 5.65 KB
/
needless_pass_by_ref_mut.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
#![allow(clippy::if_same_then_else, clippy::no_effect)]
#![feature(lint_reasons)]
// just ignore everywhere for now
//@ignore-32bit
//@ignore-64bit
use std::ptr::NonNull;
fn foo(s: &mut Vec<u32>, b: &u32, x: &mut u32) {
//~^ ERROR: this argument is a mutable reference, but not used mutably
*x += *b + s.len() as u32;
}
// Should not warn.
fn foo2(s: &mut Vec<u32>) {
s.push(8);
}
// Should not warn because we return it.
fn foo3(s: &mut Vec<u32>) -> &mut Vec<u32> {
s
}
// Should not warn because `s` is used as mutable.
fn foo4(s: &mut Vec<u32>) {
Vec::push(s, 4);
}
// Should not warn.
fn foo5(s: &mut Vec<u32>) {
foo2(s);
}
fn foo6(s: &mut Vec<u32>) {
//~^ ERROR: this argument is a mutable reference, but not used mutably
non_mut_ref(s);
}
fn non_mut_ref(_: &Vec<u32>) {}
struct Bar;
impl Bar {
// Should not warn on `&mut self`.
fn bar(&mut self) {}
fn mushroom(&self, vec: &mut Vec<i32>) -> usize {
//~^ ERROR: this argument is a mutable reference, but not used mutably
vec.len()
}
fn badger(&mut self, vec: &mut Vec<i32>) -> usize {
//~^ ERROR: this argument is a mutable reference, but not used mutably
vec.len()
}
}
trait Babar {
// Should not warn here since it's a trait method.
fn method(arg: &mut u32);
}
impl Babar for Bar {
// Should not warn here since it's a trait method.
fn method(a: &mut u32) {}
}
// Should not warn (checking variable aliasing).
fn alias_check(s: &mut Vec<u32>) {
let mut alias = s;
let mut alias2 = alias;
let mut alias3 = alias2;
alias3.push(0);
}
// Should not warn (checking variable aliasing).
fn alias_check2(mut s: &mut Vec<u32>) {
let mut alias = &mut s;
alias.push(0);
}
struct Mut<T> {
ptr: NonNull<T>,
}
impl<T> Mut<T> {
// Should not warn because `NonNull::from` also accepts `&mut`.
fn new(ptr: &mut T) -> Self {
Mut {
ptr: NonNull::from(ptr),
}
}
}
// Should not warn.
fn unused(_: &mut u32, _b: &mut u8) {}
// Should not warn.
async fn f1(x: &mut i32) {
*x += 1;
}
// Should not warn.
async fn f2(x: &mut i32, y: String) {
*x += 1;
}
// Should not warn.
async fn f3(x: &mut i32, y: String, z: String) {
*x += 1;
}
// Should not warn.
async fn f4(x: &mut i32, y: i32) {
*x += 1;
}
// Should not warn.
async fn f5(x: i32, y: &mut i32) {
*y += 1;
}
// Should not warn.
async fn f6(x: i32, y: &mut i32, z: &mut i32) {
*y += 1;
*z += 1;
}
// Should not warn.
async fn f7(x: &mut i32, y: i32, z: &mut i32, a: i32) {
*x += 1;
*z += 1;
}
async fn a1(x: &mut i32) {
//~^ ERROR: this argument is a mutable reference, but not used mutably
println!("{:?}", x);
}
async fn a2(x: &mut i32, y: String) {
//~^ ERROR: this argument is a mutable reference, but not used mutably
println!("{:?}", x);
}
async fn a3(x: &mut i32, y: String, z: String) {
//~^ ERROR: this argument is a mutable reference, but not used mutably
println!("{:?}", x);
}
async fn a4(x: &mut i32, y: i32) {
//~^ ERROR: this argument is a mutable reference, but not used mutably
println!("{:?}", x);
}
async fn a5(x: i32, y: &mut i32) {
//~^ ERROR: this argument is a mutable reference, but not used mutably
println!("{:?}", x);
}
async fn a6(x: i32, y: &mut i32) {
//~^ ERROR: this argument is a mutable reference, but not used mutably
println!("{:?}", x);
}
async fn a7(x: i32, y: i32, z: &mut i32) {
//~^ ERROR: this argument is a mutable reference, but not used mutably
println!("{:?}", z);
}
async fn a8(x: i32, a: &mut i32, y: i32, z: &mut i32) {
//~^ ERROR: this argument is a mutable reference, but not used mutably
println!("{:?}", z);
}
// Should not warn (passed as closure which takes `&mut`).
fn passed_as_closure(s: &mut u32) {}
// Should not warn.
fn passed_as_local(s: &mut u32) {}
// Should not warn.
fn ty_unify_1(s: &mut u32) {}
// Should not warn.
fn ty_unify_2(s: &mut u32) {}
// Should not warn.
fn passed_as_field(s: &mut u32) {}
fn closure_takes_mut(s: fn(&mut u32)) {}
struct A {
s: fn(&mut u32),
}
// Should warn.
fn used_as_path(s: &mut u32) {}
// Make sure lint attributes work fine
#[expect(clippy::needless_pass_by_ref_mut)]
fn lint_attr(s: &mut u32) {}
#[cfg(not(feature = "a"))]
fn cfg_warn(s: &mut u32) {}
//~^ ERROR: this argument is a mutable reference, but not used mutably
//~| NOTE: this is cfg-gated and may require further changes
#[cfg(not(feature = "a"))]
mod foo {
fn cfg_warn(s: &mut u32) {}
//~^ ERROR: this argument is a mutable reference, but not used mutably
//~| NOTE: this is cfg-gated and may require further changes
}
// Should not warn.
async fn inner_async(x: &mut i32, y: &mut u32) {
async {
*y += 1;
*x += 1;
}
.await;
}
async fn inner_async2(x: &mut i32, y: &mut u32) {
//~^ ERROR: this argument is a mutable reference, but not used mutably
async {
*x += 1;
}
.await;
}
async fn inner_async3(x: &mut i32, y: &mut u32) {
//~^ ERROR: this argument is a mutable reference, but not used mutably
async {
*y += 1;
}
.await;
}
// Should not warn.
async fn async_vec(b: &mut Vec<bool>) {
b.append(&mut vec![]);
}
// Should not warn.
async fn async_vec2(b: &mut Vec<bool>) {
b.push(true);
}
fn main() {
let mut u = 0;
let mut v = vec![0];
foo(&mut v, &0, &mut u);
foo2(&mut v);
foo3(&mut v);
foo4(&mut v);
foo5(&mut v);
alias_check(&mut v);
alias_check2(&mut v);
println!("{u}");
closure_takes_mut(passed_as_closure);
A { s: passed_as_field };
used_as_path;
let _: fn(&mut u32) = passed_as_local;
let _ = if v[0] == 0 { ty_unify_1 } else { ty_unify_2 };
}