]> git.lizzy.rs Git - rust.git/blob - src/tools/clippy/tests/ui/redundant_clone.fixed
1525f6a93dfdd4dcac0492dad3adbf63e8f621f5
[rust.git] / src / tools / clippy / tests / ui / redundant_clone.fixed
1 // run-rustfix
2 // rustfix-only-machine-applicable
3
4 #![allow(clippy::implicit_clone, clippy::drop_non_drop)]
5 use std::ffi::OsString;
6 use std::path::Path;
7
8 fn main() {
9     let _s = ["lorem", "ipsum"].join(" ");
10
11     let s = String::from("foo");
12     let _s = s;
13
14     let s = String::from("foo");
15     let _s = s;
16
17     let s = String::from("foo");
18     let _s = s;
19
20     let _s = Path::new("/a/b/").join("c");
21
22     let _s = Path::new("/a/b/").join("c");
23
24     let _s = OsString::new();
25
26     let _s = OsString::new();
27
28     // Check that lint level works
29     #[allow(clippy::redundant_clone)]
30     let _s = String::new().to_string();
31
32     let tup = (String::from("foo"),);
33     let _t = tup.0;
34
35     let tup_ref = &(String::from("foo"),);
36     let _s = tup_ref.0.clone(); // this `.clone()` cannot be removed
37
38     {
39         let x = String::new();
40         let y = &x;
41
42         let _x = x.clone(); // ok; `x` is borrowed by `y`
43
44         let _ = y.len();
45     }
46
47     let x = (String::new(),);
48     let _ = Some(String::new()).unwrap_or_else(|| x.0.clone()); // ok; closure borrows `x`
49
50     with_branch(Alpha, true);
51     cannot_double_move(Alpha);
52     cannot_move_from_type_with_drop();
53     borrower_propagation();
54     not_consumed();
55     issue_5405();
56     manually_drop();
57     clone_then_move_cloned();
58     hashmap_neg();
59     false_negative_5707();
60 }
61
62 #[derive(Clone)]
63 struct Alpha;
64 fn with_branch(a: Alpha, b: bool) -> (Alpha, Alpha) {
65     if b { (a.clone(), a) } else { (Alpha, a) }
66 }
67
68 fn cannot_double_move(a: Alpha) -> (Alpha, Alpha) {
69     (a.clone(), a)
70 }
71
72 struct TypeWithDrop {
73     x: String,
74 }
75
76 impl Drop for TypeWithDrop {
77     fn drop(&mut self) {}
78 }
79
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
83 }
84
85 fn borrower_propagation() {
86     let s = String::new();
87     let t = String::new();
88
89     {
90         fn b() -> bool {
91             unimplemented!()
92         }
93         let _u = if b() { &s } else { &t };
94
95         // ok; `s` and `t` are possibly borrowed
96         let _s = s.clone();
97         let _t = t.clone();
98     }
99
100     {
101         let _u = || s.len();
102         let _v = [&t; 32];
103         let _s = s.clone(); // ok
104         let _t = t.clone(); // ok
105     }
106
107     {
108         let _u = {
109             let u = Some(&s);
110             let _ = s.clone(); // ok
111             u
112         };
113         let _s = s.clone(); // ok
114     }
115
116     {
117         use std::convert::identity as id;
118         let _u = id(id(&s));
119         let _s = s.clone(); // ok, `u` borrows `s`
120     }
121
122     let _s = s;
123     let _t = t;
124
125     #[derive(Clone)]
126     struct Foo {
127         x: usize,
128     }
129
130     {
131         let f = Foo { x: 123 };
132         let _x = Some(f.x);
133         let _f = f;
134     }
135
136     {
137         let f = Foo { x: 123 };
138         let _x = &f.x;
139         let _f = f.clone(); // ok
140     }
141 }
142
143 fn not_consumed() {
144     let x = std::path::PathBuf::from("home");
145     let y = x.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)
148
149     println!("x: {:?}, y: {:?}", x, y);
150
151     let mut s = String::new();
152     s.clone().push_str("foo"); // OK, removing this `clone()` will change the behavior.
153     s.push_str("bar");
154     assert_eq!(s, "bar");
155
156     let t = Some(s);
157     // OK
158     if let Some(x) = t.clone() {
159         println!("{}", x);
160     }
161     if let Some(x) = t {
162         println!("{}", x);
163     }
164 }
165
166 #[allow(clippy::clone_on_copy)]
167 fn issue_5405() {
168     let a: [String; 1] = [String::from("foo")];
169     let _b: String = a[0].clone();
170
171     let c: [usize; 2] = [2, 3];
172     let _d: usize = c[1].clone();
173 }
174
175 fn manually_drop() {
176     use std::mem::ManuallyDrop;
177     use std::sync::Arc;
178
179     let a = ManuallyDrop::new(Arc::new("Hello!".to_owned()));
180     let _ = a.clone(); // OK
181
182     let p: *const String = Arc::into_raw(ManuallyDrop::into_inner(a));
183     unsafe {
184         Arc::from_raw(p);
185         Arc::from_raw(p);
186     }
187 }
188
189 fn clone_then_move_cloned() {
190     // issue #5973
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());
194
195     // issue #5595
196     fn foo<F: Fn()>(_: &Alpha, _: F) {}
197     let x = Alpha;
198     // ok, data is moved while the clone is in use.
199     foo(&x, move || {
200         let _ = x;
201     });
202
203     // issue #6998
204     struct S(String);
205     impl S {
206         fn m(&mut self) {}
207     }
208     let mut x = S(String::new());
209     x.0.clone().chars().for_each(|_| x.m());
210 }
211
212 fn hashmap_neg() {
213     // issue 5707
214     use std::collections::HashMap;
215     use std::path::PathBuf;
216
217     let p = PathBuf::from("/");
218
219     let mut h: HashMap<&str, &str> = HashMap::new();
220     h.insert("orig-p", p.to_str().unwrap());
221
222     let mut q = p.clone();
223     q.push("foo");
224
225     println!("{:?} {}", h, q.display());
226 }
227
228 fn false_negative_5707() {
229     fn foo(_x: &Alpha, _y: &mut Alpha) {}
230
231     let x = Alpha;
232     let mut y = Alpha;
233     foo(&x, &mut y);
234     let _z = x.clone(); // pr 7346 can't lint on `x`
235     drop(y);
236 }