forked from rust-lang/rust
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathref_binding_to_reference.rs
76 lines (65 loc) · 1.38 KB
/
ref_binding_to_reference.rs
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
// edition:2018
// FIXME: run-rustfix waiting on multi-span suggestions
#![warn(clippy::ref_binding_to_reference)]
#![allow(clippy::needless_borrowed_reference)]
fn f1(_: &str) {}
macro_rules! m2 {
($e:expr) => {
f1(*$e)
};
}
macro_rules! m3 {
($i:ident) => {
Some(ref $i)
};
}
#[allow(dead_code)]
fn main() {
let x = String::new();
// 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,
None => return,
};
// Err, reference to a &String
let _: &&String = match Some(&x) {
Some(ref x) => {
f1(x);
f1(*x);
x
},
None => return,
};
// Err, reference to a &String
match Some(&x) {
Some(ref x) => m2!(x),
None => return,
}
// Err, reference to a &String
let _ = |&ref x: &&String| {
let _: &&String = x;
};
}
// Err, reference to a &String
fn f2<'a>(&ref x: &&'a String) -> &'a String {
let _: &&String = x;
*x
}
trait T1 {
// Err, reference to a &String
fn f(&ref x: &&String) {
let _: &&String = x;
}
}
struct S;
impl T1 for S {
// Err, reference to a &String
fn f(&ref x: &&String) {
let _: &&String = x;
}
}