]> git.lizzy.rs Git - rust.git/blob - src/tools/clippy/tests/ui/redundant_clone.rs
acb7ffb305f2a8890b981bfed1c4f5303d8da232
[rust.git] / src / tools / clippy / tests / ui / redundant_clone.rs
1 // run-rustfix
2 // rustfix-only-machine-applicable
3
4 use std::ffi::OsString;
5 use std::path::Path;
6
7 fn main() {
8     let _s = ["lorem", "ipsum"].join(" ").to_string();
9
10     let s = String::from("foo");
11     let _s = s.clone();
12
13     let s = String::from("foo");
14     let _s = s.to_string();
15
16     let s = String::from("foo");
17     let _s = s.to_owned();
18
19     let _s = Path::new("/a/b/").join("c").to_owned();
20
21     let _s = Path::new("/a/b/").join("c").to_path_buf();
22
23     let _s = OsString::new().to_owned();
24
25     let _s = OsString::new().to_os_string();
26
27     // Check that lint level works
28     #[allow(clippy::redundant_clone)]
29     let _s = String::new().to_string();
30
31     let tup = (String::from("foo"),);
32     let _t = tup.0.clone();
33
34     let tup_ref = &(String::from("foo"),);
35     let _s = tup_ref.0.clone(); // this `.clone()` cannot be removed
36
37     {
38         let x = String::new();
39         let y = &x;
40
41         let _x = x.clone(); // ok; `x` is borrowed by `y`
42
43         let _ = y.len();
44     }
45
46     let x = (String::new(),);
47     let _ = Some(String::new()).unwrap_or_else(|| x.0.clone()); // ok; closure borrows `x`
48
49     with_branch(Alpha, true);
50     cannot_double_move(Alpha);
51     cannot_move_from_type_with_drop();
52     borrower_propagation();
53     not_consumed();
54     issue_5405();
55     manually_drop();
56 }
57
58 #[derive(Clone)]
59 struct Alpha;
60 fn with_branch(a: Alpha, b: bool) -> (Alpha, Alpha) {
61     if b {
62         (a.clone(), a.clone())
63     } else {
64         (Alpha, a)
65     }
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.clone();
123     let _t = t.clone();
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.clone();
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.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)
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 }