]> git.lizzy.rs Git - rust.git/blob - tests/ui/needless_borrowed_ref.rs
Merge pull request #3291 from JoshMcguigan/cmp_owned-3289
[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
11
12
13
14 #[warn(clippy::needless_borrowed_reference)]
15 #[allow(unused_variables)]
16 fn main() {
17     let mut v = Vec::<String>::new();
18     let _ = v.iter_mut().filter(|&ref a| a.is_empty());
19     //                            ^ should be linted
20
21     let var = 3;
22     let thingy = Some(&var);
23     if let Some(&ref v) = thingy {
24         //          ^ should be linted
25     }
26
27     let mut var2 = 5;
28     let thingy2 = Some(&mut var2);
29     if let Some(&mut ref mut v) = thingy2 {
30         //          ^ should *not* be linted
31         // v is borrowed as mutable.
32         *v = 10;
33     }
34     if let Some(&mut ref v) = thingy2 {
35         //          ^ should *not* be linted
36         // here, v is borrowed as immutable.
37         // can't do that:
38         //*v = 15;
39     }
40 }
41
42 #[allow(dead_code)]
43 enum Animal {
44     Cat(u64),
45     Dog(u64),
46 }
47
48 #[allow(unused_variables)]
49 #[allow(dead_code)]
50 fn foo(a: &Animal, b: &Animal) {
51     match (a, b) {
52         (&Animal::Cat(v), &ref k) | (&ref k, &Animal::Cat(v)) => (), // lifetime mismatch error if there is no '&ref'
53         //                  ^    and   ^ should *not* be linted
54         (&Animal::Dog(ref a), &Animal::Dog(_)) => ()
55         //              ^ should *not* be linted
56     }
57 }
58