]> git.lizzy.rs Git - rust.git/blob - tests/ui/unnecessary_clone.rs
Fix suggestion for multiple derefs
[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
11
12
13 #![warn(clippy::clone_on_ref_ptr)]
14 #![allow(unused)]
15
16 use std::cell::RefCell;
17 use std::collections::HashSet;
18 use std::collections::VecDeque;
19 use std::rc::{self, Rc};
20 use std::sync::{self, Arc};
21
22 trait SomeTrait {}
23 struct SomeImpl;
24 impl SomeTrait for SomeImpl {}
25
26 fn main() {}
27
28 fn clone_on_copy() {
29     42.clone();
30
31     vec![1].clone(); // ok, not a Copy type
32     Some(vec![1]).clone(); // ok, not a Copy type
33     (&42).clone();
34
35     let rc = RefCell::new(0);
36     rc.borrow().clone();
37 }
38
39 fn clone_on_ref_ptr() {
40     let rc = Rc::new(true);
41     let arc = Arc::new(true);
42
43     let rcweak = Rc::downgrade(&rc);
44     let arc_weak = Arc::downgrade(&arc);
45
46     rc.clone();
47     Rc::clone(&rc);
48
49     arc.clone();
50     Arc::clone(&arc);
51
52     rcweak.clone();
53     rc::Weak::clone(&rcweak);
54
55     arc_weak.clone();
56     sync::Weak::clone(&arc_weak);
57
58     let x = Arc::new(SomeImpl);
59     let _: Arc<SomeTrait> = x.clone();
60 }
61
62 fn clone_on_copy_generic<T: Copy>(t: T) {
63     t.clone();
64
65     Some(t).clone();
66 }
67
68 fn clone_on_double_ref() {
69     let x = vec![1];
70     let y = &&x;
71     let z: &Vec<_> = y.clone();
72
73     println!("{:p} {:p}",*y, z);
74 }
75
76 fn iter_clone_collect() {
77     let v = [1,2,3,4,5];
78     let v2 : Vec<isize> = v.iter().cloned().collect();
79     let v3 : HashSet<isize> = v.iter().cloned().collect();
80     let v4 : VecDeque<isize> = v.iter().cloned().collect();
81 }
82
83 mod many_derefs {
84     struct A;
85     struct B;
86     struct C;
87     struct D;
88     #[derive(Copy, Clone)]
89     struct E;
90
91     macro_rules! impl_deref {
92         ($src:ident, $dst:ident) => {
93             impl std::ops::Deref for $src {
94                 type Target = $dst;
95                 fn deref(&self) -> &Self::Target { &$dst }
96             }
97         }
98     }
99
100     impl_deref!(A, B);
101     impl_deref!(B, C);
102     impl_deref!(C, D);
103     impl std::ops::Deref for D {
104         type Target = &'static E;
105         fn deref(&self) -> &Self::Target { &&E }
106     }
107
108     fn go1() {
109         let a = A;
110         let _: E = a.clone();
111         let _: E = *****a;
112     }
113 }