]> git.lizzy.rs Git - rust.git/blob - tests/ui/unnecessary_clone.rs
Merge pull request #3265 from mikerite/fix-export
[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 #![feature(tool_lints)]
12
13 #![warn(clippy::clone_on_ref_ptr)]
14 #![allow(unused)]
15
16 use std::collections::HashSet;
17 use std::collections::VecDeque;
18 use std::rc::{self, Rc};
19 use std::sync::{self, Arc};
20
21 trait SomeTrait {}
22 struct SomeImpl;
23 impl SomeTrait for SomeImpl {}
24
25 fn main() {}
26
27 fn clone_on_copy() {
28     42.clone();
29
30     vec![1].clone(); // ok, not a Copy type
31     Some(vec![1]).clone(); // ok, not a Copy type
32     (&42).clone();
33 }
34
35 fn clone_on_ref_ptr() {
36     let rc = Rc::new(true);
37     let arc = Arc::new(true);
38
39     let rcweak = Rc::downgrade(&rc);
40     let arc_weak = Arc::downgrade(&arc);
41
42     rc.clone();
43     Rc::clone(&rc);
44
45     arc.clone();
46     Arc::clone(&arc);
47
48     rcweak.clone();
49     rc::Weak::clone(&rcweak);
50
51     arc_weak.clone();
52     sync::Weak::clone(&arc_weak);
53
54     let x = Arc::new(SomeImpl);
55     let _: Arc<SomeTrait> = x.clone();
56 }
57
58 fn clone_on_copy_generic<T: Copy>(t: T) {
59     t.clone();
60
61     Some(t).clone();
62 }
63
64 fn clone_on_double_ref() {
65     let x = vec![1];
66     let y = &&x;
67     let z: &Vec<_> = y.clone();
68
69     println!("{:p} {:p}",*y, z);
70 }
71
72 fn iter_clone_collect() {
73     let v = [1,2,3,4,5];
74     let v2 : Vec<isize> = v.iter().cloned().collect();
75     let v3 : HashSet<isize> = v.iter().cloned().collect();
76     let v4 : VecDeque<isize> = v.iter().cloned().collect();
77 }