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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
|
#![allow(
unused,
clippy::many_single_char_names,
clippy::needless_lifetimes,
clippy::redundant_clone,
clippy::needless_pass_by_ref_mut
)]
#![warn(clippy::ptr_arg)]
//@no-rustfix
use std::borrow::Cow;
use std::path::{Path, PathBuf};
fn do_vec(x: &Vec<i64>) {
//~^ ptr_arg
//Nothing here
}
fn do_vec_mut(x: &mut Vec<i64>) {
//~^ ptr_arg
//Nothing here
}
fn do_vec_mut2(x: &mut Vec<i64>) {
//~^ ptr_arg
x.len();
x.is_empty();
}
fn do_str(x: &String) {
//~^ ptr_arg
//Nothing here either
}
fn do_str_mut(x: &mut String) {
//~^ ptr_arg
//Nothing here either
}
fn do_path(x: &PathBuf) {
//~^ ptr_arg
//Nothing here either
}
fn do_path_mut(x: &mut PathBuf) {
//~^ ptr_arg
//Nothing here either
}
fn main() {}
trait Foo {
type Item;
fn do_vec(x: &Vec<i64>);
//~^ ptr_arg
fn do_item(x: &Self::Item);
}
struct Bar;
// no error, in trait impl (#425)
impl Foo for Bar {
type Item = Vec<u8>;
fn do_vec(x: &Vec<i64>) {}
fn do_item(x: &Vec<u8>) {}
}
fn cloned(x: &Vec<u8>) -> Vec<u8> {
//~^ ptr_arg
let e = x.clone();
let f = e.clone(); // OK
let g = x;
let h = g.clone();
let i = (e).clone();
x.clone()
}
fn str_cloned(x: &String) -> String {
//~^ ptr_arg
let a = x.clone();
let b = x.clone();
let c = b.clone();
let d = a.clone().clone().clone();
x.clone()
}
fn path_cloned(x: &PathBuf) -> PathBuf {
//~^ ptr_arg
let a = x.clone();
let b = x.clone();
let c = b.clone();
let d = a.clone().clone().clone();
x.clone()
}
fn false_positive_capacity(x: &Vec<u8>, y: &String) {
//~^ ptr_arg
let a = x.capacity();
let b = y.clone();
let c = y.as_str();
}
fn false_positive_capacity_too(x: &String) -> String {
if x.capacity() > 1024 {
panic!("Too large!");
}
x.clone()
}
#[allow(dead_code)]
fn test_cow_with_ref(c: &Cow<[i32]>) {}
//~^ ptr_arg
fn test_cow(c: Cow<[i32]>) {
let d = c;
}
trait Foo2 {
fn do_string(&self);
}
// no error for &self references where self is of type String (#2293)
impl Foo2 for String {
fn do_string(&self) {}
}
// Check that the allow attribute on parameters is honored
mod issue_5644 {
use std::borrow::Cow;
use std::path::PathBuf;
fn allowed(
#[allow(clippy::ptr_arg)] v: &Vec<u32>,
#[allow(clippy::ptr_arg)] s: &String,
#[allow(clippy::ptr_arg)] p: &PathBuf,
#[allow(clippy::ptr_arg)] c: &Cow<[i32]>,
#[expect(clippy::ptr_arg)] expect: &Cow<[i32]>,
) {
}
fn some_allowed(#[allow(clippy::ptr_arg)] v: &Vec<u32>, s: &String) {}
//~^ ptr_arg
struct S;
impl S {
fn allowed(
#[allow(clippy::ptr_arg)] v: &Vec<u32>,
#[allow(clippy::ptr_arg)] s: &String,
#[allow(clippy::ptr_arg)] p: &PathBuf,
#[allow(clippy::ptr_arg)] c: &Cow<[i32]>,
#[expect(clippy::ptr_arg)] expect: &Cow<[i32]>,
) {
}
}
trait T {
fn allowed(
#[allow(clippy::ptr_arg)] v: &Vec<u32>,
#[allow(clippy::ptr_arg)] s: &String,
#[allow(clippy::ptr_arg)] p: &PathBuf,
#[allow(clippy::ptr_arg)] c: &Cow<[i32]>,
#[expect(clippy::ptr_arg)] expect: &Cow<[i32]>,
) {
}
}
}
mod issue6509 {
use std::path::PathBuf;
fn foo_vec(vec: &Vec<u8>) {
//~^ ptr_arg
let a = vec.clone().pop();
let b = vec.clone().clone();
}
fn foo_path(path: &PathBuf) {
//~^ ptr_arg
let c = path.clone().pop();
let d = path.clone().clone();
}
fn foo_str(str: &String) {
//~^ ptr_arg
let e = str.clone().pop();
let f = str.clone().clone();
}
}
fn mut_vec_slice_methods(v: &mut Vec<u32>) {
//~^ ptr_arg
v.copy_within(1..5, 10);
}
fn mut_vec_vec_methods(v: &mut Vec<u32>) {
v.clear();
}
fn vec_contains(v: &Vec<u32>) -> bool {
[vec![], vec![0]].as_slice().contains(v)
}
fn fn_requires_vec(v: &Vec<u32>) -> bool {
vec_contains(v)
}
fn impl_fn_requires_vec(v: &Vec<u32>, f: impl Fn(&Vec<u32>)) {
f(v);
}
fn dyn_fn_requires_vec(v: &Vec<u32>, f: &dyn Fn(&Vec<u32>)) {
f(v);
}
// No error for types behind an alias (#7699)
type A = Vec<u8>;
fn aliased(a: &A) {}
// Issue #8366
pub trait Trait {
fn f(v: &mut Vec<i32>);
fn f2(v: &mut Vec<i32>) {}
}
// Issue #8463
fn two_vecs(a: &mut Vec<u32>, b: &mut Vec<u32>) {
a.push(0);
a.push(0);
a.push(0);
b.push(1);
}
// Issue #8495
fn cow_conditional_to_mut(a: &mut Cow<str>) {
if a.is_empty() {
a.to_mut().push_str("foo");
}
}
// Issue #9542
fn dyn_trait_ok(a: &mut Vec<u32>, b: &mut String, c: &mut PathBuf) {
trait T {}
impl<U> T for Vec<U> {}
impl T for String {}
impl T for PathBuf {}
fn takes_dyn(_: &mut dyn T) {}
takes_dyn(a);
takes_dyn(b);
takes_dyn(c);
}
fn dyn_trait(a: &mut Vec<u32>, b: &mut String, c: &mut PathBuf) {
//~^ ptr_arg
//~| ptr_arg
//~| ptr_arg
trait T {}
impl<U> T for Vec<U> {}
impl<U> T for [U] {}
impl T for String {}
impl T for str {}
impl T for PathBuf {}
impl T for Path {}
fn takes_dyn(_: &mut dyn T) {}
takes_dyn(a);
takes_dyn(b);
takes_dyn(c);
}
mod issue_9218 {
use std::borrow::Cow;
fn cow_non_elided_lifetime<'a>(input: &Cow<'a, str>) -> &'a str {
todo!()
}
// This one has an anonymous lifetime so it's not okay
fn cow_elided_lifetime<'a>(input: &'a Cow<str>) -> &'a str {
//~^ ptr_arg
todo!()
}
// These two's return types don't use 'a so it's not okay
fn cow_bad_ret_ty_1<'a>(input: &'a Cow<'a, str>) -> &'static str {
//~^ ptr_arg
todo!()
}
fn cow_bad_ret_ty_2<'a, 'b>(input: &'a Cow<'a, str>) -> &'b str {
//~^ ptr_arg
todo!()
}
// Inferred to be `&'a str`, afaik.
fn cow_good_ret_ty<'a>(input: &'a Cow<'a, str>) -> &str {
//~^ ERROR: eliding a lifetime that's named elsewhere is confusing
todo!()
}
}
mod issue_11181 {
extern "C" fn allowed(_v: &Vec<u32>) {}
struct S;
impl S {
extern "C" fn allowed(_v: &Vec<u32>) {}
}
trait T {
extern "C" fn allowed(_v: &Vec<u32>) {}
}
}
mod issue_13308 {
use std::ops::Deref;
fn repro(source: &str, destination: &mut String) {
source.clone_into(destination);
}
fn repro2(source: &str, destination: &mut String) {
ToOwned::clone_into(source, destination);
}
fn h1(x: &<String as Deref>::Target) {}
fn h2<T: Deref>(x: T, y: &T::Target) {}
// Other cases that are still ok to lint and ideally shouldn't regress
fn good(v1: &String, v2: &String) {
//~^ ptr_arg
//~| ptr_arg
h1(v1);
h2(String::new(), v2);
}
}
mod issue_13489_and_13728 {
// This is a no-lint from now on.
fn foo(_x: &Vec<i32>) {
todo!();
}
// But this still gives us a lint.
fn foo_used(x: &Vec<i32>) {
//~^ ptr_arg
todo!();
}
// This is also a no-lint from now on.
fn foo_local(x: &Vec<i32>) {
let _y = x;
todo!();
}
// But this still gives us a lint.
fn foo_local_used(x: &Vec<i32>) {
//~^ ptr_arg
let y = x;
todo!();
}
// This only lints once from now on.
fn foofoo(_x: &Vec<i32>, y: &String) {
//~^ ptr_arg
todo!();
}
// And this is also a no-lint from now on.
fn foofoo_local(_x: &Vec<i32>, y: &String) {
let _z = y;
todo!();
}
}
mod issue_13489_and_13728_mut {
// This is a no-lint from now on.
fn bar(_x: &mut Vec<u32>) {
todo!()
}
// But this still gives us a lint.
fn bar_used(x: &mut Vec<u32>) {
//~^ ptr_arg
todo!()
}
// This is also a no-lint from now on.
fn bar_local(x: &mut Vec<u32>) {
let _y = x;
todo!()
}
// But this still gives us a lint.
fn bar_local_used(x: &mut Vec<u32>) {
//~^ ptr_arg
let y = x;
todo!()
}
// This only lints once from now on.
fn barbar(_x: &mut Vec<u32>, y: &mut String) {
//~^ ptr_arg
todo!()
}
// And this is also a no-lint from now on.
fn barbar_local(_x: &mut Vec<u32>, y: &mut String) {
let _z = y;
todo!()
}
}
|