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) {
62 if b { (a.clone(), a.clone()) } else { (Alpha, a) }
65 fn cannot_double_move(a: Alpha) -> (Alpha, Alpha) {
73 impl Drop for TypeWithDrop {
77 fn cannot_move_from_type_with_drop() -> String {
78 let s = TypeWithDrop { x: String::new() };
79 s.x.clone() // removing this `clone()` summons E0509
82 fn borrower_propagation() {
83 let s = String::new();
84 let t = String::new();
90 let _u = if b() { &s } else { &t };
92 // ok; `s` and `t` are possibly borrowed
100 let _s = s.clone(); // ok
101 let _t = t.clone(); // ok
107 let _ = s.clone(); // ok
110 let _s = s.clone(); // ok
114 use std::convert::identity as id;
116 let _s = s.clone(); // ok, `u` borrows `s`
128 let f = Foo { x: 123 };
134 let f = Foo { x: 123 };
136 let _f = f.clone(); // ok
141 let x = std::path::PathBuf::from("home");
142 let y = x.clone().join("matthias");
143 // join() creates a new owned PathBuf, does not take a &mut to x variable, thus the .clone() is
144 // redundant. (It also does not consume the PathBuf)
146 println!("x: {:?}, y: {:?}", x, y);
148 let mut s = String::new();
149 s.clone().push_str("foo"); // OK, removing this `clone()` will change the behavior.
151 assert_eq!(s, "bar");
155 if let Some(x) = t.clone() {
163 #[allow(clippy::clone_on_copy)]
165 let a: [String; 1] = [String::from("foo")];
166 let _b: String = a[0].clone();
168 let c: [usize; 2] = [2, 3];
169 let _d: usize = c[1].clone();
173 use std::mem::ManuallyDrop;
176 let a = ManuallyDrop::new(Arc::new("Hello!".to_owned()));
177 let _ = a.clone(); // OK
179 let p: *const String = Arc::into_raw(ManuallyDrop::into_inner(a));