]> git.lizzy.rs Git - rust.git/blob - tests/ui/needless_borrowed_ref.rs
Auto merge of #3603 - xfix:random-state-lint, r=phansch
[rust.git] / tests / ui / needless_borrowed_ref.rs
1 // Copyright 2014-2018 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution.
3 //
4 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
5 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
6 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
7 // option. This file may not be copied, modified, or distributed
8 // except according to those terms.
9
10 #[warn(clippy::needless_borrowed_reference)]
11 #[allow(unused_variables)]
12 fn main() {
13     let mut v = Vec::<String>::new();
14     let _ = v.iter_mut().filter(|&ref a| a.is_empty());
15     //                            ^ should be linted
16
17     let var = 3;
18     let thingy = Some(&var);
19     if let Some(&ref v) = thingy {
20         //          ^ should be linted
21     }
22
23     let mut var2 = 5;
24     let thingy2 = Some(&mut var2);
25     if let Some(&mut ref mut v) = thingy2 {
26         //          ^ should *not* be linted
27         // v is borrowed as mutable.
28         *v = 10;
29     }
30     if let Some(&mut ref v) = thingy2 {
31         //          ^ should *not* be linted
32         // here, v is borrowed as immutable.
33         // can't do that:
34         //*v = 15;
35     }
36 }
37
38 #[allow(dead_code)]
39 enum Animal {
40     Cat(u64),
41     Dog(u64),
42 }
43
44 #[allow(unused_variables)]
45 #[allow(dead_code)]
46 fn foo(a: &Animal, b: &Animal) {
47     match (a, b) {
48         (&Animal::Cat(v), &ref k) | (&ref k, &Animal::Cat(v)) => (), // lifetime mismatch error if there is no '&ref'
49         //                  ^    and   ^ should *not* be linted
50         (&Animal::Dog(ref a), &Animal::Dog(_)) => (), //              ^ should *not* be linted
51     }
52 }