]> git.lizzy.rs Git - rust.git/blob - tests/ui/unnecessary_to_owned.fixed
removing unsafe from test fn's && renaming shrink to sugg_span
[rust.git] / tests / ui / unnecessary_to_owned.fixed
1 // run-rustfix
2
3 #![allow(clippy::ptr_arg)]
4 #![warn(clippy::unnecessary_to_owned)]
5
6 use std::borrow::Cow;
7 use std::ffi::{CStr, CString, OsStr, OsString};
8 use std::ops::Deref;
9
10 #[derive(Clone)]
11 struct X(String);
12
13 impl Deref for X {
14     type Target = [u8];
15     fn deref(&self) -> &[u8] {
16         self.0.as_bytes()
17     }
18 }
19
20 impl AsRef<str> for X {
21     fn as_ref(&self) -> &str {
22         self.0.as_str()
23     }
24 }
25
26 impl ToString for X {
27     fn to_string(&self) -> String {
28         self.0.to_string()
29     }
30 }
31
32 impl X {
33     fn join(&self, other: impl AsRef<str>) -> Self {
34         let mut s = self.0.clone();
35         s.push_str(other.as_ref());
36         Self(s)
37     }
38 }
39
40 #[allow(dead_code)]
41 #[derive(Clone)]
42 enum FileType {
43     Account,
44     PrivateKey,
45     Certificate,
46 }
47
48 fn main() {
49     let c_str = CStr::from_bytes_with_nul(&[0]).unwrap();
50     let os_str = OsStr::new("x");
51     let path = std::path::Path::new("x");
52     let s = "x";
53     let array = ["x"];
54     let array_ref = &["x"];
55     let slice = &["x"][..];
56     let x = X(String::from("x"));
57     let x_ref = &x;
58
59     require_c_str(&Cow::from(c_str));
60     require_c_str(c_str);
61
62     require_os_str(os_str);
63     require_os_str(&Cow::from(os_str));
64     require_os_str(os_str);
65
66     require_path(path);
67     require_path(&Cow::from(path));
68     require_path(path);
69
70     require_str(s);
71     require_str(&Cow::from(s));
72     require_str(s);
73     require_str(x_ref.as_ref());
74
75     require_slice(slice);
76     require_slice(&Cow::from(slice));
77     require_slice(array.as_ref());
78     require_slice(array_ref.as_ref());
79     require_slice(slice);
80     require_slice(x_ref);
81
82     require_x(&Cow::<X>::Owned(x.clone()));
83     require_x(x_ref);
84
85     require_deref_c_str(c_str);
86     require_deref_os_str(os_str);
87     require_deref_path(path);
88     require_deref_str(s);
89     require_deref_slice(slice);
90
91     require_impl_deref_c_str(c_str);
92     require_impl_deref_os_str(os_str);
93     require_impl_deref_path(path);
94     require_impl_deref_str(s);
95     require_impl_deref_slice(slice);
96
97     require_deref_str_slice(s, slice);
98     require_deref_slice_str(slice, s);
99
100     require_as_ref_c_str(c_str);
101     require_as_ref_os_str(os_str);
102     require_as_ref_path(path);
103     require_as_ref_str(s);
104     require_as_ref_str(&x);
105     require_as_ref_slice(array);
106     require_as_ref_slice(array_ref);
107     require_as_ref_slice(slice);
108
109     require_impl_as_ref_c_str(c_str);
110     require_impl_as_ref_os_str(os_str);
111     require_impl_as_ref_path(path);
112     require_impl_as_ref_str(s);
113     require_impl_as_ref_str(&x);
114     require_impl_as_ref_slice(array);
115     require_impl_as_ref_slice(array_ref);
116     require_impl_as_ref_slice(slice);
117
118     require_as_ref_str_slice(s, array);
119     require_as_ref_str_slice(s, array_ref);
120     require_as_ref_str_slice(s, slice);
121     require_as_ref_slice_str(array, s);
122     require_as_ref_slice_str(array_ref, s);
123     require_as_ref_slice_str(slice, s);
124
125     let _ = x.join(x_ref);
126
127     let _ = slice.iter().copied();
128     let _ = slice.iter().copied();
129     let _ = [std::path::PathBuf::new()][..].iter().cloned();
130     let _ = [std::path::PathBuf::new()][..].iter().cloned();
131
132     let _ = slice.iter().copied();
133     let _ = slice.iter().copied();
134     let _ = [std::path::PathBuf::new()][..].iter().cloned();
135     let _ = [std::path::PathBuf::new()][..].iter().cloned();
136
137     let _ = check_files(&[FileType::Account]);
138
139     // negative tests
140     require_string(&s.to_string());
141     require_string(&Cow::from(s).into_owned());
142     require_string(&s.to_owned());
143     require_string(&x_ref.to_string());
144
145     // `X` isn't copy.
146     require_slice(&x.to_owned());
147     require_deref_slice(x.to_owned());
148
149     // The following should be flagged by `redundant_clone`, but not by this lint.
150     require_c_str(&CString::from_vec_with_nul(vec![0]).unwrap());
151     require_os_str(&OsString::from("x"));
152     require_path(&std::path::PathBuf::from("x"));
153     require_str(&String::from("x"));
154 }
155
156 fn require_c_str(_: &CStr) {}
157 fn require_os_str(_: &OsStr) {}
158 fn require_path(_: &std::path::Path) {}
159 fn require_str(_: &str) {}
160 fn require_slice<T>(_: &[T]) {}
161 fn require_x(_: &X) {}
162
163 fn require_deref_c_str<T: Deref<Target = CStr>>(_: T) {}
164 fn require_deref_os_str<T: Deref<Target = OsStr>>(_: T) {}
165 fn require_deref_path<T: Deref<Target = std::path::Path>>(_: T) {}
166 fn require_deref_str<T: Deref<Target = str>>(_: T) {}
167 fn require_deref_slice<T, U: Deref<Target = [T]>>(_: U) {}
168
169 fn require_impl_deref_c_str(_: impl Deref<Target = CStr>) {}
170 fn require_impl_deref_os_str(_: impl Deref<Target = OsStr>) {}
171 fn require_impl_deref_path(_: impl Deref<Target = std::path::Path>) {}
172 fn require_impl_deref_str(_: impl Deref<Target = str>) {}
173 fn require_impl_deref_slice<T>(_: impl Deref<Target = [T]>) {}
174
175 fn require_deref_str_slice<T: Deref<Target = str>, U, V: Deref<Target = [U]>>(_: T, _: V) {}
176 fn require_deref_slice_str<T, U: Deref<Target = [T]>, V: Deref<Target = str>>(_: U, _: V) {}
177
178 fn require_as_ref_c_str<T: AsRef<CStr>>(_: T) {}
179 fn require_as_ref_os_str<T: AsRef<OsStr>>(_: T) {}
180 fn require_as_ref_path<T: AsRef<std::path::Path>>(_: T) {}
181 fn require_as_ref_str<T: AsRef<str>>(_: T) {}
182 fn require_as_ref_slice<T, U: AsRef<[T]>>(_: U) {}
183
184 fn require_impl_as_ref_c_str(_: impl AsRef<CStr>) {}
185 fn require_impl_as_ref_os_str(_: impl AsRef<OsStr>) {}
186 fn require_impl_as_ref_path(_: impl AsRef<std::path::Path>) {}
187 fn require_impl_as_ref_str(_: impl AsRef<str>) {}
188 fn require_impl_as_ref_slice<T>(_: impl AsRef<[T]>) {}
189
190 fn require_as_ref_str_slice<T: AsRef<str>, U, V: AsRef<[U]>>(_: T, _: V) {}
191 fn require_as_ref_slice_str<T, U: AsRef<[T]>, V: AsRef<str>>(_: U, _: V) {}
192
193 // `check_files` is based on:
194 // https://github.com/breard-r/acmed/blob/1f0dcc32aadbc5e52de6d23b9703554c0f925113/acmed/src/storage.rs#L262
195 fn check_files(file_types: &[FileType]) -> bool {
196     for t in file_types {
197         let path = match get_file_path(t) {
198             Ok(p) => p,
199             Err(_) => {
200                 return false;
201             },
202         };
203         if !path.is_file() {
204             return false;
205         }
206     }
207     true
208 }
209
210 fn get_file_path(_file_type: &FileType) -> Result<std::path::PathBuf, std::io::Error> {
211     Ok(std::path::PathBuf::new())
212 }
213
214 fn require_string(_: &String) {}