]> git.lizzy.rs Git - rust.git/blob - tests/ui/unnecessary_clone.rs
Auto merge of #3603 - xfix:random-state-lint, r=phansch
[rust.git] / tests / ui / unnecessary_clone.rs
1 // Copyright 2014-2018 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution.
3 //
4 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
5 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
6 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
7 // option. This file may not be copied, modified, or distributed
8 // except according to those terms.
9
10 #![warn(clippy::clone_on_ref_ptr)]
11 #![allow(unused)]
12
13 use std::cell::RefCell;
14 use std::collections::HashSet;
15 use std::collections::VecDeque;
16 use std::rc::{self, Rc};
17 use std::sync::{self, Arc};
18
19 trait SomeTrait {}
20 struct SomeImpl;
21 impl SomeTrait for SomeImpl {}
22
23 fn main() {}
24
25 fn clone_on_copy() {
26     42.clone();
27
28     vec![1].clone(); // ok, not a Copy type
29     Some(vec![1]).clone(); // ok, not a Copy type
30     (&42).clone();
31
32     let rc = RefCell::new(0);
33     rc.borrow().clone();
34 }
35
36 fn clone_on_ref_ptr() {
37     let rc = Rc::new(true);
38     let arc = Arc::new(true);
39
40     let rcweak = Rc::downgrade(&rc);
41     let arc_weak = Arc::downgrade(&arc);
42
43     rc.clone();
44     Rc::clone(&rc);
45
46     arc.clone();
47     Arc::clone(&arc);
48
49     rcweak.clone();
50     rc::Weak::clone(&rcweak);
51
52     arc_weak.clone();
53     sync::Weak::clone(&arc_weak);
54
55     let x = Arc::new(SomeImpl);
56     let _: Arc<SomeTrait> = x.clone();
57 }
58
59 fn clone_on_copy_generic<T: Copy>(t: T) {
60     t.clone();
61
62     Some(t).clone();
63 }
64
65 fn clone_on_double_ref() {
66     let x = vec![1];
67     let y = &&x;
68     let z: &Vec<_> = y.clone();
69
70     println!("{:p} {:p}", *y, z);
71 }
72
73 fn iter_clone_collect() {
74     let v = [1, 2, 3, 4, 5];
75     let v2: Vec<isize> = v.iter().cloned().collect();
76     let v3: HashSet<isize> = v.iter().cloned().collect();
77     let v4: VecDeque<isize> = v.iter().cloned().collect();
78 }
79
80 mod many_derefs {
81     struct A;
82     struct B;
83     struct C;
84     struct D;
85     #[derive(Copy, Clone)]
86     struct E;
87
88     macro_rules! impl_deref {
89         ($src:ident, $dst:ident) => {
90             impl std::ops::Deref for $src {
91                 type Target = $dst;
92                 fn deref(&self) -> &Self::Target {
93                     &$dst
94                 }
95             }
96         };
97     }
98
99     impl_deref!(A, B);
100     impl_deref!(B, C);
101     impl_deref!(C, D);
102     impl std::ops::Deref for D {
103         type Target = &'static E;
104         fn deref(&self) -> &Self::Target {
105             &&E
106         }
107     }
108
109     fn go1() {
110         let a = A;
111         let _: E = a.clone();
112         let _: E = *****a;
113     }
114 }