2 // rustfix-only-machine-applicable
4 use std::ffi::OsString;
8 let _s = ["lorem", "ipsum"].join(" ").to_string();
10 let s = String::from("foo");
13 let s = String::from("foo");
14 let _s = s.to_string();
16 let s = String::from("foo");
17 let _s = s.to_owned();
19 let _s = Path::new("/a/b/").join("c").to_owned();
21 let _s = Path::new("/a/b/").join("c").to_path_buf();
23 let _s = OsString::new().to_owned();
25 let _s = OsString::new().to_os_string();
27 // Check that lint level works
28 #[allow(clippy::redundant_clone)]
29 let _s = String::new().to_string();
31 let tup = (String::from("foo"),);
32 let _t = tup.0.clone();
34 let tup_ref = &(String::from("foo"),);
35 let _s = tup_ref.0.clone(); // this `.clone()` cannot be removed
38 let x = String::new();
41 let _x = x.clone(); // ok; `x` is borrowed by `y`
46 let x = (String::new(),);
47 let _ = Some(String::new()).unwrap_or_else(|| x.0.clone()); // ok; closure borrows `x`
49 with_branch(Alpha, true);
50 cannot_double_move(Alpha);
51 cannot_move_from_type_with_drop();
52 borrower_propagation();
58 fn with_branch(a: Alpha, b: bool) -> (Alpha, Alpha) {
60 (a.clone(), a.clone())
66 fn cannot_double_move(a: Alpha) -> (Alpha, Alpha) {
74 impl Drop for TypeWithDrop {
78 fn cannot_move_from_type_with_drop() -> String {
79 let s = TypeWithDrop { x: String::new() };
80 s.x.clone() // removing this `clone()` summons E0509
83 fn borrower_propagation() {
84 let s = String::new();
85 let t = String::new();
91 let _u = if b() { &s } else { &t };
93 // ok; `s` and `t` are possibly borrowed
101 let _s = s.clone(); // ok
102 let _t = t.clone(); // ok
108 let _ = s.clone(); // ok
111 let _s = s.clone(); // ok
115 use std::convert::identity as id;
117 let _s = s.clone(); // ok, `u` borrows `s`
129 let f = Foo { x: 123 };
135 let f = Foo { x: 123 };
137 let _f = f.clone(); // ok
142 let x = std::path::PathBuf::from("home");
143 let y = x.clone().join("matthias");
144 // join() creates a new owned PathBuf, does not take a &mut to x variable, thus the .clone() is
145 // redundant. (It also does not consume the PathBuf)
147 println!("x: {:?}, y: {:?}", x, y);
149 let mut s = String::new();
150 s.clone().push_str("foo"); // OK, removing this `clone()` will change the behavior.
152 assert_eq!(s, "bar");
156 if let Some(x) = t.clone() {