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