]> git.proxmox.com Git - rustc.git/blob - src/tools/clippy/tests/ui/ptr_arg.rs
New upstream version 1.61.0+dfsg1
[rustc.git] / src / tools / clippy / 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 use std::path::PathBuf;
6
7 fn do_vec(x: &Vec<i64>) {
8 //Nothing here
9 }
10
11 fn do_vec_mut(x: &mut Vec<i64>) {
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 //Nothing here either
21 }
22
23 fn do_path(x: &PathBuf) {
24 //Nothing here either
25 }
26
27 fn do_path_mut(x: &mut PathBuf) {
28 //Nothing here either
29 }
30
31 fn main() {}
32
33 trait Foo {
34 type Item;
35 fn do_vec(x: &Vec<i64>);
36 fn do_item(x: &Self::Item);
37 }
38
39 struct Bar;
40
41 // no error, in trait impl (#425)
42 impl Foo for Bar {
43 type Item = Vec<u8>;
44 fn do_vec(x: &Vec<i64>) {}
45 fn do_item(x: &Vec<u8>) {}
46 }
47
48 fn cloned(x: &Vec<u8>) -> Vec<u8> {
49 let e = x.clone();
50 let f = e.clone(); // OK
51 let g = x;
52 let h = g.clone();
53 let i = (e).clone();
54 x.clone()
55 }
56
57 fn str_cloned(x: &String) -> String {
58 let a = x.clone();
59 let b = x.clone();
60 let c = b.clone();
61 let d = a.clone().clone().clone();
62 x.clone()
63 }
64
65 fn path_cloned(x: &PathBuf) -> PathBuf {
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 false_positive_capacity(x: &Vec<u8>, y: &String) {
74 let a = x.capacity();
75 let b = y.clone();
76 let c = y.as_str();
77 }
78
79 fn false_positive_capacity_too(x: &String) -> String {
80 if x.capacity() > 1024 {
81 panic!("Too large!");
82 }
83 x.clone()
84 }
85
86 #[allow(dead_code)]
87 fn test_cow_with_ref(c: &Cow<[i32]>) {}
88
89 fn test_cow(c: Cow<[i32]>) {
90 let _c = c;
91 }
92
93 trait Foo2 {
94 fn do_string(&self);
95 }
96
97 // no error for &self references where self is of type String (#2293)
98 impl Foo2 for String {
99 fn do_string(&self) {}
100 }
101
102 // Check that the allow attribute on parameters is honored
103 mod issue_5644 {
104 use std::borrow::Cow;
105 use std::path::PathBuf;
106
107 fn allowed(
108 #[allow(clippy::ptr_arg)] _v: &Vec<u32>,
109 #[allow(clippy::ptr_arg)] _s: &String,
110 #[allow(clippy::ptr_arg)] _p: &PathBuf,
111 #[allow(clippy::ptr_arg)] _c: &Cow<[i32]>,
112 ) {
113 }
114
115 struct S {}
116 impl S {
117 fn allowed(
118 #[allow(clippy::ptr_arg)] _v: &Vec<u32>,
119 #[allow(clippy::ptr_arg)] _s: &String,
120 #[allow(clippy::ptr_arg)] _p: &PathBuf,
121 #[allow(clippy::ptr_arg)] _c: &Cow<[i32]>,
122 ) {
123 }
124 }
125
126 trait T {
127 fn allowed(
128 #[allow(clippy::ptr_arg)] _v: &Vec<u32>,
129 #[allow(clippy::ptr_arg)] _s: &String,
130 #[allow(clippy::ptr_arg)] _p: &PathBuf,
131 #[allow(clippy::ptr_arg)] _c: &Cow<[i32]>,
132 ) {
133 }
134 }
135 }
136
137 mod issue6509 {
138 use std::path::PathBuf;
139
140 fn foo_vec(vec: &Vec<u8>) {
141 let _ = vec.clone().pop();
142 let _ = vec.clone().clone();
143 }
144
145 fn foo_path(path: &PathBuf) {
146 let _ = path.clone().pop();
147 let _ = path.clone().clone();
148 }
149
150 fn foo_str(str: &PathBuf) {
151 let _ = str.clone().pop();
152 let _ = str.clone().clone();
153 }
154 }
155
156 fn mut_vec_slice_methods(v: &mut Vec<u32>) {
157 v.copy_within(1..5, 10);
158 }
159
160 fn mut_vec_vec_methods(v: &mut Vec<u32>) {
161 v.clear();
162 }
163
164 fn vec_contains(v: &Vec<u32>) -> bool {
165 [vec![], vec![0]].as_slice().contains(v)
166 }
167
168 fn fn_requires_vec(v: &Vec<u32>) -> bool {
169 vec_contains(v)
170 }
171
172 fn impl_fn_requires_vec(v: &Vec<u32>, f: impl Fn(&Vec<u32>)) {
173 f(v);
174 }
175
176 fn dyn_fn_requires_vec(v: &Vec<u32>, f: &dyn Fn(&Vec<u32>)) {
177 f(v);
178 }
179
180 // No error for types behind an alias (#7699)
181 type A = Vec<u8>;
182 fn aliased(a: &A) {}
183
184 // Issue #8366
185 pub trait Trait {
186 fn f(v: &mut Vec<i32>);
187 fn f2(v: &mut Vec<i32>) {}
188 }
189
190 // Issue #8463
191 fn two_vecs(a: &mut Vec<u32>, b: &mut Vec<u32>) {
192 a.push(0);
193 a.push(0);
194 a.push(0);
195 b.push(1);
196 }
197
198 // Issue #8495
199 fn cow_conditional_to_mut(a: &mut Cow<str>) {
200 if a.is_empty() {
201 a.to_mut().push_str("foo");
202 }
203 }