fork/rust

git clone https://git.julia.blue/fork/rust

borsAuto merge of #103046 - JanBeh:PR_clarify_cmp_terminology, r=workingjubilee3bcad65fbf6

master
4.5 KiB228 linesraw
1#![feature(lint_reasons)]
2#![allow(
3    unused,
4    clippy::uninlined_format_args,
5    clippy::unnecessary_mut_passed,
6    clippy::unnecessary_to_owned,
7    clippy::unnecessary_literal_unwrap
8)]
9#![warn(clippy::needless_borrow)]
10
11fn main() {
12    let a = 5;
13    let ref_a = &a;
14    let _ = x(&a); // no warning
15    let _ = x(&a); // warn
16
17    let mut b = 5;
18    mut_ref(&mut b); // no warning
19    mut_ref(&mut b); // warn
20
21    let s = &String::from("hi");
22    let s_ident = f(&s); // should not error, because `&String` implements Copy, but `String` does not
23    let g_val = g(&Vec::new()); // should not error, because `&Vec<T>` derefs to `&[T]`
24    let vec = Vec::new();
25    let vec_val = g(&vec); // should not error, because `&Vec<T>` derefs to `&[T]`
26    h(&"foo"); // should not error, because the `&&str` is required, due to `&Trait`
27    let garbl = match 42 {
28        44 => &a,
29        45 => {
30            println!("foo");
31            &a
32        },
33        46 => &a,
34        47 => {
35            println!("foo");
36            loop {
37                println!("{}", a);
38                if a == 25 {
39                    break ref_a;
40                }
41            }
42        },
43        _ => panic!(),
44    };
45
46    let _ = x(&a);
47    let _ = x(&a);
48    let _ = x(&mut b);
49    let _ = x(ref_a);
50    {
51        let b = &mut b;
52        x(b);
53    }
54
55    // Issue #8191
56    let mut x = 5;
57    let mut x = &mut x;
58
59    mut_ref(x);
60    mut_ref(x);
61    let y: &mut i32 = x;
62    let y: &mut i32 = x;
63
64    let y = match 0 {
65        // Don't lint. Removing the borrow would move 'x'
66        0 => &mut x,
67        _ => &mut *x,
68    };
69    let y: &mut i32 = match 0 {
70        // Lint here. The type given above triggers auto-borrow.
71        0 => x,
72        _ => &mut *x,
73    };
74    fn ref_mut_i32(_: &mut i32) {}
75    ref_mut_i32(match 0 {
76        // Lint here. The type given above triggers auto-borrow.
77        0 => x,
78        _ => &mut *x,
79    });
80    // use 'x' after to make sure it's still usable in the fixed code.
81    *x = 5;
82
83    let s = String::new();
84    // let _ = (&s).len();
85    // let _ = (&s).capacity();
86    // let _ = (&&s).capacity();
87
88    let x = (1, 2);
89    let _ = x.0;
90    let x = &x as *const (i32, i32);
91    let _ = unsafe { (*x).0 };
92
93    // Issue #8367
94    trait Foo {
95        fn foo(self);
96    }
97    impl Foo for &'_ () {
98        fn foo(self) {}
99    }
100    (&()).foo(); // Don't lint. `()` doesn't implement `Foo`
101    (&()).foo();
102
103    impl Foo for i32 {
104        fn foo(self) {}
105    }
106    impl Foo for &'_ i32 {
107        fn foo(self) {}
108    }
109    (&5).foo(); // Don't lint. `5` will call `<i32 as Foo>::foo`
110    (&5).foo();
111
112    trait FooRef {
113        fn foo_ref(&self);
114    }
115    impl FooRef for () {
116        fn foo_ref(&self) {}
117    }
118    impl FooRef for &'_ () {
119        fn foo_ref(&self) {}
120    }
121    (&&()).foo_ref(); // Don't lint. `&()` will call `<() as FooRef>::foo_ref`
122
123    struct S;
124    impl From<S> for u32 {
125        fn from(s: S) -> Self {
126            (&s).into()
127        }
128    }
129    impl From<&S> for u32 {
130        fn from(s: &S) -> Self {
131            0
132        }
133    }
134}
135
136#[allow(clippy::needless_borrowed_reference)]
137fn x(y: &i32) -> i32 {
138    *y
139}
140
141fn mut_ref(y: &mut i32) {
142    *y = 5;
143}
144
145fn f<T: Copy>(y: &T) -> T {
146    *y
147}
148
149fn g(y: &[u8]) -> u8 {
150    y[0]
151}
152
153trait Trait {}
154
155impl<'a> Trait for &'a str {}
156
157fn h(_: &dyn Trait) {}
158
159fn check_expect_suppression() {
160    let a = 5;
161    #[expect(clippy::needless_borrow)]
162    let _ = x(&&a);
163}
164
165mod issue9160 {
166    pub struct S<F> {
167        f: F,
168    }
169
170    impl<T, F> S<F>
171    where
172        F: Fn() -> T,
173    {
174        fn calls_field(&self) -> T {
175            (self.f)()
176        }
177    }
178
179    impl<T, F> S<F>
180    where
181        F: FnMut() -> T,
182    {
183        fn calls_mut_field(&mut self) -> T {
184            (self.f)()
185        }
186    }
187}
188
189fn issue9383() {
190    // Should not lint because unions need explicit deref when accessing field
191    use std::mem::ManuallyDrop;
192
193    union Coral {
194        crab: ManuallyDrop<Vec<i32>>,
195    }
196
197    union Ocean {
198        coral: ManuallyDrop<Coral>,
199    }
200
201    let mut ocean = Ocean {
202        coral: ManuallyDrop::new(Coral {
203            crab: ManuallyDrop::new(vec![1, 2, 3]),
204        }),
205    };
206
207    unsafe {
208        ManuallyDrop::drop(&mut (&mut ocean.coral).crab);
209
210        (*ocean.coral).crab = ManuallyDrop::new(vec![4, 5, 6]);
211        ManuallyDrop::drop(&mut (*ocean.coral).crab);
212
213        ManuallyDrop::drop(&mut ocean.coral);
214    }
215}
216
217mod issue_10253 {
218    struct S;
219    trait X {
220        fn f<T>(&self);
221    }
222    impl X for &S {
223        fn f<T>(&self) {}
224    }
225    fn f() {
226        (&S).f::<()>();
227    }
228}