]> git.lizzy.rs Git - rust.git/blob - tests/ui/ptr_arg.rs
99e6d2aad8dd6b46ac5229da0866d36218688f0a
[rust.git] / tests / ui / ptr_arg.rs
1 #![allow(
2     unused,
3     clippy::many_single_char_names,
4     clippy::redundant_clone,
5     clippy::if_then_panic
6 )]
7 #![warn(clippy::ptr_arg)]
8
9 use std::borrow::Cow;
10 use std::path::PathBuf;
11
12 fn do_vec(x: &Vec<i64>) {
13     //Nothing here
14 }
15
16 fn do_vec_mut(x: &mut Vec<i64>) {
17     // no error here
18     //Nothing here
19 }
20
21 fn do_str(x: &String) {
22     //Nothing here either
23 }
24
25 fn do_str_mut(x: &mut String) {
26     // no error here
27     //Nothing here either
28 }
29
30 fn do_path(x: &PathBuf) {
31     //Nothing here either
32 }
33
34 fn do_path_mut(x: &mut PathBuf) {
35     // no error here
36     //Nothing here either
37 }
38
39 fn main() {}
40
41 trait Foo {
42     type Item;
43     fn do_vec(x: &Vec<i64>);
44     fn do_item(x: &Self::Item);
45 }
46
47 struct Bar;
48
49 // no error, in trait impl (#425)
50 impl Foo for Bar {
51     type Item = Vec<u8>;
52     fn do_vec(x: &Vec<i64>) {}
53     fn do_item(x: &Vec<u8>) {}
54 }
55
56 fn cloned(x: &Vec<u8>) -> Vec<u8> {
57     let e = x.clone();
58     let f = e.clone(); // OK
59     let g = x;
60     let h = g.clone(); // Alas, we cannot reliably detect this without following data.
61     let i = (e).clone();
62     x.clone()
63 }
64
65 fn str_cloned(x: &String) -> String {
66     let a = x.clone();
67     let b = x.clone();
68     let c = b.clone();
69     let d = a.clone().clone().clone();
70     x.clone()
71 }
72
73 fn path_cloned(x: &PathBuf) -> PathBuf {
74     let a = x.clone();
75     let b = x.clone();
76     let c = b.clone();
77     let d = a.clone().clone().clone();
78     x.clone()
79 }
80
81 fn false_positive_capacity(x: &Vec<u8>, y: &String) {
82     let a = x.capacity();
83     let b = y.clone();
84     let c = y.as_str();
85 }
86
87 fn false_positive_capacity_too(x: &String) -> String {
88     if x.capacity() > 1024 {
89         panic!("Too large!");
90     }
91     x.clone()
92 }
93
94 #[allow(dead_code)]
95 fn test_cow_with_ref(c: &Cow<[i32]>) {}
96
97 fn test_cow(c: Cow<[i32]>) {
98     let _c = c;
99 }
100
101 trait Foo2 {
102     fn do_string(&self);
103 }
104
105 // no error for &self references where self is of type String (#2293)
106 impl Foo2 for String {
107     fn do_string(&self) {}
108 }
109
110 // Check that the allow attribute on parameters is honored
111 mod issue_5644 {
112     use std::borrow::Cow;
113     use std::path::PathBuf;
114
115     fn allowed(
116         #[allow(clippy::ptr_arg)] _v: &Vec<u32>,
117         #[allow(clippy::ptr_arg)] _s: &String,
118         #[allow(clippy::ptr_arg)] _p: &PathBuf,
119         #[allow(clippy::ptr_arg)] _c: &Cow<[i32]>,
120     ) {
121     }
122
123     struct S {}
124     impl S {
125         fn allowed(
126             #[allow(clippy::ptr_arg)] _v: &Vec<u32>,
127             #[allow(clippy::ptr_arg)] _s: &String,
128             #[allow(clippy::ptr_arg)] _p: &PathBuf,
129             #[allow(clippy::ptr_arg)] _c: &Cow<[i32]>,
130         ) {
131         }
132     }
133
134     trait T {
135         fn allowed(
136             #[allow(clippy::ptr_arg)] _v: &Vec<u32>,
137             #[allow(clippy::ptr_arg)] _s: &String,
138             #[allow(clippy::ptr_arg)] _p: &PathBuf,
139             #[allow(clippy::ptr_arg)] _c: &Cow<[i32]>,
140         ) {
141         }
142     }
143 }
144
145 mod issue6509 {
146     use std::path::PathBuf;
147
148     fn foo_vec(vec: &Vec<u8>) {
149         let _ = vec.clone().pop();
150         let _ = vec.clone().clone();
151     }
152
153     fn foo_path(path: &PathBuf) {
154         let _ = path.clone().pop();
155         let _ = path.clone().clone();
156     }
157
158     fn foo_str(str: &PathBuf) {
159         let _ = str.clone().pop();
160         let _ = str.clone().clone();
161     }
162 }