]> git.lizzy.rs Git - rust.git/blob - src/tools/clippy/tests/ui/explicit_auto_deref.fixed
Auto merge of #99174 - scottmcm:reoptimize-layout-array, r=joshtriplett
[rust.git] / src / tools / clippy / tests / ui / explicit_auto_deref.fixed
1 // run-rustfix
2
3 #![warn(clippy::explicit_auto_deref)]
4 #![allow(
5     dead_code,
6     unused_braces,
7     clippy::borrowed_box,
8     clippy::needless_borrow,
9     clippy::needless_return,
10     clippy::ptr_arg,
11     clippy::redundant_field_names,
12     clippy::too_many_arguments,
13     clippy::borrow_deref_ref,
14     clippy::let_unit_value
15 )]
16
17 trait CallableStr {
18     type T: Fn(&str);
19     fn callable_str(&self) -> Self::T;
20 }
21 impl CallableStr for () {
22     type T = fn(&str);
23     fn callable_str(&self) -> Self::T {
24         fn f(_: &str) {}
25         f
26     }
27 }
28 impl CallableStr for i32 {
29     type T = <() as CallableStr>::T;
30     fn callable_str(&self) -> Self::T {
31         ().callable_str()
32     }
33 }
34
35 trait CallableT<U: ?Sized> {
36     type T: Fn(&U);
37     fn callable_t(&self) -> Self::T;
38 }
39 impl<U: ?Sized> CallableT<U> for () {
40     type T = fn(&U);
41     fn callable_t(&self) -> Self::T {
42         fn f<U: ?Sized>(_: &U) {}
43         f::<U>
44     }
45 }
46 impl<U: ?Sized> CallableT<U> for i32 {
47     type T = <() as CallableT<U>>::T;
48     fn callable_t(&self) -> Self::T {
49         ().callable_t()
50     }
51 }
52
53 fn f_str(_: &str) {}
54 fn f_string(_: &String) {}
55 fn f_t<T>(_: T) {}
56 fn f_ref_t<T: ?Sized>(_: &T) {}
57
58 fn f_str_t<T>(_: &str, _: T) {}
59
60 fn f_box_t<T>(_: &Box<T>) {}
61
62 extern "C" {
63     fn var(_: u32, ...);
64 }
65
66 fn main() {
67     let s = String::new();
68
69     let _: &str = &s;
70     let _ = &*s; // Don't lint. Inferred type would change.
71     let _: &_ = &*s; // Don't lint. Inferred type would change.
72
73     f_str(&s);
74     f_t(&*s); // Don't lint. Inferred type would change.
75     f_ref_t(&*s); // Don't lint. Inferred type would change.
76
77     f_str_t(&s, &*s); // Don't lint second param.
78
79     let b = Box::new(Box::new(Box::new(5)));
80     let _: &Box<i32> = &b;
81     let _: &Box<_> = &**b; // Don't lint. Inferred type would change.
82
83     f_box_t(&**b); // Don't lint. Inferred type would change.
84
85     let c = |_x: &str| ();
86     c(&s);
87
88     let c = |_x| ();
89     c(&*s); // Don't lint. Inferred type would change.
90
91     fn _f(x: &String) -> &str {
92         x
93     }
94
95     fn _f1(x: &String) -> &str {
96         { x }
97     }
98
99     fn _f2(x: &String) -> &str {
100         { x }
101     }
102
103     fn _f3(x: &Box<Box<Box<i32>>>) -> &Box<i32> {
104         x
105     }
106
107     fn _f4(
108         x: String,
109         f1: impl Fn(&str),
110         f2: &dyn Fn(&str),
111         f3: fn(&str),
112         f4: impl CallableStr,
113         f5: <() as CallableStr>::T,
114         f6: <i32 as CallableStr>::T,
115         f7: &dyn CallableStr<T = fn(&str)>,
116         f8: impl CallableT<str>,
117         f9: <() as CallableT<str>>::T,
118         f10: <i32 as CallableT<str>>::T,
119         f11: &dyn CallableT<str, T = fn(&str)>,
120     ) {
121         f1(&x);
122         f2(&x);
123         f3(&x);
124         f4.callable_str()(&x);
125         f5(&x);
126         f6(&x);
127         f7.callable_str()(&x);
128         f8.callable_t()(&x);
129         f9(&x);
130         f10(&x);
131         f11.callable_t()(&x);
132     }
133
134     struct S1<'a>(&'a str);
135     let _ = S1(&s);
136
137     struct S2<'a> {
138         s: &'a str,
139     }
140     let _ = S2 { s: &s };
141
142     struct S3<'a, T: ?Sized>(&'a T);
143     let _ = S3(&*s); // Don't lint. Inferred type would change.
144
145     struct S4<'a, T: ?Sized> {
146         s: &'a T,
147     }
148     let _ = S4 { s: &*s }; // Don't lint. Inferred type would change.
149
150     enum E1<'a> {
151         S1(&'a str),
152         S2 { s: &'a str },
153     }
154     impl<'a> E1<'a> {
155         fn m1(s: &'a String) {
156             let _ = Self::S1(s);
157             let _ = Self::S2 { s: s };
158         }
159     }
160     let _ = E1::S1(&s);
161     let _ = E1::S2 { s: &s };
162
163     enum E2<'a, T: ?Sized> {
164         S1(&'a T),
165         S2 { s: &'a T },
166     }
167     let _ = E2::S1(&*s); // Don't lint. Inferred type would change.
168     let _ = E2::S2 { s: &*s }; // Don't lint. Inferred type would change.
169
170     let ref_s = &s;
171     let _: &String = &*ref_s; // Don't lint reborrow.
172     f_string(&*ref_s); // Don't lint reborrow.
173
174     struct S5 {
175         foo: u32,
176     }
177     let b = Box::new(Box::new(S5 { foo: 5 }));
178     let _ = b.foo;
179     let _ = b.foo;
180     let _ = b.foo;
181
182     struct S6 {
183         foo: S5,
184     }
185     impl core::ops::Deref for S6 {
186         type Target = S5;
187         fn deref(&self) -> &Self::Target {
188             &self.foo
189         }
190     }
191     let s6 = S6 { foo: S5 { foo: 5 } };
192     let _ = (*s6).foo; // Don't lint. `S6` also has a field named `foo`
193
194     let ref_str = &"foo";
195     let _ = f_str(ref_str);
196     let ref_ref_str = &ref_str;
197     let _ = f_str(ref_ref_str);
198
199     fn _f5(x: &u32) -> u32 {
200         if true {
201             *x
202         } else {
203             return *x;
204         }
205     }
206
207     f_str(&&ref_str); // `needless_borrow` will suggest removing both references
208     f_str(&ref_str); // `needless_borrow` will suggest removing only one reference
209
210     let x = &&40;
211     unsafe {
212         var(0, &**x);
213     }
214
215     let s = &"str";
216     let _ = || return *s;
217     let _ = || -> &'static str { return s };
218 }