]> git.lizzy.rs Git - rust.git/blob - src/tools/clippy/tests/ui/useless_asref.rs
Rollup merge of #105602 - RalfJung:read-convenience, r=oli-obk
[rust.git] / src / tools / clippy / tests / ui / useless_asref.rs
1 // run-rustfix
2 #![deny(clippy::useless_asref)]
3 #![allow(clippy::explicit_auto_deref, clippy::uninlined_format_args)]
4
5 use std::fmt::Debug;
6
7 struct FakeAsRef;
8
9 #[allow(clippy::should_implement_trait)]
10 impl FakeAsRef {
11     fn as_ref(&self) -> &Self {
12         self
13     }
14 }
15
16 struct MoreRef;
17
18 impl<'a, 'b, 'c> AsRef<&'a &'b &'c MoreRef> for MoreRef {
19     fn as_ref(&self) -> &&'a &'b &'c MoreRef {
20         &&&&MoreRef
21     }
22 }
23
24 fn foo_rstr(x: &str) {
25     println!("{:?}", x);
26 }
27 fn foo_rslice(x: &[i32]) {
28     println!("{:?}", x);
29 }
30 fn foo_mrslice(x: &mut [i32]) {
31     println!("{:?}", x);
32 }
33 fn foo_rrrrmr(_: &&&&MoreRef) {
34     println!("so many refs");
35 }
36
37 fn not_ok() {
38     let rstr: &str = "hello";
39     let mut mrslice: &mut [i32] = &mut [1, 2, 3];
40
41     {
42         let rslice: &[i32] = &*mrslice;
43         foo_rstr(rstr.as_ref());
44         foo_rstr(rstr);
45         foo_rslice(rslice.as_ref());
46         foo_rslice(rslice);
47     }
48     {
49         foo_mrslice(mrslice.as_mut());
50         foo_mrslice(mrslice);
51         foo_rslice(mrslice.as_ref());
52         foo_rslice(mrslice);
53     }
54
55     {
56         let rrrrrstr = &&&&rstr;
57         let rrrrrslice = &&&&&*mrslice;
58         foo_rslice(rrrrrslice.as_ref());
59         foo_rslice(rrrrrslice);
60         foo_rstr(rrrrrstr.as_ref());
61         foo_rstr(rrrrrstr);
62     }
63     {
64         let mrrrrrslice = &mut &mut &mut &mut mrslice;
65         foo_mrslice(mrrrrrslice.as_mut());
66         foo_mrslice(mrrrrrslice);
67         foo_rslice(mrrrrrslice.as_ref());
68         foo_rslice(mrrrrrslice);
69     }
70     #[allow(unused_parens, clippy::double_parens, clippy::needless_borrow)]
71     foo_rrrrmr((&&&&MoreRef).as_ref());
72
73     generic_not_ok(mrslice);
74     generic_ok(mrslice);
75 }
76
77 fn ok() {
78     let string = "hello".to_owned();
79     let mut arr = [1, 2, 3];
80     let mut vec = vec![1, 2, 3];
81
82     {
83         foo_rstr(string.as_ref());
84         foo_rslice(arr.as_ref());
85         foo_rslice(vec.as_ref());
86     }
87     {
88         foo_mrslice(arr.as_mut());
89         foo_mrslice(vec.as_mut());
90     }
91
92     {
93         let rrrrstring = &&&&string;
94         let rrrrarr = &&&&arr;
95         let rrrrvec = &&&&vec;
96         foo_rstr(rrrrstring.as_ref());
97         foo_rslice(rrrrarr.as_ref());
98         foo_rslice(rrrrvec.as_ref());
99     }
100     {
101         let mrrrrarr = &mut &mut &mut &mut arr;
102         let mrrrrvec = &mut &mut &mut &mut vec;
103         foo_mrslice(mrrrrarr.as_mut());
104         foo_mrslice(mrrrrvec.as_mut());
105     }
106     FakeAsRef.as_ref();
107     foo_rrrrmr(MoreRef.as_ref());
108
109     generic_not_ok(arr.as_mut());
110     generic_ok(&mut arr);
111 }
112
113 fn foo_mrt<T: Debug + ?Sized>(t: &mut T) {
114     println!("{:?}", t);
115 }
116 fn foo_rt<T: Debug + ?Sized>(t: &T) {
117     println!("{:?}", t);
118 }
119
120 fn generic_not_ok<T: AsMut<T> + AsRef<T> + Debug + ?Sized>(mrt: &mut T) {
121     foo_mrt(mrt.as_mut());
122     foo_mrt(mrt);
123     foo_rt(mrt.as_ref());
124     foo_rt(mrt);
125 }
126
127 fn generic_ok<U: AsMut<T> + AsRef<T> + ?Sized, T: Debug + ?Sized>(mru: &mut U) {
128     foo_mrt(mru.as_mut());
129     foo_rt(mru.as_ref());
130 }
131
132 fn main() {
133     not_ok();
134     ok();
135 }