2 // rustfix-only-machine-applicable
4 #![allow(clippy::implicit_clone)]
5 use std::ffi::OsString;
9 let _s = ["lorem", "ipsum"].join(" ");
11 let s = String::from("foo");
14 let s = String::from("foo");
17 let s = String::from("foo");
20 let _s = Path::new("/a/b/").join("c");
22 let _s = Path::new("/a/b/").join("c");
24 let _s = OsString::new();
26 let _s = OsString::new();
28 // Check that lint level works
29 #[allow(clippy::redundant_clone)]
30 let _s = String::new().to_string();
32 let tup = (String::from("foo"),);
35 let tup_ref = &(String::from("foo"),);
36 let _s = tup_ref.0.clone(); // this `.clone()` cannot be removed
39 let x = String::new();
42 let _x = x.clone(); // ok; `x` is borrowed by `y`
47 let x = (String::new(),);
48 let _ = Some(String::new()).unwrap_or_else(|| x.0.clone()); // ok; closure borrows `x`
50 with_branch(Alpha, true);
51 cannot_double_move(Alpha);
52 cannot_move_from_type_with_drop();
53 borrower_propagation();
57 clone_then_move_cloned();
62 fn with_branch(a: Alpha, b: bool) -> (Alpha, Alpha) {
63 if b { (a.clone(), a) } else { (Alpha, a) }
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.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() {
164 #[allow(clippy::clone_on_copy)]
166 let a: [String; 1] = [String::from("foo")];
167 let _b: String = a[0].clone();
169 let c: [usize; 2] = [2, 3];
170 let _d: usize = c[1].clone();
174 use std::mem::ManuallyDrop;
177 let a = ManuallyDrop::new(Arc::new("Hello!".to_owned()));
178 let _ = a.clone(); // OK
180 let p: *const String = Arc::into_raw(ManuallyDrop::into_inner(a));
187 fn clone_then_move_cloned() {
189 let x = Some(String::new());
190 // ok, x is moved while the clone is in use.
191 assert_eq!(x.clone(), None, "not equal {}", x.unwrap());
194 fn foo<F: Fn()>(_: &Alpha, _: F) {}
196 // ok, data is moved while the clone is in use.
197 foo(&x.clone(), move || {
206 let mut x = S(String::new());
207 x.0.clone().chars().for_each(|_| x.m());