fork/rust

git clone https://git.julia.blue/fork/rust

borsAuto merge of #103046 - JanBeh:PR_clarify_cmp_terminology, r=workingjubilee3bcad65fbf6

master
7.1 KiB324 linesraw
1#![feature(closure_lifetime_binder)]
2#![warn(clippy::explicit_auto_deref)]
3#![allow(
4    dead_code,
5    unused_braces,
6    clippy::borrowed_box,
7    clippy::needless_borrow,
8    clippy::needless_return,
9    clippy::ptr_arg,
10    clippy::redundant_field_names,
11    clippy::too_many_arguments,
12    clippy::borrow_deref_ref,
13    clippy::let_unit_value
14)]
15
16trait CallableStr {
17    type T: Fn(&str);
18    fn callable_str(&self) -> Self::T;
19}
20impl CallableStr for () {
21    type T = fn(&str);
22    fn callable_str(&self) -> Self::T {
23        fn f(_: &str) {}
24        f
25    }
26}
27impl CallableStr for i32 {
28    type T = <() as CallableStr>::T;
29    fn callable_str(&self) -> Self::T {
30        ().callable_str()
31    }
32}
33
34trait CallableT<U: ?Sized> {
35    type T: Fn(&U);
36    fn callable_t(&self) -> Self::T;
37}
38impl<U: ?Sized> CallableT<U> for () {
39    type T = fn(&U);
40    fn callable_t(&self) -> Self::T {
41        fn f<U: ?Sized>(_: &U) {}
42        f::<U>
43    }
44}
45impl<U: ?Sized> CallableT<U> for i32 {
46    type T = <() as CallableT<U>>::T;
47    fn callable_t(&self) -> Self::T {
48        ().callable_t()
49    }
50}
51
52fn f_str(_: &str) {}
53fn f_string(_: &String) {}
54fn f_t<T>(_: T) {}
55fn f_ref_t<T: ?Sized>(_: &T) {}
56
57fn f_str_t<T>(_: &str, _: T) {}
58
59fn f_box_t<T>(_: &Box<T>) {}
60
61extern "C" {
62    fn var(_: u32, ...);
63}
64
65fn main() {
66    let s = String::new();
67
68    let _: &str = &s;
69    let _: &str = &{ String::new() };
70    let _: &str = &mut { String::new() };
71    let _ = &*s; // Don't lint. Inferred type would change.
72    let _: &_ = &*s; // Don't lint. Inferred type would change.
73
74    f_str(&s);
75    f_t(&*s); // Don't lint. Inferred type would change.
76    f_ref_t(&*s); // Don't lint. Inferred type would change.
77
78    f_str_t(&s, &*s); // Don't lint second param.
79
80    let b = Box::new(Box::new(Box::new(5)));
81    let _: &Box<i32> = &b;
82    let _: &Box<_> = &**b; // Don't lint. Inferred type would change.
83
84    f_box_t(&**b); // Don't lint. Inferred type would change.
85
86    let c = |_x: &str| ();
87    c(&s);
88
89    let c = |_x| ();
90    c(&*s); // Don't lint. Inferred type would change.
91
92    fn _f(x: &String) -> &str {
93        x
94    }
95
96    fn _f1(x: &String) -> &str {
97        { x }
98    }
99
100    fn _f2(x: &String) -> &str {
101        { x }
102    }
103
104    fn _f3(x: &Box<Box<Box<i32>>>) -> &Box<i32> {
105        x
106    }
107
108    fn _f4(
109        x: String,
110        f1: impl Fn(&str),
111        f2: &dyn Fn(&str),
112        f3: fn(&str),
113        f4: impl CallableStr,
114        f5: <() as CallableStr>::T,
115        f6: <i32 as CallableStr>::T,
116        f7: &dyn CallableStr<T = fn(&str)>,
117        f8: impl CallableT<str>,
118        f9: <() as CallableT<str>>::T,
119        f10: <i32 as CallableT<str>>::T,
120        f11: &dyn CallableT<str, T = fn(&str)>,
121    ) {
122        f1(&x);
123        f2(&x);
124        f3(&x);
125        f4.callable_str()(&x);
126        f5(&x);
127        f6(&x);
128        f7.callable_str()(&x);
129        f8.callable_t()(&x);
130        f9(&x);
131        f10(&x);
132        f11.callable_t()(&x);
133    }
134
135    struct S1<'a>(&'a str);
136    let _ = S1(&s);
137
138    struct S2<'a> {
139        s: &'a str,
140    }
141    let _ = S2 { s: &s };
142
143    struct S3<'a, T: ?Sized>(&'a T);
144    let _ = S3(&*s); // Don't lint. Inferred type would change.
145
146    struct S4<'a, T: ?Sized> {
147        s: &'a T,
148    }
149    let _ = S4 { s: &*s }; // Don't lint. Inferred type would change.
150
151    enum E1<'a> {
152        S1(&'a str),
153        S2 { s: &'a str },
154    }
155    impl<'a> E1<'a> {
156        fn m1(s: &'a String) {
157            let _ = Self::S1(s);
158            let _ = Self::S2 { s: s };
159        }
160    }
161    let _ = E1::S1(&s);
162    let _ = E1::S2 { s: &s };
163
164    enum E2<'a, T: ?Sized> {
165        S1(&'a T),
166        S2 { s: &'a T },
167    }
168    let _ = E2::S1(&*s); // Don't lint. Inferred type would change.
169    let _ = E2::S2 { s: &*s }; // Don't lint. Inferred type would change.
170
171    let ref_s = &s;
172    let _: &String = &*ref_s; // Don't lint reborrow.
173    f_string(&*ref_s); // Don't lint reborrow.
174
175    struct S5 {
176        foo: u32,
177    }
178    let b = Box::new(Box::new(S5 { foo: 5 }));
179    let _ = b.foo;
180    let _ = b.foo;
181    let _ = b.foo;
182
183    struct S6 {
184        foo: S5,
185    }
186    impl core::ops::Deref for S6 {
187        type Target = S5;
188        fn deref(&self) -> &Self::Target {
189            &self.foo
190        }
191    }
192    let s6 = S6 { foo: S5 { foo: 5 } };
193    let _ = (*s6).foo; // Don't lint. `S6` also has a field named `foo`
194
195    let ref_str = &"foo";
196    let _ = f_str(ref_str);
197    let ref_ref_str = &ref_str;
198    let _ = f_str(ref_ref_str);
199
200    fn _f5(x: &u32) -> u32 {
201        if true {
202            *x
203        } else {
204            return *x;
205        }
206    }
207
208    f_str(&ref_str); // `needless_borrow` will suggest removing both references
209    f_str(&ref_str); // `needless_borrow` will suggest removing only one reference
210
211    let x = &&40;
212    unsafe {
213        var(0, &**x);
214    }
215
216    let s = &"str";
217    let _ = || return *s;
218    let _ = || -> &'static str { return s };
219
220    struct X;
221    struct Y(X);
222    impl core::ops::Deref for Y {
223        type Target = X;
224        fn deref(&self) -> &Self::Target {
225            &self.0
226        }
227    }
228    let _: &X = &*{ Y(X) };
229    let _: &X = &*match 0 {
230        #[rustfmt::skip]
231        0 => { Y(X) },
232        _ => panic!(),
233    };
234    let _: &X = &*if true { Y(X) } else { panic!() };
235
236    fn deref_to_u<U, T: core::ops::Deref<Target = U>>(x: &T) -> &U {
237        x
238    }
239
240    let _ = |x: &'static Box<dyn Iterator<Item = u32>>| -> &'static dyn Iterator<Item = u32> { &**x };
241    fn ret_any(x: &Box<dyn std::any::Any>) -> &dyn std::any::Any {
242        &**x
243    }
244
245    let x = String::new();
246    let _: *const str = &*x;
247
248    struct S7([u32; 1]);
249    impl core::ops::Deref for S7 {
250        type Target = [u32; 1];
251        fn deref(&self) -> &Self::Target {
252            &self.0
253        }
254    }
255    let x = S7([0]);
256    let _: &[u32] = &*x;
257
258    let c1 = |_: &Vec<&u32>| {};
259    let x = &&vec![&1u32];
260    c1(x);
261    let _ = for<'a, 'b> |x: &'a &'a Vec<&'b u32>, b: bool| -> &'a Vec<&'b u32> {
262        if b {
263            return x;
264        }
265        x
266    };
267
268    trait WithAssoc {
269        type Assoc: ?Sized;
270        fn to_assoc(&self) -> &Self::Assoc {
271            panic!()
272        }
273    }
274    impl WithAssoc for String {
275        type Assoc = str;
276    }
277    fn takes_assoc<T: WithAssoc>(_: &T::Assoc) -> T {
278        unimplemented!()
279    }
280    let _: String = takes_assoc(&*String::new());
281
282    // Issue #9901
283    fn takes_ref(_: &i32) {}
284    takes_ref(*Box::new(&0i32));
285
286    // Issue #10384
287    impl<'a> WithAssoc for &'a u32 {
288        type Assoc = dyn core::fmt::Display;
289        fn to_assoc(&self) -> &Self::Assoc {
290            *self
291        }
292    }
293    fn return_dyn_assoc<'a>(x: &'a &'a u32) -> &'a <&'a u32 as WithAssoc>::Assoc {
294        *x
295    }
296
297    // Issue #11366
298    let _: &mut u32 = match &mut Some(&mut 0u32) {
299        Some(x) => x,
300        None => panic!(),
301    };
302
303    // Issue #11474
304    pub struct Variant {
305        pub anonymous: Variant0,
306    }
307
308    pub union Variant0 {
309        pub anonymous: std::mem::ManuallyDrop<Variant00>,
310    }
311
312    pub struct Variant00 {
313        pub anonymous: Variant000,
314    }
315
316    pub union Variant000 {
317        pub val: i32,
318    }
319
320    unsafe {
321        let mut p = core::mem::zeroed::<Variant>();
322        (*p.anonymous.anonymous).anonymous.val = 1;
323    }
324}