Skip to content

Commit 54e2051

Browse files
committed
Auto merge of #3886 - phansch:rustfix_useless_asref, r=flip1995
Enable rustfix for `useless_asref` lint tests cc #3630
2 parents e780641 + 0019ca5 commit 54e2051

File tree

3 files changed

+152
-12
lines changed

3 files changed

+152
-12
lines changed

tests/ui/useless_asref.fixed

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

tests/ui/useless_asref.rs

+4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
1+
// run-rustfix
2+
13
#![deny(clippy::useless_asref)]
24
#![allow(clippy::trivially_copy_pass_by_ref)]
5+
36
use std::fmt::Debug;
47

58
struct FakeAsRef;
@@ -65,6 +68,7 @@ fn not_ok() {
6568
foo_rslice(mrrrrrslice.as_ref());
6669
foo_rslice(mrrrrrslice);
6770
}
71+
#[allow(unused_parens, clippy::double_parens)]
6872
foo_rrrrmr((&&&&MoreRef).as_ref());
6973

7074
generic_not_ok(mrslice);

tests/ui/useless_asref.stderr

+12-12
Original file line numberDiff line numberDiff line change
@@ -1,71 +1,71 @@
11
error: this call to `as_ref` does nothing
2-
--> $DIR/useless_asref.rs:41:18
2+
--> $DIR/useless_asref.rs:44:18
33
|
44
LL | foo_rstr(rstr.as_ref());
55
| ^^^^^^^^^^^^^ help: try this: `rstr`
66
|
77
note: lint level defined here
8-
--> $DIR/useless_asref.rs:1:9
8+
--> $DIR/useless_asref.rs:3:9
99
|
1010
LL | #![deny(clippy::useless_asref)]
1111
| ^^^^^^^^^^^^^^^^^^^^^
1212

1313
error: this call to `as_ref` does nothing
14-
--> $DIR/useless_asref.rs:43:20
14+
--> $DIR/useless_asref.rs:46:20
1515
|
1616
LL | foo_rslice(rslice.as_ref());
1717
| ^^^^^^^^^^^^^^^ help: try this: `rslice`
1818

1919
error: this call to `as_mut` does nothing
20-
--> $DIR/useless_asref.rs:47:21
20+
--> $DIR/useless_asref.rs:50:21
2121
|
2222
LL | foo_mrslice(mrslice.as_mut());
2323
| ^^^^^^^^^^^^^^^^ help: try this: `mrslice`
2424

2525
error: this call to `as_ref` does nothing
26-
--> $DIR/useless_asref.rs:49:20
26+
--> $DIR/useless_asref.rs:52:20
2727
|
2828
LL | foo_rslice(mrslice.as_ref());
2929
| ^^^^^^^^^^^^^^^^ help: try this: `mrslice`
3030

3131
error: this call to `as_ref` does nothing
32-
--> $DIR/useless_asref.rs:56:20
32+
--> $DIR/useless_asref.rs:59:20
3333
|
3434
LL | foo_rslice(rrrrrslice.as_ref());
3535
| ^^^^^^^^^^^^^^^^^^^ help: try this: `rrrrrslice`
3636

3737
error: this call to `as_ref` does nothing
38-
--> $DIR/useless_asref.rs:58:18
38+
--> $DIR/useless_asref.rs:61:18
3939
|
4040
LL | foo_rstr(rrrrrstr.as_ref());
4141
| ^^^^^^^^^^^^^^^^^ help: try this: `rrrrrstr`
4242

4343
error: this call to `as_mut` does nothing
44-
--> $DIR/useless_asref.rs:63:21
44+
--> $DIR/useless_asref.rs:66:21
4545
|
4646
LL | foo_mrslice(mrrrrrslice.as_mut());
4747
| ^^^^^^^^^^^^^^^^^^^^ help: try this: `mrrrrrslice`
4848

4949
error: this call to `as_ref` does nothing
50-
--> $DIR/useless_asref.rs:65:20
50+
--> $DIR/useless_asref.rs:68:20
5151
|
5252
LL | foo_rslice(mrrrrrslice.as_ref());
5353
| ^^^^^^^^^^^^^^^^^^^^ help: try this: `mrrrrrslice`
5454

5555
error: this call to `as_ref` does nothing
56-
--> $DIR/useless_asref.rs:68:16
56+
--> $DIR/useless_asref.rs:72:16
5757
|
5858
LL | foo_rrrrmr((&&&&MoreRef).as_ref());
5959
| ^^^^^^^^^^^^^^^^^^^^^^ help: try this: `(&&&&MoreRef)`
6060

6161
error: this call to `as_mut` does nothing
62-
--> $DIR/useless_asref.rs:118:13
62+
--> $DIR/useless_asref.rs:122:13
6363
|
6464
LL | foo_mrt(mrt.as_mut());
6565
| ^^^^^^^^^^^^ help: try this: `mrt`
6666

6767
error: this call to `as_ref` does nothing
68-
--> $DIR/useless_asref.rs:120:12
68+
--> $DIR/useless_asref.rs:124:12
6969
|
7070
LL | foo_rt(mrt.as_ref());
7171
| ^^^^^^^^^^^^ help: try this: `mrt`

0 commit comments

Comments
 (0)