1 // Copyright 2016 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
4 //
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
10 
11 //! Test that #[derive(Copy, Clone)] produces a shallow copy
12 //! even when a member violates RFC 1521
13 
14 #![allow(clippy::clone_on_copy)]
15 
16 #[cfg(feature = "use_core")]
17 extern crate core;
18 
19 #[macro_use]
20 extern crate derivative;
21 
22 use std::sync::atomic::{AtomicBool, Ordering};
23 
24 /// A struct that pretends to be Copy, but actually does something
25 /// in its Clone impl
26 #[derive(Copy)]
27 struct Liar;
28 
29 /// Static cooperating with the rogue Clone impl
30 static CLONED: AtomicBool = AtomicBool::new(false);
31 
32 impl Clone for Liar {
clone(&self) -> Self33     fn clone(&self) -> Self {
34         // this makes Clone vs Copy observable
35         CLONED.store(true, Ordering::SeqCst);
36 
37         *self
38     }
39 }
40 
41 /// This struct is actually Copy... at least, it thinks it is!
42 #[derive(Copy, Clone)]
43 struct TheirTheir(Liar);
44 
45 #[derive(Derivative)]
46 #[derivative(Copy, Clone)]
47 struct OurOur1(Liar);
48 #[derive(Derivative)]
49 #[derivative(Clone, Copy)]
50 struct OurOur2(Liar);
51 
52 #[test]
main()53 fn main() {
54     let _ = TheirTheir(Liar).clone();
55     assert!(!CLONED.load(Ordering::SeqCst), "TheirTheir");
56 
57     let _ = OurOur1(Liar).clone();
58     assert!(!CLONED.load(Ordering::SeqCst), "OurOur1");
59     let _ = OurOur2(Liar).clone();
60     assert!(!CLONED.load(Ordering::SeqCst), "OurOur2");
61 }
62