]> git.lizzy.rs Git - rust.git/blob - tests/ui/needless_borrow.rs
Merge different parent walking loops in `dereference.rs`
[rust.git] / tests / ui / needless_borrow.rs
1 // run-rustfix
2
3 #![feature(lint_reasons)]
4
5 #[warn(clippy::all, clippy::needless_borrow)]
6 #[allow(unused_variables, clippy::unnecessary_mut_passed)]
7 fn main() {
8     let a = 5;
9     let ref_a = &a;
10     let _ = x(&a); // no warning
11     let _ = x(&&a); // warn
12
13     let mut b = 5;
14     mut_ref(&mut b); // no warning
15     mut_ref(&mut &mut b); // warn
16
17     let s = &String::from("hi");
18     let s_ident = f(&s); // should not error, because `&String` implements Copy, but `String` does not
19     let g_val = g(&Vec::new()); // should not error, because `&Vec<T>` derefs to `&[T]`
20     let vec = Vec::new();
21     let vec_val = g(&vec); // should not error, because `&Vec<T>` derefs to `&[T]`
22     h(&"foo"); // should not error, because the `&&str` is required, due to `&Trait`
23     let garbl = match 42 {
24         44 => &a,
25         45 => {
26             println!("foo");
27             &&a
28         },
29         46 => &&a,
30         47 => {
31             println!("foo");
32             loop {
33                 println!("{}", a);
34                 if a == 25 {
35                     break &ref_a;
36                 }
37             }
38         },
39         _ => panic!(),
40     };
41
42     let _ = x(&&&a);
43     let _ = x(&mut &&a);
44     let _ = x(&&&mut b);
45     let _ = x(&&ref_a);
46     {
47         let b = &mut b;
48         x(&b);
49     }
50
51     // Issue #8191
52     let mut x = 5;
53     let mut x = &mut x;
54
55     mut_ref(&mut x);
56     mut_ref(&mut &mut x);
57     let y: &mut i32 = &mut x;
58     let y: &mut i32 = &mut &mut x;
59
60     let y = match 0 {
61         // Don't lint. Removing the borrow would move 'x'
62         0 => &mut x,
63         _ => &mut *x,
64     };
65     let y: &mut i32 = match 0 {
66         // Lint here. The type given above triggers auto-borrow.
67         0 => &mut x,
68         _ => &mut *x,
69     };
70     fn ref_mut_i32(_: &mut i32) {}
71     ref_mut_i32(match 0 {
72         // Lint here. The type given above triggers auto-borrow.
73         0 => &mut x,
74         _ => &mut *x,
75     });
76     // use 'x' after to make sure it's still usable in the fixed code.
77     *x = 5;
78
79     let s = String::new();
80     // let _ = (&s).len();
81     // let _ = (&s).capacity();
82     // let _ = (&&s).capacity();
83
84     let x = (1, 2);
85     let _ = (&x).0;
86     let x = &x as *const (i32, i32);
87     let _ = unsafe { (&*x).0 };
88 }
89
90 #[allow(clippy::needless_borrowed_reference)]
91 fn x(y: &i32) -> i32 {
92     *y
93 }
94
95 fn mut_ref(y: &mut i32) {
96     *y = 5;
97 }
98
99 fn f<T: Copy>(y: &T) -> T {
100     *y
101 }
102
103 fn g(y: &[u8]) -> u8 {
104     y[0]
105 }
106
107 trait Trait {}
108
109 impl<'a> Trait for &'a str {}
110
111 fn h(_: &dyn Trait) {}
112
113 #[allow(dead_code)]
114 fn check_expect_suppression() {
115     let a = 5;
116     #[expect(clippy::needless_borrow)]
117     let _ = x(&&a);
118 }