]> git.lizzy.rs Git - rust.git/blob - tests/ui/ptr_arg.rs
Merge commit '7ea7cd165ad6705603852771bf82cc2fd6560db5' into clippyup2
[rust.git] / tests / ui / ptr_arg.rs
1 #![allow(unused, clippy::many_single_char_names, clippy::redundant_clone)]
2 #![warn(clippy::ptr_arg)]
3
4 use std::borrow::Cow;
5
6 fn do_vec(x: &Vec<i64>) {
7     //Nothing here
8 }
9
10 fn do_vec_mut(x: &mut Vec<i64>) {
11     // no error here
12     //Nothing here
13 }
14
15 fn do_str(x: &String) {
16     //Nothing here either
17 }
18
19 fn do_str_mut(x: &mut String) {
20     // no error here
21     //Nothing here either
22 }
23
24 fn main() {}
25
26 trait Foo {
27     type Item;
28     fn do_vec(x: &Vec<i64>);
29     fn do_item(x: &Self::Item);
30 }
31
32 struct Bar;
33
34 // no error, in trait impl (#425)
35 impl Foo for Bar {
36     type Item = Vec<u8>;
37     fn do_vec(x: &Vec<i64>) {}
38     fn do_item(x: &Vec<u8>) {}
39 }
40
41 fn cloned(x: &Vec<u8>) -> Vec<u8> {
42     let e = x.clone();
43     let f = e.clone(); // OK
44     let g = x;
45     let h = g.clone(); // Alas, we cannot reliably detect this without following data.
46     let i = (e).clone();
47     x.clone()
48 }
49
50 fn str_cloned(x: &String) -> String {
51     let a = x.clone();
52     let b = x.clone();
53     let c = b.clone();
54     let d = a.clone().clone().clone();
55     x.clone()
56 }
57
58 fn false_positive_capacity(x: &Vec<u8>, y: &String) {
59     let a = x.capacity();
60     let b = y.clone();
61     let c = y.as_str();
62 }
63
64 fn false_positive_capacity_too(x: &String) -> String {
65     if x.capacity() > 1024 {
66         panic!("Too large!");
67     }
68     x.clone()
69 }
70
71 #[allow(dead_code)]
72 fn test_cow_with_ref(c: &Cow<[i32]>) {}
73
74 fn test_cow(c: Cow<[i32]>) {
75     let _c = c;
76 }
77
78 trait Foo2 {
79     fn do_string(&self);
80 }
81
82 // no error for &self references where self is of type String (#2293)
83 impl Foo2 for String {
84     fn do_string(&self) {}
85 }
86
87 // Check that the allow attribute on parameters is honored
88 mod issue_5644 {
89     use std::borrow::Cow;
90
91     fn allowed(
92         #[allow(clippy::ptr_arg)] _v: &Vec<u32>,
93         #[allow(clippy::ptr_arg)] _s: &String,
94         #[allow(clippy::ptr_arg)] _c: &Cow<[i32]>,
95     ) {
96     }
97
98     struct S {}
99     impl S {
100         fn allowed(
101             #[allow(clippy::ptr_arg)] _v: &Vec<u32>,
102             #[allow(clippy::ptr_arg)] _s: &String,
103             #[allow(clippy::ptr_arg)] _c: &Cow<[i32]>,
104         ) {
105         }
106     }
107
108     trait T {
109         fn allowed(
110             #[allow(clippy::ptr_arg)] _v: &Vec<u32>,
111             #[allow(clippy::ptr_arg)] _s: &String,
112             #[allow(clippy::ptr_arg)] _c: &Cow<[i32]>,
113         ) {
114         }
115     }
116 }