3 #![feature(lint_reasons)]
5 #[warn(clippy::all, clippy::needless_borrow)]
6 #[allow(unused_variables, clippy::unnecessary_mut_passed)]
10 let _ = x(&a); // no warning
11 let _ = x(&&a); // warn
14 mut_ref(&mut b); // no warning
15 mut_ref(&mut &mut b); // warn
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]`
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 {
57 let y: &mut i32 = &mut x;
58 let y: &mut i32 = &mut &mut x;
61 // Don't lint. Removing the borrow would move 'x'
65 let y: &mut i32 = match 0 {
66 // Lint here. The type given above triggers auto-borrow.
70 fn ref_mut_i32(_: &mut i32) {}
72 // Lint here. The type given above triggers auto-borrow.
76 // use 'x' after to make sure it's still usable in the fixed code.
79 let s = String::new();
80 // let _ = (&s).len();
81 // let _ = (&s).capacity();
82 // let _ = (&&s).capacity();
86 let x = &x as *const (i32, i32);
87 let _ = unsafe { (&*x).0 };
96 (&()).foo(); // Don't lint. `()` doesn't implement `Foo`
102 impl Foo for &'_ i32 {
105 (&5).foo(); // Don't lint. `5` will call `<i32 as Foo>::foo`
114 impl FooRef for &'_ () {
117 (&&()).foo_ref(); // Don't lint. `&()` will call `<() as FooRef>::foo_ref`
120 #[allow(clippy::needless_borrowed_reference)]
121 fn x(y: &i32) -> i32 {
125 fn mut_ref(y: &mut i32) {
129 fn f<T: Copy>(y: &T) -> T {
133 fn g(y: &[u8]) -> u8 {
139 impl<'a> Trait for &'a str {}
141 fn h(_: &dyn Trait) {}
144 fn check_expect_suppression() {
146 #[expect(clippy::needless_borrow)]