]> git.lizzy.rs Git - rust.git/blob - tests/ui/unnecessary_clone.rs
Ignore associated items in trait *implementations* when considering type complexity
[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, clippy::redundant_clone, clippy::unnecessary_wraps)]
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_ref_ptr() {
17     let rc = Rc::new(true);
18     let arc = Arc::new(true);
19
20     let rcweak = Rc::downgrade(&rc);
21     let arc_weak = Arc::downgrade(&arc);
22
23     rc.clone();
24     Rc::clone(&rc);
25
26     arc.clone();
27     Arc::clone(&arc);
28
29     rcweak.clone();
30     rc::Weak::clone(&rcweak);
31
32     arc_weak.clone();
33     sync::Weak::clone(&arc_weak);
34
35     let x = Arc::new(SomeImpl);
36     let _: Arc<dyn SomeTrait> = x.clone();
37 }
38
39 fn clone_on_copy_generic<T: Copy>(t: T) {
40     t.clone();
41
42     Some(t).clone();
43 }
44
45 fn clone_on_double_ref() {
46     let x = vec![1];
47     let y = &&x;
48     let z: &Vec<_> = y.clone();
49
50     println!("{:p} {:p}", *y, z);
51 }
52
53 mod many_derefs {
54     struct A;
55     struct B;
56     struct C;
57     struct D;
58     #[derive(Copy, Clone)]
59     struct E;
60
61     macro_rules! impl_deref {
62         ($src:ident, $dst:ident) => {
63             impl std::ops::Deref for $src {
64                 type Target = $dst;
65                 fn deref(&self) -> &Self::Target {
66                     &$dst
67                 }
68             }
69         };
70     }
71
72     impl_deref!(A, B);
73     impl_deref!(B, C);
74     impl_deref!(C, D);
75     impl std::ops::Deref for D {
76         type Target = &'static E;
77         fn deref(&self) -> &Self::Target {
78             &&E
79         }
80     }
81
82     fn go1() {
83         let a = A;
84         let _: E = a.clone();
85         let _: E = *****a;
86     }
87
88     fn check(mut encoded: &[u8]) {
89         let _ = &mut encoded.clone();
90         let _ = &encoded.clone();
91     }
92 }
93
94 mod issue2076 {
95     use std::rc::Rc;
96
97     macro_rules! try_opt {
98         ($expr: expr) => {
99             match $expr {
100                 Some(value) => value,
101                 None => return None,
102             }
103         };
104     }
105
106     fn func() -> Option<Rc<u8>> {
107         let rc = Rc::new(42);
108         Some(try_opt!(Some(rc)).clone())
109     }
110 }