]> git.lizzy.rs Git - rust.git/blob - tests/ui/needless_borrow.rs
Rollup merge of #5422 - nickrtorres:contributing-triage, r=flip1995
[rust.git] / tests / ui / needless_borrow.rs
1 // run-rustfix
2
3 #![allow(clippy::needless_borrowed_reference)]
4
5 fn x(y: &i32) -> i32 {
6     *y
7 }
8
9 #[warn(clippy::all, clippy::needless_borrow)]
10 #[allow(unused_variables)]
11 fn main() {
12     let a = 5;
13     let b = x(&a);
14     let c = x(&&a);
15     let s = &String::from("hi");
16     let s_ident = f(&s); // should not error, because `&String` implements Copy, but `String` does not
17     let g_val = g(&Vec::new()); // should not error, because `&Vec<T>` derefs to `&[T]`
18     let vec = Vec::new();
19     let vec_val = g(&vec); // should not error, because `&Vec<T>` derefs to `&[T]`
20     h(&"foo"); // should not error, because the `&&str` is required, due to `&Trait`
21     if let Some(ref cake) = Some(&5) {}
22     let garbl = match 42 {
23         44 => &a,
24         45 => {
25             println!("foo");
26             &&a // FIXME: this should lint, too
27         },
28         46 => &&a,
29         _ => panic!(),
30     };
31 }
32
33 fn f<T: Copy>(y: &T) -> T {
34     *y
35 }
36
37 fn g(y: &[u8]) -> u8 {
38     y[0]
39 }
40
41 trait Trait {}
42
43 impl<'a> Trait for &'a str {}
44
45 fn h(_: &dyn Trait) {}
46 #[warn(clippy::needless_borrow)]
47 #[allow(dead_code)]
48 fn issue_1432() {
49     let mut v = Vec::<String>::new();
50     let _ = v.iter_mut().filter(|&ref a| a.is_empty());
51     let _ = v.iter().filter(|&ref a| a.is_empty());
52
53     let _ = v.iter().filter(|&a| a.is_empty());
54 }
55
56 #[allow(dead_code)]
57 #[warn(clippy::needless_borrow)]
58 #[derive(Debug)]
59 enum Foo<'a> {
60     Str(&'a str),
61 }