]> git.lizzy.rs Git - rust.git/blob - src/tools/clippy/tests/ui/needless_borrow.fixed
Auto merge of #99963 - cjgillot:iter-submodule, r=compiler-errors
[rust.git] / src / tools / clippy / tests / ui / needless_borrow.fixed
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 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(&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(x);
56     mut_ref(x);
57     let y: &mut i32 = x;
58     let y: &mut i32 = 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 => 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 => 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
132 #[allow(clippy::needless_borrowed_reference)]
133 fn x(y: &i32) -> i32 {
134     *y
135 }
136
137 fn mut_ref(y: &mut i32) {
138     *y = 5;
139 }
140
141 fn f<T: Copy>(y: &T) -> T {
142     *y
143 }
144
145 fn g(y: &[u8]) -> u8 {
146     y[0]
147 }
148
149 trait Trait {}
150
151 impl<'a> Trait for &'a str {}
152
153 fn h(_: &dyn Trait) {}
154
155 #[allow(dead_code)]
156 fn check_expect_suppression() {
157     let a = 5;
158     #[expect(clippy::needless_borrow)]
159     let _ = x(&&a);
160 }
161
162 #[allow(dead_code)]
163 mod issue9160 {
164     pub struct S<F> {
165         f: F,
166     }
167
168     impl<T, F> S<F>
169     where
170         F: Fn() -> T,
171     {
172         fn calls_field(&self) -> T {
173             (self.f)()
174         }
175     }
176
177     impl<T, F> S<F>
178     where
179         F: FnMut() -> T,
180     {
181         fn calls_mut_field(&mut self) -> T {
182             (self.f)()
183         }
184     }
185 }