]> git.lizzy.rs Git - rust.git/blobdiff - tests/ui/redundant_clone.rs
iterate List by value
[rust.git] / tests / ui / redundant_clone.rs
index 48687c82c2fbb08841af746aac7c36591b5f5658..839747b131d77da22c7d559d0133f7006bd2d246 100644 (file)
@@ -1,5 +1,6 @@
 // run-rustfix
 // rustfix-only-machine-applicable
+
 use std::ffi::OsString;
 use std::path::Path;
 
@@ -46,8 +47,11 @@ fn main() {
     let _ = Some(String::new()).unwrap_or_else(|| x.0.clone()); // ok; closure borrows `x`
 
     with_branch(Alpha, true);
+    cannot_double_move(Alpha);
     cannot_move_from_type_with_drop();
     borrower_propagation();
+    not_consumed();
+    issue_5405();
 }
 
 #[derive(Clone)]
@@ -60,6 +64,10 @@ fn with_branch(a: Alpha, b: bool) -> (Alpha, Alpha) {
     }
 }
 
+fn cannot_double_move(a: Alpha) -> (Alpha, Alpha) {
+    (a.clone(), a)
+}
+
 struct TypeWithDrop {
     x: String,
 }
@@ -130,3 +138,35 @@ struct Foo {
         let _f = f.clone(); // ok
     }
 }
+
+fn not_consumed() {
+    let x = std::path::PathBuf::from("home");
+    let y = x.clone().join("matthias");
+    // join() creates a new owned PathBuf, does not take a &mut to x variable, thus the .clone() is
+    // 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();
+}