]> git.lizzy.rs Git - rust.git/blob - tests/ui/redundant_clone.rs
Auto merge of #5304 - sinkuu:redundant_clone_not_consumed, r=flip1995
[rust.git] / 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 }
55
56 #[derive(Clone)]
57 struct Alpha;
58 fn with_branch(a: Alpha, b: bool) -> (Alpha, Alpha) {
59     if b {
60         (a.clone(), a.clone())
61     } else {
62         (Alpha, a)
63     }
64 }
65
66 fn cannot_double_move(a: Alpha) -> (Alpha, Alpha) {
67     (a.clone(), a)
68 }
69
70 struct TypeWithDrop {
71     x: String,
72 }
73
74 impl Drop for TypeWithDrop {
75     fn drop(&mut self) {}
76 }
77
78 fn cannot_move_from_type_with_drop() -> String {
79     let s = TypeWithDrop { x: String::new() };
80     s.x.clone() // removing this `clone()` summons E0509
81 }
82
83 fn borrower_propagation() {
84     let s = String::new();
85     let t = String::new();
86
87     {
88         fn b() -> bool {
89             unimplemented!()
90         }
91         let _u = if b() { &s } else { &t };
92
93         // ok; `s` and `t` are possibly borrowed
94         let _s = s.clone();
95         let _t = t.clone();
96     }
97
98     {
99         let _u = || s.len();
100         let _v = [&t; 32];
101         let _s = s.clone(); // ok
102         let _t = t.clone(); // ok
103     }
104
105     {
106         let _u = {
107             let u = Some(&s);
108             let _ = s.clone(); // ok
109             u
110         };
111         let _s = s.clone(); // ok
112     }
113
114     {
115         use std::convert::identity as id;
116         let _u = id(id(&s));
117         let _s = s.clone(); // ok, `u` borrows `s`
118     }
119
120     let _s = s.clone();
121     let _t = t.clone();
122
123     #[derive(Clone)]
124     struct Foo {
125         x: usize,
126     }
127
128     {
129         let f = Foo { x: 123 };
130         let _x = Some(f.x);
131         let _f = f.clone();
132     }
133
134     {
135         let f = Foo { x: 123 };
136         let _x = &f.x;
137         let _f = f.clone(); // ok
138     }
139 }
140
141 fn not_consumed() {
142     let x = std::path::PathBuf::from("home");
143     let y = x.clone().join("matthias");
144     // join() creates a new owned PathBuf, does not take a &mut to x variable, thus the .clone() is
145     // redundant. (It also does not consume the PathBuf)
146
147     println!("x: {:?}, y: {:?}", x, y);
148
149     let mut s = String::new();
150     s.clone().push_str("foo"); // OK, removing this `clone()` will change the behavior.
151     s.push_str("bar");
152     assert_eq!(s, "bar");
153
154     let t = Some(s);
155     // OK
156     if let Some(x) = t.clone() {
157         println!("{}", x);
158     }
159     if let Some(x) = t {
160         println!("{}", x);
161     }
162 }