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