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