]> git.lizzy.rs Git - rust.git/blob - tests/ui/unnecessary_clone.rs
iterate List by value
[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)]
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 is_ascii(ch: char) -> bool {
17     ch.is_ascii()
18 }
19
20 fn clone_on_copy() {
21     42.clone();
22
23     vec![1].clone(); // ok, not a Copy type
24     Some(vec![1]).clone(); // ok, not a Copy type
25     (&42).clone();
26
27     let rc = RefCell::new(0);
28     rc.borrow().clone();
29
30     // Issue #4348
31     let mut x = 43;
32     let _ = &x.clone(); // ok, getting a ref
33     'a'.clone().make_ascii_uppercase(); // ok, clone and then mutate
34     is_ascii('z'.clone());
35
36     // Issue #5436
37     let mut vec = Vec::new();
38     vec.push(42.clone());
39 }
40
41 fn clone_on_ref_ptr() {
42     let rc = Rc::new(true);
43     let arc = Arc::new(true);
44
45     let rcweak = Rc::downgrade(&rc);
46     let arc_weak = Arc::downgrade(&arc);
47
48     rc.clone();
49     Rc::clone(&rc);
50
51     arc.clone();
52     Arc::clone(&arc);
53
54     rcweak.clone();
55     rc::Weak::clone(&rcweak);
56
57     arc_weak.clone();
58     sync::Weak::clone(&arc_weak);
59
60     let x = Arc::new(SomeImpl);
61     let _: Arc<dyn SomeTrait> = x.clone();
62 }
63
64 fn clone_on_copy_generic<T: Copy>(t: T) {
65     t.clone();
66
67     Some(t).clone();
68 }
69
70 fn clone_on_double_ref() {
71     let x = vec![1];
72     let y = &&x;
73     let z: &Vec<_> = y.clone();
74
75     println!("{:p} {:p}", *y, z);
76 }
77
78 mod many_derefs {
79     struct A;
80     struct B;
81     struct C;
82     struct D;
83     #[derive(Copy, Clone)]
84     struct E;
85
86     macro_rules! impl_deref {
87         ($src:ident, $dst:ident) => {
88             impl std::ops::Deref for $src {
89                 type Target = $dst;
90                 fn deref(&self) -> &Self::Target {
91                     &$dst
92                 }
93             }
94         };
95     }
96
97     impl_deref!(A, B);
98     impl_deref!(B, C);
99     impl_deref!(C, D);
100     impl std::ops::Deref for D {
101         type Target = &'static E;
102         fn deref(&self) -> &Self::Target {
103             &&E
104         }
105     }
106
107     fn go1() {
108         let a = A;
109         let _: E = a.clone();
110         let _: E = *****a;
111     }
112
113     fn check(mut encoded: &[u8]) {
114         let _ = &mut encoded.clone();
115         let _ = &encoded.clone();
116     }
117 }