]> git.proxmox.com Git - rustc.git/blob - src/tools/clippy/tests/ui/ref_binding_to_reference.rs
New upstream version 1.56.0~beta.4+dfsg1
[rustc.git] / src / tools / clippy / tests / ui / ref_binding_to_reference.rs
1 // edition:2018
2 // FIXME: run-rustfix waiting on multi-span suggestions
3
4 #![warn(clippy::ref_binding_to_reference)]
5 #![allow(clippy::needless_borrowed_reference)]
6
7 fn f1(_: &str) {}
8 macro_rules! m2 {
9 ($e:expr) => {
10 f1(*$e)
11 };
12 }
13 macro_rules! m3 {
14 ($i:ident) => {
15 Some(ref $i)
16 };
17 }
18
19 #[allow(dead_code)]
20 fn main() {
21 let x = String::new();
22
23 // Ok, the pattern is from a macro
24 let _: &&String = match Some(&x) {
25 m3!(x) => x,
26 None => return,
27 };
28
29 // Err, reference to a &String
30 let _: &&String = match Some(&x) {
31 Some(ref x) => x,
32 None => return,
33 };
34
35 // Err, reference to a &String
36 let _: &&String = match Some(&x) {
37 Some(ref x) => {
38 f1(x);
39 f1(*x);
40 x
41 },
42 None => return,
43 };
44
45 // Err, reference to a &String
46 match Some(&x) {
47 Some(ref x) => m2!(x),
48 None => return,
49 }
50
51 // Err, reference to a &String
52 let _ = |&ref x: &&String| {
53 let _: &&String = x;
54 };
55 }
56
57 // Err, reference to a &String
58 fn f2<'a>(&ref x: &&'a String) -> &'a String {
59 let _: &&String = x;
60 *x
61 }
62
63 trait T1 {
64 // Err, reference to a &String
65 fn f(&ref x: &&String) {
66 let _: &&String = x;
67 }
68 }
69
70 struct S;
71 impl T1 for S {
72 // Err, reference to a &String
73 fn f(&ref x: &&String) {
74 let _: &&String = x;
75 }
76 }