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