]> git.lizzy.rs Git - rust.git/blob - tests/ui/redundant_clone.rs
Rustup to rustc 1.41.0-nightly (d1da8023d 2019-11-19)
[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_move_from_type_with_drop();
51     borrower_propagation();
52 }
53
54 #[derive(Clone)]
55 struct Alpha;
56 fn with_branch(a: Alpha, b: bool) -> (Alpha, Alpha) {
57     if b {
58         (a.clone(), a.clone())
59     } else {
60         (Alpha, a)
61     }
62 }
63
64 struct TypeWithDrop {
65     x: String,
66 }
67
68 impl Drop for TypeWithDrop {
69     fn drop(&mut self) {}
70 }
71
72 fn cannot_move_from_type_with_drop() -> String {
73     let s = TypeWithDrop { x: String::new() };
74     s.x.clone() // removing this `clone()` summons E0509
75 }
76
77 fn borrower_propagation() {
78     let s = String::new();
79     let t = String::new();
80
81     {
82         fn b() -> bool {
83             unimplemented!()
84         }
85         let _u = if b() { &s } else { &t };
86
87         // ok; `s` and `t` are possibly borrowed
88         let _s = s.clone();
89         let _t = t.clone();
90     }
91
92     {
93         let _u = || s.len();
94         let _v = [&t; 32];
95         let _s = s.clone(); // ok
96         let _t = t.clone(); // ok
97     }
98
99     {
100         let _u = {
101             let u = Some(&s);
102             let _ = s.clone(); // ok
103             u
104         };
105         let _s = s.clone(); // ok
106     }
107
108     {
109         use std::convert::identity as id;
110         let _u = id(id(&s));
111         let _s = s.clone(); // ok, `u` borrows `s`
112     }
113
114     let _s = s.clone();
115     let _t = t.clone();
116
117     #[derive(Clone)]
118     struct Foo {
119         x: usize,
120     }
121
122     {
123         let f = Foo { x: 123 };
124         let _x = Some(f.x);
125         let _f = f.clone();
126     }
127
128     {
129         let f = Foo { x: 123 };
130         let _x = &f.x;
131         let _f = f.clone(); // ok
132     }
133 }