]> git.lizzy.rs Git - rust.git/blobdiff - tests/ui/redundant_clone.fixed
Merge commit 'b40ea209e7f14c8193ddfc98143967b6a2f4f5c9' into clippyup
[rust.git] / tests / ui / redundant_clone.fixed
index 764c10a6d398feaa87957ed4bdbe7f353f7fb657..f5da703cd1dea5e9d524f8c2950ec629525faec2 100644 (file)
@@ -1,6 +1,7 @@
 // run-rustfix
 // rustfix-only-machine-applicable
 
+#![allow(clippy::implicit_clone)]
 use std::ffi::OsString;
 use std::path::Path;
 
@@ -52,16 +53,14 @@ fn main() {
     borrower_propagation();
     not_consumed();
     issue_5405();
+    manually_drop();
+    clone_then_move_cloned();
 }
 
 #[derive(Clone)]
 struct Alpha;
 fn with_branch(a: Alpha, b: bool) -> (Alpha, Alpha) {
-    if b {
-        (a.clone(), a)
-    } else {
-        (Alpha, a)
-    }
+    if b { (a.clone(), a) } else { (Alpha, a) }
 }
 
 fn cannot_double_move(a: Alpha) -> (Alpha, Alpha) {
@@ -170,3 +169,40 @@ fn issue_5405() {
     let c: [usize; 2] = [2, 3];
     let _d: usize = c[1].clone();
 }
+
+fn manually_drop() {
+    use std::mem::ManuallyDrop;
+    use std::sync::Arc;
+
+    let a = ManuallyDrop::new(Arc::new("Hello!".to_owned()));
+    let _ = a.clone(); // OK
+
+    let p: *const String = Arc::into_raw(ManuallyDrop::into_inner(a));
+    unsafe {
+        Arc::from_raw(p);
+        Arc::from_raw(p);
+    }
+}
+
+fn clone_then_move_cloned() {
+    // issue #5973
+    let x = Some(String::new());
+    // ok, x is moved while the clone is in use.
+    assert_eq!(x.clone(), None, "not equal {}", x.unwrap());
+
+    // issue #5595
+    fn foo<F: Fn()>(_: &Alpha, _: F) {}
+    let x = Alpha;
+    // ok, data is moved while the clone is in use.
+    foo(&x.clone(), move || {
+        let _ = x;
+    });
+
+    // issue #6998
+    struct S(String);
+    impl S {
+        fn m(&mut self) {}
+    }
+    let mut x = S(String::new());
+    x.0.clone().chars().for_each(|_| x.m());
+}