1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
|
// FIXME: run-rustfix waiting on multi-span suggestions
#![warn(clippy::needless_borrow)]
#![allow(clippy::needless_borrowed_reference, clippy::explicit_auto_deref)]
fn f1(_: &str) {}
macro_rules! m1 {
($e:expr) => {
f1($e)
};
}
macro_rules! m3 {
($i:ident) => {
Some(ref $i)
};
}
macro_rules! if_chain {
(if $e:expr; $($rest:tt)*) => {
if $e {
if_chain!($($rest)*)
}
};
(if let $p:pat = $e:expr; $($rest:tt)*) => {
if let $p = $e {
if_chain!($($rest)*)
}
};
(then $b:block) => {
$b
};
}
#[allow(dead_code)]
fn main() {
let x = String::new();
// Ok, reference to a String.
let _: &String = match Some(x.clone()) {
Some(ref x) => x,
None => return,
};
// Ok, reference to a &mut String
let _: &&mut String = match Some(&mut x.clone()) {
Some(ref x) => x,
None => return,
};
// Ok, the pattern is from a macro
let _: &String = match Some(&x) {
m3!(x) => x,
None => return,
};
// Err, reference to a &String
let _: &String = match Some(&x) {
Some(ref x) => x,
//~^ needless_borrow
None => return,
};
// Err, reference to a &String.
let _: &String = match Some(&x) {
Some(ref x) => *x,
//~^ needless_borrow
None => return,
};
// Err, reference to a &String
let _: &String = match Some(&x) {
Some(ref x) => {
//~^ needless_borrow
f1(x);
f1(*x);
x
},
None => return,
};
// Err, reference to a &String
match Some(&x) {
Some(ref x) => m1!(x),
//~^ needless_borrow
None => return,
};
// Err, reference to a &String
let _ = |&ref x: &&String| {
//~^ needless_borrow
let _: &String = x;
};
// Err, reference to a &String
let (ref y,) = (&x,);
//~^ needless_borrow
let _: &String = *y;
let y = &&x;
// Ok, different y
let _: &String = *y;
let x = (0, 0);
// Err, reference to a &u32. Don't suggest adding a reference to the field access.
let _: u32 = match Some(&x) {
Some(ref x) => x.0,
//~^ needless_borrow
None => return,
};
enum E {
A(&'static u32),
B(&'static u32),
}
// Err, reference to &u32.
let _: &u32 = match E::A(&0) {
E::A(ref x) | E::B(ref x) => *x,
//~^ needless_borrow
};
// Err, reference to &String.
if_chain! {
if true;
if let Some(ref x) = Some(&String::new());
//~^ needless_borrow
then {
f1(x);
}
}
}
// Err, reference to a &String
fn f2<'a>(&ref x: &&'a String) -> &'a String {
//~^ needless_borrow
let _: &String = x;
*x
}
trait T1 {
// Err, reference to a &String
fn f(&ref x: &&String) {
//~^ needless_borrow
let _: &String = x;
}
}
struct S;
impl T1 for S {
// Err, reference to a &String
fn f(&ref x: &&String) {
//~^ needless_borrow
let _: &String = *x;
}
}
// Ok - used to error due to rustc bug
#[allow(dead_code)]
#[derive(Debug)]
enum Foo<'a> {
Str(&'a str),
}
|