]> git.lizzy.rs Git - rust.git/blob - tests/ui/unnecessary_clone.rs
Auto merge of #4575 - Manishearth:suggestions, r=oli-obk
[rust.git] / tests / ui / unnecessary_clone.rs
1 // does not test any rustfixable lints
2
3 #![warn(clippy::clone_on_ref_ptr)]
4 #![allow(unused)]
5
6 use std::cell::RefCell;
7 use std::rc::{self, Rc};
8 use std::sync::{self, Arc};
9
10 trait SomeTrait {}
11 struct SomeImpl;
12 impl SomeTrait for SomeImpl {}
13
14 fn main() {}
15
16 fn clone_on_copy() {
17     42.clone();
18
19     vec![1].clone(); // ok, not a Copy type
20     Some(vec![1]).clone(); // ok, not a Copy type
21     (&42).clone();
22
23     let rc = RefCell::new(0);
24     rc.borrow().clone();
25
26     // Issue #4348
27     let mut x = 43;
28     let _ = &x.clone(); // ok, getting a ref
29     'a'.clone().make_ascii_uppercase(); // ok, clone and then mutate
30 }
31
32 fn clone_on_ref_ptr() {
33     let rc = Rc::new(true);
34     let arc = Arc::new(true);
35
36     let rcweak = Rc::downgrade(&rc);
37     let arc_weak = Arc::downgrade(&arc);
38
39     rc.clone();
40     Rc::clone(&rc);
41
42     arc.clone();
43     Arc::clone(&arc);
44
45     rcweak.clone();
46     rc::Weak::clone(&rcweak);
47
48     arc_weak.clone();
49     sync::Weak::clone(&arc_weak);
50
51     let x = Arc::new(SomeImpl);
52     let _: Arc<dyn SomeTrait> = x.clone();
53 }
54
55 fn clone_on_copy_generic<T: Copy>(t: T) {
56     t.clone();
57
58     Some(t).clone();
59 }
60
61 fn clone_on_double_ref() {
62     let x = vec![1];
63     let y = &&x;
64     let z: &Vec<_> = y.clone();
65
66     println!("{:p} {:p}", *y, z);
67 }
68
69 mod many_derefs {
70     struct A;
71     struct B;
72     struct C;
73     struct D;
74     #[derive(Copy, Clone)]
75     struct E;
76
77     macro_rules! impl_deref {
78         ($src:ident, $dst:ident) => {
79             impl std::ops::Deref for $src {
80                 type Target = $dst;
81                 fn deref(&self) -> &Self::Target {
82                     &$dst
83                 }
84             }
85         };
86     }
87
88     impl_deref!(A, B);
89     impl_deref!(B, C);
90     impl_deref!(C, D);
91     impl std::ops::Deref for D {
92         type Target = &'static E;
93         fn deref(&self) -> &Self::Target {
94             &&E
95         }
96     }
97
98     fn go1() {
99         let a = A;
100         let _: E = a.clone();
101         let _: E = *****a;
102     }
103 }