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