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