]> git.lizzy.rs Git - rust.git/blob - src/tools/clippy/tests/ui/needless_borrow.rs
Rollup merge of #98856 - GuillaumeGomez:rustdoc-test-rm-fixme, r=Dylan-DPC
[rust.git] / src / tools / clippy / tests / ui / needless_borrow.rs
1 // run-rustfix
2
3 #![feature(lint_reasons)]
4
5 #[warn(clippy::all, clippy::needless_borrow)]
6 #[allow(unused_variables, 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
120 #[allow(clippy::needless_borrowed_reference)]
121 fn x(y: &i32) -> i32 {
122     *y
123 }
124
125 fn mut_ref(y: &mut i32) {
126     *y = 5;
127 }
128
129 fn f<T: Copy>(y: &T) -> T {
130     *y
131 }
132
133 fn g(y: &[u8]) -> u8 {
134     y[0]
135 }
136
137 trait Trait {}
138
139 impl<'a> Trait for &'a str {}
140
141 fn h(_: &dyn Trait) {}
142
143 #[allow(dead_code)]
144 fn check_expect_suppression() {
145     let a = 5;
146     #[expect(clippy::needless_borrow)]
147     let _ = x(&&a);
148 }