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