]> git.lizzy.rs Git - rust.git/blobdiff - tests/ui/redundant_clone.fixed
iterate List by value
[rust.git] / tests / ui / redundant_clone.fixed
index e5e706e8483e6c43c459921b2427cf1651bd81f9..764c10a6d398feaa87957ed4bdbe7f353f7fb657 100644 (file)
@@ -18,11 +18,11 @@ fn main() {
 
     let _s = Path::new("/a/b/").join("c");
 
-    let _s = Path::new("/a/b/").join("c").to_path_buf();
+    let _s = Path::new("/a/b/").join("c");
 
     let _s = OsString::new();
 
-    let _s = OsString::new().to_os_string();
+    let _s = OsString::new();
 
     // Check that lint level works
     #[allow(clippy::redundant_clone)]
@@ -50,6 +50,8 @@ fn main() {
     cannot_double_move(Alpha);
     cannot_move_from_type_with_drop();
     borrower_propagation();
+    not_consumed();
+    issue_5405();
 }
 
 #[derive(Clone)]
@@ -136,3 +138,35 @@ fn borrower_propagation() {
         let _f = f.clone(); // ok
     }
 }
+
+fn not_consumed() {
+    let x = std::path::PathBuf::from("home");
+    let y = x.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();
+}