]> git.lizzy.rs Git - rust.git/blob - src/tools/clippy/tests/ui/needless_borrow.rs
Rollup merge of #102454 - chenyukang:fix-102396-missing-parentheses, r=lcnr
[rust.git] / src / tools / clippy / tests / ui / needless_borrow.rs
1 // run-rustfix
2 #![feature(custom_inner_attributes, lint_reasons)]
3
4 #[warn(clippy::all, clippy::needless_borrow)]
5 #[allow(unused_variables)]
6 #[allow(clippy::uninlined_format_args, clippy::unnecessary_mut_passed)]
7 fn main() {
8     let a = 5;
9     let ref_a = &a;
10     let _ = x(&a); // no warning
11     let _ = x(&&a); // warn
12
13     let mut b = 5;
14     mut_ref(&mut b); // no warning
15     mut_ref(&mut &mut b); // warn
16
17     let s = &String::from("hi");
18     let s_ident = f(&s); // should not error, because `&String` implements Copy, but `String` does not
19     let g_val = g(&Vec::new()); // should not error, because `&Vec<T>` derefs to `&[T]`
20     let vec = Vec::new();
21     let vec_val = g(&vec); // should not error, because `&Vec<T>` derefs to `&[T]`
22     h(&"foo"); // should not error, because the `&&str` is required, due to `&Trait`
23     let garbl = match 42 {
24         44 => &a,
25         45 => {
26             println!("foo");
27             &&a
28         },
29         46 => &&a,
30         47 => {
31             println!("foo");
32             loop {
33                 println!("{}", a);
34                 if a == 25 {
35                     break &ref_a;
36                 }
37             }
38         },
39         _ => panic!(),
40     };
41
42     let _ = x(&&&a);
43     let _ = x(&mut &&a);
44     let _ = x(&&&mut b);
45     let _ = x(&&ref_a);
46     {
47         let b = &mut b;
48         x(&b);
49     }
50
51     // Issue #8191
52     let mut x = 5;
53     let mut x = &mut x;
54
55     mut_ref(&mut x);
56     mut_ref(&mut &mut x);
57     let y: &mut i32 = &mut x;
58     let y: &mut i32 = &mut &mut x;
59
60     let y = match 0 {
61         // Don't lint. Removing the borrow would move 'x'
62         0 => &mut x,
63         _ => &mut *x,
64     };
65     let y: &mut i32 = match 0 {
66         // Lint here. The type given above triggers auto-borrow.
67         0 => &mut x,
68         _ => &mut *x,
69     };
70     fn ref_mut_i32(_: &mut i32) {}
71     ref_mut_i32(match 0 {
72         // Lint here. The type given above triggers auto-borrow.
73         0 => &mut x,
74         _ => &mut *x,
75     });
76     // use 'x' after to make sure it's still usable in the fixed code.
77     *x = 5;
78
79     let s = String::new();
80     // let _ = (&s).len();
81     // let _ = (&s).capacity();
82     // let _ = (&&s).capacity();
83
84     let x = (1, 2);
85     let _ = (&x).0;
86     let x = &x as *const (i32, i32);
87     let _ = unsafe { (&*x).0 };
88
89     // Issue #8367
90     trait Foo {
91         fn foo(self);
92     }
93     impl Foo for &'_ () {
94         fn foo(self) {}
95     }
96     (&()).foo(); // Don't lint. `()` doesn't implement `Foo`
97     (&&()).foo();
98
99     impl Foo for i32 {
100         fn foo(self) {}
101     }
102     impl Foo for &'_ i32 {
103         fn foo(self) {}
104     }
105     (&5).foo(); // Don't lint. `5` will call `<i32 as Foo>::foo`
106     (&&5).foo();
107
108     trait FooRef {
109         fn foo_ref(&self);
110     }
111     impl FooRef for () {
112         fn foo_ref(&self) {}
113     }
114     impl FooRef for &'_ () {
115         fn foo_ref(&self) {}
116     }
117     (&&()).foo_ref(); // Don't lint. `&()` will call `<() as FooRef>::foo_ref`
118
119     struct S;
120     impl From<S> for u32 {
121         fn from(s: S) -> Self {
122             (&s).into()
123         }
124     }
125     impl From<&S> for u32 {
126         fn from(s: &S) -> Self {
127             0
128         }
129     }
130
131     let _ = std::process::Command::new("ls").args(&["-a", "-l"]).status().unwrap();
132     let _ = std::path::Path::new(".").join(&&".");
133     deref_target_is_x(&X);
134     multiple_constraints(&[[""]]);
135     multiple_constraints_normalizes_to_same(&X, X);
136     let _ = Some("").unwrap_or(&"");
137
138     only_sized(&""); // Don't lint. `Sized` is only bound
139     let _ = std::any::Any::type_id(&""); // Don't lint. `Any` is only bound
140     let _ = Box::new(&""); // Don't lint. Type parameter appears in return type
141     ref_as_ref_path(&""); // Don't lint. Argument type is not a type parameter
142     refs_only(&()); // Don't lint. `&T` implements trait, but `T` doesn't
143     multiple_constraints_normalizes_to_different(&[[""]], &[""]); // Don't lint. Projected type appears in arguments
144 }
145
146 #[allow(clippy::needless_borrowed_reference)]
147 fn x(y: &i32) -> i32 {
148     *y
149 }
150
151 fn mut_ref(y: &mut i32) {
152     *y = 5;
153 }
154
155 fn f<T: Copy>(y: &T) -> T {
156     *y
157 }
158
159 fn g(y: &[u8]) -> u8 {
160     y[0]
161 }
162
163 trait Trait {}
164
165 impl<'a> Trait for &'a str {}
166
167 fn h(_: &dyn Trait) {}
168
169 #[allow(dead_code)]
170 fn check_expect_suppression() {
171     let a = 5;
172     #[expect(clippy::needless_borrow)]
173     let _ = x(&&a);
174 }
175
176 #[allow(dead_code)]
177 mod issue9160 {
178     pub struct S<F> {
179         f: F,
180     }
181
182     impl<T, F> S<F>
183     where
184         F: Fn() -> T,
185     {
186         fn calls_field(&self) -> T {
187             (&self.f)()
188         }
189     }
190
191     impl<T, F> S<F>
192     where
193         F: FnMut() -> T,
194     {
195         fn calls_mut_field(&mut self) -> T {
196             (&mut self.f)()
197         }
198     }
199 }
200
201 #[derive(Clone, Copy)]
202 struct X;
203
204 impl std::ops::Deref for X {
205     type Target = X;
206     fn deref(&self) -> &Self::Target {
207         self
208     }
209 }
210
211 fn deref_target_is_x<T>(_: T)
212 where
213     T: std::ops::Deref<Target = X>,
214 {
215 }
216
217 fn multiple_constraints<T, U, V, X, Y>(_: T)
218 where
219     T: IntoIterator<Item = U> + IntoIterator<Item = X>,
220     U: IntoIterator<Item = V>,
221     V: AsRef<str>,
222     X: IntoIterator<Item = Y>,
223     Y: AsRef<std::ffi::OsStr>,
224 {
225 }
226
227 fn multiple_constraints_normalizes_to_same<T, U, V>(_: T, _: V)
228 where
229     T: std::ops::Deref<Target = U>,
230     U: std::ops::Deref<Target = V>,
231 {
232 }
233
234 fn only_sized<T>(_: T) {}
235
236 fn ref_as_ref_path<T: 'static>(_: &'static T)
237 where
238     &'static T: AsRef<std::path::Path>,
239 {
240 }
241
242 trait RefsOnly {
243     type Referent;
244 }
245
246 impl<T> RefsOnly for &T {
247     type Referent = T;
248 }
249
250 fn refs_only<T, U>(_: T)
251 where
252     T: RefsOnly<Referent = U>,
253 {
254 }
255
256 fn multiple_constraints_normalizes_to_different<T, U, V>(_: T, _: U)
257 where
258     T: IntoIterator<Item = U>,
259     U: IntoIterator<Item = V>,
260     V: AsRef<str>,
261 {
262 }
263
264 // https://github.com/rust-lang/rust-clippy/pull/9136#pullrequestreview-1037379321
265 #[allow(dead_code)]
266 mod copyable_iterator {
267     #[derive(Clone, Copy)]
268     struct Iter;
269     impl Iterator for Iter {
270         type Item = ();
271         fn next(&mut self) -> Option<Self::Item> {
272             None
273         }
274     }
275     fn takes_iter(_: impl Iterator) {}
276     fn dont_warn(mut x: Iter) {
277         takes_iter(&mut x);
278     }
279     fn warn(mut x: &mut Iter) {
280         takes_iter(&mut x)
281     }
282 }
283
284 mod under_msrv {
285     #![allow(dead_code)]
286     #![clippy::msrv = "1.52.0"]
287
288     fn foo() {
289         let _ = std::process::Command::new("ls").args(&["-a", "-l"]).status().unwrap();
290     }
291 }
292
293 mod meets_msrv {
294     #![allow(dead_code)]
295     #![clippy::msrv = "1.53.0"]
296
297     fn foo() {
298         let _ = std::process::Command::new("ls").args(&["-a", "-l"]).status().unwrap();
299     }
300 }
301
302 #[allow(unused)]
303 fn issue9383() {
304     // Should not lint because unions need explicit deref when accessing field
305     use std::mem::ManuallyDrop;
306
307     union Coral {
308         crab: ManuallyDrop<Vec<i32>>,
309     }
310
311     union Ocean {
312         coral: ManuallyDrop<Coral>,
313     }
314
315     let mut ocean = Ocean {
316         coral: ManuallyDrop::new(Coral {
317             crab: ManuallyDrop::new(vec![1, 2, 3]),
318         }),
319     };
320
321     unsafe {
322         ManuallyDrop::drop(&mut (&mut ocean.coral).crab);
323
324         (*ocean.coral).crab = ManuallyDrop::new(vec![4, 5, 6]);
325         ManuallyDrop::drop(&mut (*ocean.coral).crab);
326
327         ManuallyDrop::drop(&mut ocean.coral);
328     }
329 }