]> git.proxmox.com Git - rustc.git/blob - tests/ui/lint/noop-method-call.rs
New upstream version 1.71.1+dfsg1
[rustc.git] / tests / ui / lint / noop-method-call.rs
1 // check-pass
2
3 #![allow(unused)]
4 #![warn(noop_method_call)]
5
6 use std::borrow::Borrow;
7 use std::ops::Deref;
8
9 struct PlainType<T>(T);
10
11 #[derive(Clone)]
12 struct CloneType<T>(T);
13
14 fn main() {
15 let non_clone_type_ref = &PlainType(1u32);
16 let non_clone_type_ref_clone: &PlainType<u32> = non_clone_type_ref.clone();
17 //~^ WARNING call to `.clone()` on a reference in this situation does nothing
18
19 let clone_type_ref = &CloneType(1u32);
20 let clone_type_ref_clone: CloneType<u32> = clone_type_ref.clone();
21
22 let clone_type_ref = &&CloneType(1u32);
23 let clone_type_ref_clone: &CloneType<u32> = clone_type_ref.clone();
24 //~^ WARNING using `.clone()` on a double reference, which returns `&CloneType<u32>`
25
26 let non_deref_type = &PlainType(1u32);
27 let non_deref_type_deref: &PlainType<u32> = non_deref_type.deref();
28 //~^ WARNING call to `.deref()` on a reference in this situation does nothing
29
30 let non_deref_type = &&PlainType(1u32);
31 let non_deref_type_deref: &PlainType<u32> = non_deref_type.deref();
32 //~^ WARNING using `.deref()` on a double reference, which returns `&PlainType<u32>`
33
34 let non_borrow_type = &PlainType(1u32);
35 let non_borrow_type_borrow: &PlainType<u32> = non_borrow_type.borrow();
36 //~^ WARNING call to `.borrow()` on a reference in this situation does nothing
37
38 // Borrowing a &&T does not warn since it has collapsed the double reference
39 let non_borrow_type = &&PlainType(1u32);
40 let non_borrow_type_borrow: &PlainType<u32> = non_borrow_type.borrow();
41
42 let xs = ["a", "b", "c"];
43 let _v: Vec<&str> = xs.iter().map(|x| x.clone()).collect(); // could use `*x` instead
44 //~^ WARNING using `.clone()` on a double reference, which returns `&str`
45 }
46
47 fn generic<T>(non_clone_type: &PlainType<T>) {
48 non_clone_type.clone();
49 //~^ WARNING call to `.clone()` on a reference in this situation does nothing
50 }
51
52 fn non_generic(non_clone_type: &PlainType<u32>) {
53 non_clone_type.clone();
54 //~^ WARNING call to `.clone()` on a reference in this situation does nothing
55 }