2 // rustfix-only-machine-applicable
4 #![allow(clippy::implicit_clone)]
5 use std::ffi::OsString;
9 let _s = ["lorem", "ipsum"].join(" ").to_string();
11 let s = String::from("foo");
14 let s = String::from("foo");
15 let _s = s.to_string();
17 let s = String::from("foo");
18 let _s = s.to_owned();
20 let _s = Path::new("/a/b/").join("c").to_owned();
22 let _s = Path::new("/a/b/").join("c").to_path_buf();
24 let _s = OsString::new().to_owned();
26 let _s = OsString::new().to_os_string();
28 // Check that lint level works
29 #[allow(clippy::redundant_clone)]
30 let _s = String::new().to_string();
32 let tup = (String::from("foo"),);
33 let _t = tup.0.clone();
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();
61 fn with_branch(a: Alpha, b: bool) -> (Alpha, Alpha) {
63 (a.clone(), a.clone())
69 fn cannot_double_move(a: Alpha) -> (Alpha, Alpha) {
77 impl Drop for TypeWithDrop {
81 fn cannot_move_from_type_with_drop() -> String {
82 let s = TypeWithDrop { x: String::new() };
83 s.x.clone() // removing this `clone()` summons E0509
86 fn borrower_propagation() {
87 let s = String::new();
88 let t = String::new();
94 let _u = if b() { &s } else { &t };
96 // ok; `s` and `t` are possibly borrowed
104 let _s = s.clone(); // ok
105 let _t = t.clone(); // ok
111 let _ = s.clone(); // ok
114 let _s = s.clone(); // ok
118 use std::convert::identity as id;
120 let _s = s.clone(); // ok, `u` borrows `s`
132 let f = Foo { x: 123 };
138 let f = Foo { x: 123 };
140 let _f = f.clone(); // ok
145 let x = std::path::PathBuf::from("home");
146 let y = x.clone().join("matthias");
147 // join() creates a new owned PathBuf, does not take a &mut to x variable, thus the .clone() is
148 // redundant. (It also does not consume the PathBuf)
150 println!("x: {:?}, y: {:?}", x, y);
152 let mut s = String::new();
153 s.clone().push_str("foo"); // OK, removing this `clone()` will change the behavior.
155 assert_eq!(s, "bar");
159 if let Some(x) = t.clone() {
167 #[allow(clippy::clone_on_copy)]
169 let a: [String; 1] = [String::from("foo")];
170 let _b: String = a[0].clone();
172 let c: [usize; 2] = [2, 3];
173 let _d: usize = c[1].clone();
177 use std::mem::ManuallyDrop;
180 let a = ManuallyDrop::new(Arc::new("Hello!".to_owned()));
181 let _ = a.clone(); // OK
183 let p: *const String = Arc::into_raw(ManuallyDrop::into_inner(a));