1 // Copyright 2014-2018 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution.
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.
13 #![deny(clippy::useless_asref)]
14 #![allow(clippy::trivially_copy_pass_by_ref)]
19 #[allow(clippy::should_implement_trait)]
21 fn as_ref(&self) -> &Self { self }
26 impl<'a, 'b, 'c> AsRef<&'a &'b &'c MoreRef> for MoreRef {
27 fn as_ref(&self) -> &&'a &'b &'c MoreRef {
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"); }
38 let rstr: &str = "hello";
39 let mut mrslice: &mut [i32] = &mut [1,2,3];
42 let rslice: &[i32] = &*mrslice;
43 foo_rstr(rstr.as_ref());
45 foo_rslice(rslice.as_ref());
49 foo_mrslice(mrslice.as_mut());
51 foo_rslice(mrslice.as_ref());
56 let rrrrrstr = &&&&rstr;
57 let rrrrrslice = &&&&&*mrslice;
58 foo_rslice(rrrrrslice.as_ref());
59 foo_rslice(rrrrrslice);
60 foo_rstr(rrrrrstr.as_ref());
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);
70 foo_rrrrmr((&&&&MoreRef).as_ref());
72 generic_not_ok(mrslice);
77 let string = "hello".to_owned();
78 let mut arr = [1,2,3];
79 let mut vec = vec![1,2,3];
82 foo_rstr(string.as_ref());
83 foo_rslice(arr.as_ref());
84 foo_rslice(vec.as_ref());
87 foo_mrslice(arr.as_mut());
88 foo_mrslice(vec.as_mut());
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());
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());
106 foo_rrrrmr(MoreRef.as_ref());
108 generic_not_ok(arr.as_mut());
109 generic_ok(&mut arr);
112 fn foo_mrt<T: Debug + ?Sized>(t: &mut T) { println!("{:?}", t); }
113 fn foo_rt<T: Debug + ?Sized>(t: &T) { println!("{:?}", t); }
115 fn generic_not_ok<T: AsMut<T> + AsRef<T> + Debug + ?Sized>(mrt: &mut T) {
116 foo_mrt(mrt.as_mut());
118 foo_rt(mrt.as_ref());
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());