]> git.lizzy.rs Git - rust.git/blob - tests/ui/needless_borrow.fixed
Auto merge of #7847 - mikerite:fix-7829, r=flip1995
[rust.git] / tests / ui / needless_borrow.fixed
1 // run-rustfix
2
3 #[warn(clippy::all, clippy::needless_borrow)]
4 #[allow(unused_variables)]
5 fn main() {
6     let a = 5;
7     let _ = x(&a); // no warning
8     let _ = x(&a); // warn
9
10     let mut b = 5;
11     mut_ref(&mut b); // no warning
12     mut_ref(&mut b); // warn
13
14     let s = &String::from("hi");
15     let s_ident = f(&s); // should not error, because `&String` implements Copy, but `String` does not
16     let g_val = g(&Vec::new()); // should not error, because `&Vec<T>` derefs to `&[T]`
17     let vec = Vec::new();
18     let vec_val = g(&vec); // should not error, because `&Vec<T>` derefs to `&[T]`
19     h(&"foo"); // should not error, because the `&&str` is required, due to `&Trait`
20     let garbl = match 42 {
21         44 => &a,
22         45 => {
23             println!("foo");
24             &&a // FIXME: this should lint, too
25         },
26         46 => &a,
27         _ => panic!(),
28     };
29 }
30
31 #[allow(clippy::needless_borrowed_reference)]
32 fn x(y: &i32) -> i32 {
33     *y
34 }
35
36 fn mut_ref(y: &mut i32) {
37     *y = 5;
38 }
39
40 fn f<T: Copy>(y: &T) -> T {
41     *y
42 }
43
44 fn g(y: &[u8]) -> u8 {
45     y[0]
46 }
47
48 trait Trait {}
49
50 impl<'a> Trait for &'a str {}
51
52 fn h(_: &dyn Trait) {}