Skip to content

Commit 4e5744d

Browse files
test(ref_binding_to_reference): enable autofix (#17371)
Multi-span suggestions work now! Unlike the lint, unfortunately -- see #17370. changelog: none
2 parents 8c03205 + 63f2678 commit 4e5744d

3 files changed

Lines changed: 116 additions & 41 deletions

File tree

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
#![warn(clippy::ref_binding_to_reference)]
2+
#![expect(clippy::explicit_auto_deref)]
3+
#![allow(clippy::needless_borrowed_reference)]
4+
5+
fn f1(_: &str) {}
6+
macro_rules! m2 {
7+
($e:expr) => {
8+
f1(*$e)
9+
};
10+
}
11+
macro_rules! m3 {
12+
($i:ident) => {
13+
Some(ref $i)
14+
};
15+
}
16+
17+
fn main() {
18+
let x = String::new();
19+
20+
// Ok, the pattern is from a macro
21+
let _: &&String = match Some(&x) {
22+
m3!(x) => x,
23+
None => return,
24+
};
25+
26+
// Err, reference to a &String
27+
#[expect(
28+
clippy::ref_binding_to_reference,
29+
reason = "The suggestion doesn't compile, see https://github.com/rust-lang/rust-clippy/issues/17370"
30+
)]
31+
let _: &&String = match Some(&x) {
32+
Some(ref x) => x,
33+
None => return,
34+
};
35+
36+
// Err, reference to a &String
37+
#[expect(
38+
clippy::ref_binding_to_reference,
39+
reason = "The suggestion doesn't compile, see https://github.com/rust-lang/rust-clippy/issues/17370"
40+
)]
41+
let _: &&String = match Some(&x) {
42+
Some(ref x) => {
43+
f1(x);
44+
f1(*x);
45+
x
46+
},
47+
None => return,
48+
};
49+
50+
// Err, reference to a &String
51+
match Some(&x) {
52+
Some(x) => m2!(&x),
53+
//~^ ref_binding_to_reference
54+
None => return,
55+
}
56+
57+
// Err, reference to a &String
58+
let _ = |&x: &&String| {
59+
//~^ ref_binding_to_reference
60+
61+
let _: &&String = &x;
62+
};
63+
}
64+
65+
// Err, reference to a &String
66+
fn f2<'a>(&x: &&'a String) -> &'a String {
67+
//~^ ref_binding_to_reference
68+
69+
let _: &&String = &x;
70+
x
71+
}
72+
73+
trait T1 {
74+
// Err, reference to a &String
75+
fn f(&x: &&String) {
76+
//~^ ref_binding_to_reference
77+
78+
let _: &&String = &x;
79+
}
80+
}
81+
82+
struct S;
83+
impl T1 for S {
84+
// Err, reference to a &String
85+
fn f(&x: &&String) {
86+
//~^ ref_binding_to_reference
87+
88+
let _: &&String = &x;
89+
}
90+
}
91+
92+
fn check_expect_suppression() {
93+
let x = String::new();
94+
#[expect(clippy::ref_binding_to_reference)]
95+
let _: &&String = match Some(&x) {
96+
Some(ref x) => x,
97+
None => return,
98+
};
99+
}

tests/ui/ref_binding_to_reference.rs

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
1-
// FIXME: run-rustfix waiting on multi-span suggestions
2-
//@no-rustfix
31
#![warn(clippy::ref_binding_to_reference)]
4-
#![expect(clippy::explicit_auto_deref, clippy::needless_borrowed_reference)]
2+
#![expect(clippy::explicit_auto_deref)]
3+
#![allow(clippy::needless_borrowed_reference)]
54

65
fn f1(_: &str) {}
76
macro_rules! m2 {
@@ -25,17 +24,22 @@ fn main() {
2524
};
2625

2726
// Err, reference to a &String
27+
#[expect(
28+
clippy::ref_binding_to_reference,
29+
reason = "The suggestion doesn't compile, see https://github.com/rust-lang/rust-clippy/issues/17370"
30+
)]
2831
let _: &&String = match Some(&x) {
2932
Some(ref x) => x,
30-
//~^ ref_binding_to_reference
3133
None => return,
3234
};
3335

3436
// Err, reference to a &String
37+
#[expect(
38+
clippy::ref_binding_to_reference,
39+
reason = "The suggestion doesn't compile, see https://github.com/rust-lang/rust-clippy/issues/17370"
40+
)]
3541
let _: &&String = match Some(&x) {
3642
Some(ref x) => {
37-
//~^ ref_binding_to_reference
38-
3943
f1(x);
4044
f1(*x);
4145
x
Lines changed: 7 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,19 @@
11
error: this pattern creates a reference to a reference
2-
--> tests/ui/ref_binding_to_reference.rs:29:14
2+
--> tests/ui/ref_binding_to_reference.rs:52:14
33
|
4-
LL | Some(ref x) => x,
4+
LL | Some(ref x) => m2!(x),
55
| ^^^^^
66
|
77
= note: `-D clippy::ref-binding-to-reference` implied by `-D warnings`
88
= help: to override `-D warnings` add `#[allow(clippy::ref_binding_to_reference)]`
99
help: try
1010
|
11-
LL - Some(ref x) => x,
12-
LL + Some(x) => &x,
13-
|
14-
15-
error: this pattern creates a reference to a reference
16-
--> tests/ui/ref_binding_to_reference.rs:36:14
17-
|
18-
LL | Some(ref x) => {
19-
| ^^^^^
20-
|
21-
help: try
22-
|
23-
LL ~ Some(x) => {
24-
LL |
25-
LL |
26-
LL | f1(x);
27-
LL ~ f1(x);
28-
LL ~ &x
29-
|
30-
31-
error: this pattern creates a reference to a reference
32-
--> tests/ui/ref_binding_to_reference.rs:48:14
33-
|
34-
LL | Some(ref x) => m2!(x),
35-
| ^^^^^
36-
|
37-
help: try
38-
|
3911
LL - Some(ref x) => m2!(x),
4012
LL + Some(x) => m2!(&x),
4113
|
4214

4315
error: this pattern creates a reference to a reference
44-
--> tests/ui/ref_binding_to_reference.rs:54:15
16+
--> tests/ui/ref_binding_to_reference.rs:58:15
4517
|
4618
LL | let _ = |&ref x: &&String| {
4719
| ^^^^^
@@ -55,7 +27,7 @@ LL ~ let _: &&String = &x;
5527
|
5628

5729
error: this pattern creates a reference to a reference
58-
--> tests/ui/ref_binding_to_reference.rs:62:12
30+
--> tests/ui/ref_binding_to_reference.rs:66:12
5931
|
6032
LL | fn f2<'a>(&ref x: &&'a String) -> &'a String {
6133
| ^^^^^
@@ -70,7 +42,7 @@ LL ~ x
7042
|
7143

7244
error: this pattern creates a reference to a reference
73-
--> tests/ui/ref_binding_to_reference.rs:71:11
45+
--> tests/ui/ref_binding_to_reference.rs:75:11
7446
|
7547
LL | fn f(&ref x: &&String) {
7648
| ^^^^^
@@ -84,7 +56,7 @@ LL ~ let _: &&String = &x;
8456
|
8557

8658
error: this pattern creates a reference to a reference
87-
--> tests/ui/ref_binding_to_reference.rs:81:11
59+
--> tests/ui/ref_binding_to_reference.rs:85:11
8860
|
8961
LL | fn f(&ref x: &&String) {
9062
| ^^^^^
@@ -97,5 +69,5 @@ LL |
9769
LL ~ let _: &&String = &x;
9870
|
9971

100-
error: aborting due to 7 previous errors
72+
error: aborting due to 5 previous errors
10173

0 commit comments

Comments
 (0)