2 #![feature(lint_reasons)]
5 clippy::uninlined_format_args,
6 clippy::unnecessary_mut_passed,
7 clippy::unnecessary_to_owned
9 #![warn(clippy::needless_borrow)]
14 let _ = x(&a); // no warning
15 let _ = x(&&a); // warn
18 mut_ref(&mut b); // no warning
19 mut_ref(&mut &mut b); // warn
21 let s = &String::from("hi");
22 let s_ident = f(&s); // should not error, because `&String` implements Copy, but `String` does not
23 let g_val = g(&Vec::new()); // should not error, because `&Vec<T>` derefs to `&[T]`
25 let vec_val = g(&vec); // should not error, because `&Vec<T>` derefs to `&[T]`
26 h(&"foo"); // should not error, because the `&&str` is required, due to `&Trait`
27 let garbl = match 42 {
61 let y: &mut i32 = &mut x;
62 let y: &mut i32 = &mut &mut x;
65 // Don't lint. Removing the borrow would move 'x'
69 let y: &mut i32 = match 0 {
70 // Lint here. The type given above triggers auto-borrow.
74 fn ref_mut_i32(_: &mut i32) {}
76 // Lint here. The type given above triggers auto-borrow.
80 // use 'x' after to make sure it's still usable in the fixed code.
83 let s = String::new();
84 // let _ = (&s).len();
85 // let _ = (&s).capacity();
86 // let _ = (&&s).capacity();
90 let x = &x as *const (i32, i32);
91 let _ = unsafe { (&*x).0 };
100 (&()).foo(); // Don't lint. `()` doesn't implement `Foo`
106 impl Foo for &'_ i32 {
109 (&5).foo(); // Don't lint. `5` will call `<i32 as Foo>::foo`
118 impl FooRef for &'_ () {
121 (&&()).foo_ref(); // Don't lint. `&()` will call `<() as FooRef>::foo_ref`
124 impl From<S> for u32 {
125 fn from(s: S) -> Self {
129 impl From<&S> for u32 {
130 fn from(s: &S) -> Self {
135 let _ = std::process::Command::new("ls").args(&["-a", "-l"]).status().unwrap();
136 let _ = std::path::Path::new(".").join(&&".");
137 deref_target_is_x(&X);
138 multiple_constraints(&[[""]]);
139 multiple_constraints_normalizes_to_same(&X, X);
140 let _ = Some("").unwrap_or(&"");
141 let _ = std::fs::write("x", &"".to_string());
143 only_sized(&""); // Don't lint. `Sized` is only bound
144 let _ = std::any::Any::type_id(&""); // Don't lint. `Any` is only bound
145 let _ = Box::new(&""); // Don't lint. Type parameter appears in return type
146 ref_as_ref_path(&""); // Don't lint. Argument type is not a type parameter
147 refs_only(&()); // Don't lint. `&T` implements trait, but `T` doesn't
148 multiple_constraints_normalizes_to_different(&[[""]], &[""]); // Don't lint. Projected type appears in arguments
151 #[allow(clippy::needless_borrowed_reference)]
152 fn x(y: &i32) -> i32 {
156 fn mut_ref(y: &mut i32) {
160 fn f<T: Copy>(y: &T) -> T {
164 fn g(y: &[u8]) -> u8 {
170 impl<'a> Trait for &'a str {}
172 fn h(_: &dyn Trait) {}
174 fn check_expect_suppression() {
176 #[expect(clippy::needless_borrow)]
189 fn calls_field(&self) -> T {
198 fn calls_mut_field(&mut self) -> T {
204 #[derive(Clone, Copy)]
207 impl std::ops::Deref for X {
209 fn deref(&self) -> &Self::Target {
214 fn deref_target_is_x<T>(_: T)
216 T: std::ops::Deref<Target = X>,
220 fn multiple_constraints<T, U, V, X, Y>(_: T)
222 T: IntoIterator<Item = U> + IntoIterator<Item = X>,
223 U: IntoIterator<Item = V>,
225 X: IntoIterator<Item = Y>,
226 Y: AsRef<std::ffi::OsStr>,
230 fn multiple_constraints_normalizes_to_same<T, U, V>(_: T, _: V)
232 T: std::ops::Deref<Target = U>,
233 U: std::ops::Deref<Target = V>,
237 fn only_sized<T>(_: T) {}
239 fn ref_as_ref_path<T: 'static>(_: &'static T)
241 &'static T: AsRef<std::path::Path>,
249 impl<T> RefsOnly for &T {
253 fn refs_only<T, U>(_: T)
255 T: RefsOnly<Referent = U>,
259 fn multiple_constraints_normalizes_to_different<T, U, V>(_: T, _: U)
261 T: IntoIterator<Item = U>,
262 U: IntoIterator<Item = V>,
267 // https://github.com/rust-lang/rust-clippy/pull/9136#pullrequestreview-1037379321
268 mod copyable_iterator {
269 #[derive(Clone, Copy)]
271 impl Iterator for Iter {
273 fn next(&mut self) -> Option<Self::Item> {
277 fn takes_iter(_: impl Iterator) {}
278 fn dont_warn(mut x: Iter) {
282 fn warn(mut x: &mut Iter) {
287 #[clippy::msrv = "1.52.0"]
290 let _ = std::process::Command::new("ls").args(&["-a", "-l"]).status().unwrap();
294 #[clippy::msrv = "1.53.0"]
297 let _ = std::process::Command::new("ls").args(&["-a", "-l"]).status().unwrap();
302 // Should not lint because unions need explicit deref when accessing field
303 use std::mem::ManuallyDrop;
306 crab: ManuallyDrop<Vec<i32>>,
310 coral: ManuallyDrop<Coral>,
313 let mut ocean = Ocean {
314 coral: ManuallyDrop::new(Coral {
315 crab: ManuallyDrop::new(vec![1, 2, 3]),
320 ManuallyDrop::drop(&mut (&mut ocean.coral).crab);
322 (*ocean.coral).crab = ManuallyDrop::new(vec![4, 5, 6]);
323 ManuallyDrop::drop(&mut (*ocean.coral).crab);
325 ManuallyDrop::drop(&mut ocean.coral);
330 let env = "env".to_owned();
331 let arg = "arg".to_owned();
333 let loc = "loc".to_owned();
334 let _ = std::fs::write("x", &env); // Don't lint. In environment
335 let _ = std::fs::write("x", &arg);
336 let _ = std::fs::write("x", &loc);
338 let _ = std::fs::write("x", &env); // Don't lint. Borrowed by `f`
342 mod significant_drop {
350 fn drop(&mut self) {}
355 debug(&y); // Don't lint. Has significant drop
358 fn debug(_: impl std::fmt::Debug) {}
361 mod used_exactly_once {
365 fn use_x(_: impl AsRef<str>) {}
368 mod used_more_than_once {
373 fn use_x(_: impl AsRef<str>) {}
374 fn use_x_again(_: impl AsRef<str>) {}
377 // https://github.com/rust-lang/rust-clippy/issues/9111#issuecomment-1277114280
381 impl Extend<u8> for A {
382 fn extend<T: IntoIterator<Item = u8>>(&mut self, _: T) {
387 impl<'a> Extend<&'a u8> for A {
388 fn extend<T: IntoIterator<Item = &'a u8>>(&mut self, _: T) {
395 a.extend(&[]); // vs a.extend([]);
401 let string = String::new();
407 fn f<T: AsRef<str>>(_: T) {}
411 fn foo<D: std::fmt::Display>(_it: impl IntoIterator<Item = D>) {}
414 foo(if std::env::var_os("HI").is_some() {
422 mod issue_9739_method_variant {
426 fn foo<D: std::fmt::Display>(&self, _it: impl IntoIterator<Item = D>) {}
430 S.foo(if std::env::var_os("HI").is_some() {
439 fn foo<T: AsRef<[u8]>>(t: T) {
440 println!("{}", std::mem::size_of::<T>());
441 let _t: &[u8] = t.as_ref();
445 let a: [u8; 100] = [0u8; 100];
456 foo::<&[u8; 100]>(&a);
461 mod issue_9782_type_relative_variant {
465 fn foo<T: AsRef<[u8]>>(t: T) {
466 println!("{}", std::mem::size_of::<T>());
467 let _t: &[u8] = t.as_ref();
472 let a: [u8; 100] = [0u8; 100];
474 S::foo::<&[u8; 100]>(&a);
478 mod issue_9782_method_variant {
482 fn foo<T: AsRef<[u8]>>(&self, t: T) {
483 println!("{}", std::mem::size_of::<T>());
484 let _t: &[u8] = t.as_ref();
489 let a: [u8; 100] = [0u8; 100];
491 S.foo::<&[u8; 100]>(&a);