fork/rust

git clone https://git.julia.blue/fork/rust

borsAuto merge of #103046 - JanBeh:PR_clarify_cmp_terminology, r=workingjubilee3bcad65fbf6

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