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