]> git.lizzy.rs Git - rust.git/blob - tests/ui/needless_borrow.rs
f8a170f38d422f140829e0077118f469b3391dbc
[rust.git] / tests / ui / needless_borrow.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 use std::borrow::Cow;
14
15 #[allow(clippy::trivially_copy_pass_by_ref)]
16 fn x(y: &i32) -> i32 {
17     *y
18 }
19
20 #[warn(clippy::all, clippy::needless_borrow)]
21 #[allow(unused_variables)]
22 fn main() {
23     let a = 5;
24     let b = x(&a);
25     let c = x(&&a);
26     let s = &String::from("hi");
27     let s_ident = f(&s); // should not error, because `&String` implements Copy, but `String` does not
28     let g_val = g(&Vec::new()); // should not error, because `&Vec<T>` derefs to `&[T]`
29     let vec = Vec::new();
30     let vec_val = g(&vec); // should not error, because `&Vec<T>` derefs to `&[T]`
31     h(&"foo"); // should not error, because the `&&str` is required, due to `&Trait`
32     if let Some(ref cake) = Some(&5) {}
33     let garbl = match 42 {
34         44 => &a,
35         45 => {
36             println!("foo");
37             &&a // FIXME: this should lint, too
38         },
39         46 => &&a,
40         _ => panic!(),
41     };
42 }
43
44 fn f<T:Copy>(y: &T) -> T {
45     *y
46 }
47
48 fn g(y: &[u8]) -> u8 {
49     y[0]
50 }
51
52 trait Trait {}
53
54 impl<'a> Trait for &'a str {}
55
56 fn h(_: &Trait) {}
57 #[warn(clippy::needless_borrow)]
58 #[allow(dead_code)]
59 fn issue_1432() {
60     let mut v = Vec::<String>::new();
61     let _ = v.iter_mut().filter(|&ref a| a.is_empty());
62     let _ = v.iter().filter(|&ref a| a.is_empty());
63
64     let _ = v.iter().filter(|&a| a.is_empty());
65 }