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