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();
57 clone_then_move_cloned();
59 false_negative_5707();
64 fn with_branch(a: Alpha, b: bool) -> (Alpha, Alpha) {
65 if b { (a.clone(), a.clone()) } else { (Alpha, a) }
68 fn cannot_double_move(a: Alpha) -> (Alpha, Alpha) {
76 impl Drop for TypeWithDrop {
80 fn cannot_move_from_type_with_drop() -> String {
81 let s = TypeWithDrop { x: String::new() };
82 s.x.clone() // removing this `clone()` summons E0509
85 fn borrower_propagation() {
86 let s = String::new();
87 let t = String::new();
93 let _u = if b() { &s } else { &t };
95 // ok; `s` and `t` are possibly borrowed
103 let _s = s.clone(); // ok
104 let _t = t.clone(); // ok
110 let _ = s.clone(); // ok
113 let _s = s.clone(); // ok
117 use std::convert::identity as id;
119 let _s = s.clone(); // ok, `u` borrows `s`
131 let f = Foo { x: 123 };
137 let f = Foo { x: 123 };
139 let _f = f.clone(); // ok
144 let x = std::path::PathBuf::from("home");
145 let y = x.clone().join("matthias");
146 // join() creates a new owned PathBuf, does not take a &mut to x variable, thus the .clone() is
147 // redundant. (It also does not consume the PathBuf)
149 println!("x: {:?}, y: {:?}", x, y);
151 let mut s = String::new();
152 s.clone().push_str("foo"); // OK, removing this `clone()` will change the behavior.
154 assert_eq!(s, "bar");
158 if let Some(x) = t.clone() {
166 #[allow(clippy::clone_on_copy)]
168 let a: [String; 1] = [String::from("foo")];
169 let _b: String = a[0].clone();
171 let c: [usize; 2] = [2, 3];
172 let _d: usize = c[1].clone();
176 use std::mem::ManuallyDrop;
179 let a = ManuallyDrop::new(Arc::new("Hello!".to_owned()));
180 let _ = a.clone(); // OK
182 let p: *const String = Arc::into_raw(ManuallyDrop::into_inner(a));
189 fn clone_then_move_cloned() {
191 let x = Some(String::new());
192 // ok, x is moved while the clone is in use.
193 assert_eq!(x.clone(), None, "not equal {}", x.unwrap());
196 fn foo<F: Fn()>(_: &Alpha, _: F) {}
198 // ok, data is moved while the clone is in use.
199 foo(&x.clone(), move || {
208 let mut x = S(String::new());
209 x.0.clone().chars().for_each(|_| x.m());
214 use std::collections::HashMap;
215 use std::path::PathBuf;
217 let p = PathBuf::from("/");
219 let mut h: HashMap<&str, &str> = HashMap::new();
220 h.insert("orig-p", p.to_str().unwrap());
222 let mut q = p.clone();
225 println!("{:?} {}", h, q.display());
228 fn false_negative_5707() {
229 fn foo(_x: &Alpha, _y: &mut Alpha) {}
234 let _z = x.clone(); // pr 7346 can't lint on `x`