]> git.lizzy.rs Git - rust.git/blobdiff - tests/ui/redundant_clone.fixed
Auto merge of #5820 - ThibsG:FixSuspiciousArithmeticImpl, r=flip1995
[rust.git] / tests / ui / redundant_clone.fixed
index 99d97f9cc3ce57b2ab6ef2627ad10124b5cc55d0..cdeefda4c234c071e573445af3e10e1f0e404702 100644 (file)
@@ -51,6 +51,8 @@ fn main() {
     cannot_move_from_type_with_drop();
     borrower_propagation();
     not_consumed();
+    issue_5405();
+    manually_drop();
 }
 
 #[derive(Clone)]
@@ -145,4 +147,41 @@ fn not_consumed() {
     // redundant. (It also does not consume the PathBuf)
 
     println!("x: {:?}, y: {:?}", x, y);
+
+    let mut s = String::new();
+    s.clone().push_str("foo"); // OK, removing this `clone()` will change the behavior.
+    s.push_str("bar");
+    assert_eq!(s, "bar");
+
+    let t = Some(s);
+    // OK
+    if let Some(x) = t.clone() {
+        println!("{}", x);
+    }
+    if let Some(x) = t {
+        println!("{}", x);
+    }
+}
+
+#[allow(clippy::clone_on_copy)]
+fn issue_5405() {
+    let a: [String; 1] = [String::from("foo")];
+    let _b: String = a[0].clone();
+
+    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);
+    }
 }