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