]> git.lizzy.rs Git - rust.git/blob - tests/ui/useless_asref.rs
Auto merge of #3529 - matthiaskrgr:rustfmt_tests, r=phansch
[rust.git] / tests / ui / useless_asref.rs
1 // Copyright 2014-2018 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution.
3 //
4 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
5 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
6 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
7 // option. This file may not be copied, modified, or distributed
8 // except according to those terms.
9
10 #![deny(clippy::useless_asref)]
11 #![allow(clippy::trivially_copy_pass_by_ref)]
12 use std::fmt::Debug;
13
14 struct FakeAsRef;
15
16 #[allow(clippy::should_implement_trait)]
17 impl FakeAsRef {
18     fn as_ref(&self) -> &Self {
19         self
20     }
21 }
22
23 struct MoreRef;
24
25 impl<'a, 'b, 'c> AsRef<&'a &'b &'c MoreRef> for MoreRef {
26     fn as_ref(&self) -> &&'a &'b &'c MoreRef {
27         &&&&MoreRef
28     }
29 }
30
31 fn foo_rstr(x: &str) {
32     println!("{:?}", x);
33 }
34 fn foo_rslice(x: &[i32]) {
35     println!("{:?}", x);
36 }
37 fn foo_mrslice(x: &mut [i32]) {
38     println!("{:?}", x);
39 }
40 fn foo_rrrrmr(_: &&&&MoreRef) {
41     println!("so many refs");
42 }
43
44 fn not_ok() {
45     let rstr: &str = "hello";
46     let mut mrslice: &mut [i32] = &mut [1, 2, 3];
47
48     {
49         let rslice: &[i32] = &*mrslice;
50         foo_rstr(rstr.as_ref());
51         foo_rstr(rstr);
52         foo_rslice(rslice.as_ref());
53         foo_rslice(rslice);
54     }
55     {
56         foo_mrslice(mrslice.as_mut());
57         foo_mrslice(mrslice);
58         foo_rslice(mrslice.as_ref());
59         foo_rslice(mrslice);
60     }
61
62     {
63         let rrrrrstr = &&&&rstr;
64         let rrrrrslice = &&&&&*mrslice;
65         foo_rslice(rrrrrslice.as_ref());
66         foo_rslice(rrrrrslice);
67         foo_rstr(rrrrrstr.as_ref());
68         foo_rstr(rrrrrstr);
69     }
70     {
71         let mrrrrrslice = &mut &mut &mut &mut mrslice;
72         foo_mrslice(mrrrrrslice.as_mut());
73         foo_mrslice(mrrrrrslice);
74         foo_rslice(mrrrrrslice.as_ref());
75         foo_rslice(mrrrrrslice);
76     }
77     foo_rrrrmr((&&&&MoreRef).as_ref());
78
79     generic_not_ok(mrslice);
80     generic_ok(mrslice);
81 }
82
83 fn ok() {
84     let string = "hello".to_owned();
85     let mut arr = [1, 2, 3];
86     let mut vec = vec![1, 2, 3];
87
88     {
89         foo_rstr(string.as_ref());
90         foo_rslice(arr.as_ref());
91         foo_rslice(vec.as_ref());
92     }
93     {
94         foo_mrslice(arr.as_mut());
95         foo_mrslice(vec.as_mut());
96     }
97
98     {
99         let rrrrstring = &&&&string;
100         let rrrrarr = &&&&arr;
101         let rrrrvec = &&&&vec;
102         foo_rstr(rrrrstring.as_ref());
103         foo_rslice(rrrrarr.as_ref());
104         foo_rslice(rrrrvec.as_ref());
105     }
106     {
107         let mrrrrarr = &mut &mut &mut &mut arr;
108         let mrrrrvec = &mut &mut &mut &mut vec;
109         foo_mrslice(mrrrrarr.as_mut());
110         foo_mrslice(mrrrrvec.as_mut());
111     }
112     FakeAsRef.as_ref();
113     foo_rrrrmr(MoreRef.as_ref());
114
115     generic_not_ok(arr.as_mut());
116     generic_ok(&mut arr);
117 }
118
119 fn foo_mrt<T: Debug + ?Sized>(t: &mut T) {
120     println!("{:?}", t);
121 }
122 fn foo_rt<T: Debug + ?Sized>(t: &T) {
123     println!("{:?}", t);
124 }
125
126 fn generic_not_ok<T: AsMut<T> + AsRef<T> + Debug + ?Sized>(mrt: &mut T) {
127     foo_mrt(mrt.as_mut());
128     foo_mrt(mrt);
129     foo_rt(mrt.as_ref());
130     foo_rt(mrt);
131 }
132
133 fn generic_ok<U: AsMut<T> + AsRef<T> + ?Sized, T: Debug + ?Sized>(mru: &mut U) {
134     foo_mrt(mru.as_mut());
135     foo_rt(mru.as_ref());
136 }
137
138 fn main() {
139     not_ok();
140     ok();
141 }