]> git.lizzy.rs Git - rust.git/blob - tests/ui/explicit_auto_deref.rs
Don't lint `explicit_auto_deref` on reborrows
[rust.git] / tests / ui / explicit_auto_deref.rs
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::ptr_arg,
10     clippy::redundant_field_names,
11     clippy::too_many_arguments,
12     clippy::borrow_deref_ref
13 )]
14
15 trait CallableStr {
16     type T: Fn(&str);
17     fn callable_str(&self) -> Self::T;
18 }
19 impl CallableStr for () {
20     type T = fn(&str);
21     fn callable_str(&self) -> Self::T {
22         fn f(_: &str) {}
23         f
24     }
25 }
26 impl CallableStr for i32 {
27     type T = <() as CallableStr>::T;
28     fn callable_str(&self) -> Self::T {
29         ().callable_str()
30     }
31 }
32
33 trait CallableT<U: ?Sized> {
34     type T: Fn(&U);
35     fn callable_t(&self) -> Self::T;
36 }
37 impl<U: ?Sized> CallableT<U> for () {
38     type T = fn(&U);
39     fn callable_t(&self) -> Self::T {
40         fn f<U: ?Sized>(_: &U) {}
41         f::<U>
42     }
43 }
44 impl<U: ?Sized> CallableT<U> for i32 {
45     type T = <() as CallableT<U>>::T;
46     fn callable_t(&self) -> Self::T {
47         ().callable_t()
48     }
49 }
50
51 fn f_str(_: &str) {}
52 fn f_string(_: &String) {}
53 fn f_t<T>(_: T) {}
54 fn f_ref_t<T: ?Sized>(_: &T) {}
55
56 fn f_str_t<T>(_: &str, _: T) {}
57
58 fn f_box_t<T>(_: &Box<T>) {}
59
60 fn main() {
61     let s = String::new();
62
63     let _: &str = &*s;
64     let _ = &*s; // Don't lint. Inferred type would change.
65     let _: &_ = &*s; // Don't lint. Inferred type would change.
66
67     f_str(&*s);
68     f_t(&*s); // Don't lint. Inferred type would change.
69     f_ref_t(&*s); // Don't lint. Inferred type would change.
70
71     f_str_t(&*s, &*s); // Don't lint second param.
72
73     let b = Box::new(Box::new(Box::new(5)));
74     let _: &Box<i32> = &**b;
75     let _: &Box<_> = &**b; // Don't lint. Inferred type would change.
76
77     f_box_t(&**b); // Don't lint. Inferred type would change.
78
79     let c = |_x: &str| ();
80     c(&*s);
81
82     let c = |_x| ();
83     c(&*s); // Don't lint. Inferred type would change.
84
85     fn _f(x: &String) -> &str {
86         &**x
87     }
88
89     fn _f1(x: &String) -> &str {
90         { &**x }
91     }
92
93     fn _f2(x: &String) -> &str {
94         &**{ x }
95     }
96
97     fn _f3(x: &Box<Box<Box<i32>>>) -> &Box<i32> {
98         &***x
99     }
100
101     fn _f4(
102         x: String,
103         f1: impl Fn(&str),
104         f2: &dyn Fn(&str),
105         f3: fn(&str),
106         f4: impl CallableStr,
107         f5: <() as CallableStr>::T,
108         f6: <i32 as CallableStr>::T,
109         f7: &dyn CallableStr<T = fn(&str)>,
110         f8: impl CallableT<str>,
111         f9: <() as CallableT<str>>::T,
112         f10: <i32 as CallableT<str>>::T,
113         f11: &dyn CallableT<str, T = fn(&str)>,
114     ) {
115         f1(&*x);
116         f2(&*x);
117         f3(&*x);
118         f4.callable_str()(&*x);
119         f5(&*x);
120         f6(&*x);
121         f7.callable_str()(&*x);
122         f8.callable_t()(&*x);
123         f9(&*x);
124         f10(&*x);
125         f11.callable_t()(&*x);
126     }
127
128     struct S1<'a>(&'a str);
129     let _ = S1(&*s);
130
131     struct S2<'a> {
132         s: &'a str,
133     }
134     let _ = S2 { s: &*s };
135
136     struct S3<'a, T: ?Sized>(&'a T);
137     let _ = S3(&*s); // Don't lint. Inferred type would change.
138
139     struct S4<'a, T: ?Sized> {
140         s: &'a T,
141     }
142     let _ = S4 { s: &*s }; // Don't lint. Inferred type would change.
143
144     enum E1<'a> {
145         S1(&'a str),
146         S2 { s: &'a str },
147     }
148     impl<'a> E1<'a> {
149         fn m1(s: &'a String) {
150             let _ = Self::S1(&**s);
151             let _ = Self::S2 { s: &**s };
152         }
153     }
154     let _ = E1::S1(&*s);
155     let _ = E1::S2 { s: &*s };
156
157     enum E2<'a, T: ?Sized> {
158         S1(&'a T),
159         S2 { s: &'a T },
160     }
161     let _ = E2::S1(&*s); // Don't lint. Inferred type would change.
162     let _ = E2::S2 { s: &*s }; // Don't lint. Inferred type would change.
163
164     let ref_s = &s;
165     let _: &String = &*ref_s; // Don't lint reborrow.
166     f_string(&*ref_s); // Don't lint reborrow.
167 }