]> git.lizzy.rs Git - rust.git/blob - tests/ui/useless_asref.rs
resolved conflicts
[rust.git] / tests / ui / useless_asref.rs
1 #![feature(tool_lints)]
2
3 #![deny(clippy::useless_asref)]
4 #![allow(clippy::trivially_copy_pass_by_ref)]
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 { self }
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) { println!("{:?}", x); }
23 fn foo_rslice(x: &[i32]) { println!("{:?}", x); }
24 fn foo_mrslice(x: &mut [i32]) { println!("{:?}", x); }
25 fn foo_rrrrmr(_: &&&&MoreRef) { println!("so many refs"); }
26
27 fn not_ok() {
28     let rstr: &str = "hello";
29     let mut mrslice: &mut [i32] = &mut [1,2,3];
30
31     {
32         let rslice: &[i32] = &*mrslice;
33         foo_rstr(rstr.as_ref());
34         foo_rstr(rstr);
35         foo_rslice(rslice.as_ref());
36         foo_rslice(rslice);
37     }
38     {
39         foo_mrslice(mrslice.as_mut());
40         foo_mrslice(mrslice);
41         foo_rslice(mrslice.as_ref());
42         foo_rslice(mrslice);
43     }
44
45     {
46         let rrrrrstr = &&&&rstr;
47         let rrrrrslice = &&&&&*mrslice;
48         foo_rslice(rrrrrslice.as_ref());
49         foo_rslice(rrrrrslice);
50         foo_rstr(rrrrrstr.as_ref());
51         foo_rstr(rrrrrstr);
52     }
53     {
54         let mrrrrrslice = &mut &mut &mut &mut mrslice;
55         foo_mrslice(mrrrrrslice.as_mut());
56         foo_mrslice(mrrrrrslice);
57         foo_rslice(mrrrrrslice.as_ref());
58         foo_rslice(mrrrrrslice);
59     }
60     foo_rrrrmr((&&&&MoreRef).as_ref());
61
62     generic_not_ok(mrslice);
63     generic_ok(mrslice);
64 }
65
66 fn ok() {
67     let string = "hello".to_owned();
68     let mut arr = [1,2,3];
69     let mut vec = vec![1,2,3];
70
71     {
72         foo_rstr(string.as_ref());
73         foo_rslice(arr.as_ref());
74         foo_rslice(vec.as_ref());
75     }
76     {
77         foo_mrslice(arr.as_mut());
78         foo_mrslice(vec.as_mut());
79     }
80
81     {
82         let rrrrstring = &&&&string;
83         let rrrrarr = &&&&arr;
84         let rrrrvec = &&&&vec;
85         foo_rstr(rrrrstring.as_ref());
86         foo_rslice(rrrrarr.as_ref());
87         foo_rslice(rrrrvec.as_ref());
88     }
89     {
90         let mrrrrarr = &mut &mut &mut &mut arr;
91         let mrrrrvec = &mut &mut &mut &mut vec;
92         foo_mrslice(mrrrrarr.as_mut());
93         foo_mrslice(mrrrrvec.as_mut());
94     }
95     FakeAsRef.as_ref();
96     foo_rrrrmr(MoreRef.as_ref());
97
98     generic_not_ok(arr.as_mut());
99     generic_ok(&mut arr);
100 }
101
102 fn foo_mrt<T: Debug + ?Sized>(t: &mut T) { println!("{:?}", t); }
103 fn foo_rt<T: Debug + ?Sized>(t: &T) { println!("{:?}", t); }
104
105 fn generic_not_ok<T: AsMut<T> + AsRef<T> + Debug + ?Sized>(mrt: &mut T) {
106     foo_mrt(mrt.as_mut());
107     foo_mrt(mrt);
108     foo_rt(mrt.as_ref());
109     foo_rt(mrt);
110 }
111
112 fn generic_ok<U: AsMut<T> + AsRef<T> + ?Sized, T: Debug + ?Sized>(mru: &mut U) {
113     foo_mrt(mru.as_mut());
114     foo_rt(mru.as_ref());
115 }
116
117 fn main() {
118     not_ok();
119     ok();
120 }