about summary refs log tree commit diff
path: root/src/tools/clippy/tests/ui/assigning_clones.rs
blob: 8ae8abfed367ac7dda8f65ca82cf5824d82182f5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
#![allow(unused)]
#![allow(clippy::redundant_clone)]
#![allow(clippy::ptr_arg)] // https://github.com/rust-lang/rust-clippy/issues/10612
#![allow(clippy::needless_late_init)]
#![allow(clippy::box_collection)]
#![allow(clippy::boxed_local)]
#![warn(clippy::assigning_clones)]

use std::borrow::ToOwned;
use std::ops::{Add, Deref, DerefMut};

// Clone
pub struct HasCloneFrom;

impl Clone for HasCloneFrom {
    fn clone(&self) -> Self {
        Self
    }
    fn clone_from(&mut self, source: &Self) {
        *self = HasCloneFrom;
    }
}

fn clone_method_rhs_val(mut_thing: &mut HasCloneFrom, value_thing: HasCloneFrom) {
    *mut_thing = value_thing.clone();
    //~^ assigning_clones
}

fn clone_method_rhs_ref(mut_thing: &mut HasCloneFrom, ref_thing: &HasCloneFrom) {
    *mut_thing = ref_thing.clone();
    //~^ assigning_clones
}

fn clone_method_lhs_val(mut mut_thing: HasCloneFrom, ref_thing: &HasCloneFrom) {
    mut_thing = ref_thing.clone();
    //~^ assigning_clones
}

fn clone_function_lhs_mut_ref(mut_thing: &mut HasCloneFrom, ref_thing: &HasCloneFrom) {
    *mut_thing = Clone::clone(ref_thing);
    //~^ assigning_clones
}

fn clone_function_lhs_val(mut mut_thing: HasCloneFrom, ref_thing: &HasCloneFrom) {
    mut_thing = Clone::clone(ref_thing);
    //~^ assigning_clones
}

fn clone_function_through_trait(mut_thing: &mut HasCloneFrom, ref_thing: &HasCloneFrom) {
    *mut_thing = Clone::clone(ref_thing);
    //~^ assigning_clones
}

fn clone_function_through_type(mut_thing: &mut HasCloneFrom, ref_thing: &HasCloneFrom) {
    *mut_thing = HasCloneFrom::clone(ref_thing);
    //~^ assigning_clones
}

fn clone_function_fully_qualified(mut_thing: &mut HasCloneFrom, ref_thing: &HasCloneFrom) {
    *mut_thing = <HasCloneFrom as Clone>::clone(ref_thing);
    //~^ assigning_clones
}

fn clone_method_lhs_complex(mut_thing: &mut HasCloneFrom, ref_thing: &HasCloneFrom) {
    // These parens should be kept as necessary for a receiver
    *(mut_thing + &mut HasCloneFrom) = ref_thing.clone();
    //~^ assigning_clones
}

fn clone_method_rhs_complex(mut_thing: &mut HasCloneFrom, ref_thing: &HasCloneFrom) {
    // These parens should be removed since they are not needed in a function argument
    *mut_thing = (ref_thing + ref_thing).clone();
    //~^ assigning_clones
}

fn clone_method_macro() {
    let mut s = String::from("");
    s = format!("{} {}", "hello", "world").clone();
    //~^ assigning_clones
}

fn clone_function_macro() {
    let mut s = String::from("");
    s = Clone::clone(&format!("{} {}", "hello", "world"));
    //~^ assigning_clones
}

fn assign_to_init_mut_var(b: HasCloneFrom) -> HasCloneFrom {
    let mut a = HasCloneFrom;
    for _ in 1..10 {
        a = b.clone();
        //~^ assigning_clones
    }
    a
}

fn assign_to_late_init_mut_var(b: HasCloneFrom) {
    let mut a;
    a = HasCloneFrom;
    a = b.clone();
}

fn assign_to_uninit_var(b: HasCloneFrom) {
    let a;
    a = b.clone();
}

fn assign_to_uninit_mut_var(b: HasCloneFrom) {
    let mut a;
    a = b.clone();
}

fn late_init_let_tuple() {
    let (p, q): (String, String);
    p = "ghi".to_string();
    q = p.clone();
}

#[derive(Clone)]
pub struct HasDeriveClone;

fn ignore_derive_clone(a: &mut HasDeriveClone, b: &HasDeriveClone) {
    // Should not be linted, since the Clone impl is derived
    *a = b.clone();
}

pub struct HasCloneImpl;

impl Clone for HasCloneImpl {
    fn clone(&self) -> Self {
        Self
    }
}

fn ignore_missing_clone_from(a: &mut HasCloneImpl, b: &HasCloneImpl) {
    // Should not be linted, since the Clone impl doesn't override clone_from
    *a = b.clone();
}

struct FakeClone;

impl FakeClone {
    /// This looks just like `Clone::clone`
    fn clone(&self) -> Self {
        FakeClone
    }
}

fn ignore_fake_clone() {
    let mut a = FakeClone;
    let b = FakeClone;
    // Should not be linted, since the Clone impl doesn't come from std
    a = b.clone();
}

fn ignore_generic_clone<T: Clone>(a: &mut T, b: &T) {
    // Should not be linted, since we don't know the actual clone impl
    *a = b.clone();
}

#[clippy::msrv = "1.62"]
fn msrv_1_62(mut a: String, b: String, c: &str) {
    a = b.clone();
    //~^ assigning_clones
    // Should not be linted, as clone_into wasn't stabilized until 1.63
    a = c.to_owned();
}

#[clippy::msrv = "1.63"]
fn msrv_1_63(mut a: String, b: String, c: &str) {
    a = b.clone();
    //~^ assigning_clones
    a = c.to_owned();
    //~^ assigning_clones
}

macro_rules! clone_inside {
    ($a:expr, $b: expr) => {
        $a = $b.clone();
    };
}

fn clone_inside_macro() {
    let mut a = String::new();
    let b = String::new();
    clone_inside!(a, b);
}

// Make sure that we don't suggest the lint when we call clone inside a Clone impl
// https://github.com/rust-lang/rust-clippy/issues/12600
pub struct AvoidRecursiveCloneFrom;

impl Clone for AvoidRecursiveCloneFrom {
    fn clone(&self) -> Self {
        Self
    }
    fn clone_from(&mut self, source: &Self) {
        *self = source.clone();
    }
}

// Deref handling
fn clone_into_deref_method(mut a: DerefWrapper<HasCloneFrom>, b: HasCloneFrom) {
    *a = b.clone();
    //~^ assigning_clones
}

fn clone_into_deref_with_clone_method(mut a: DerefWrapperWithClone<HasCloneFrom>, b: HasCloneFrom) {
    *a = b.clone();
    //~^ assigning_clones
}

fn clone_into_box_method(mut a: Box<HasCloneFrom>, b: HasCloneFrom) {
    *a = b.clone();
    //~^ assigning_clones
}

fn clone_into_self_deref_method(a: &mut DerefWrapperWithClone<HasCloneFrom>, b: DerefWrapperWithClone<HasCloneFrom>) {
    *a = b.clone();
    //~^ assigning_clones
}

fn clone_into_deref_function(mut a: DerefWrapper<HasCloneFrom>, b: HasCloneFrom) {
    *a = Clone::clone(&b);
    //~^ assigning_clones
}

fn clone_into_box_function(mut a: Box<HasCloneFrom>, b: HasCloneFrom) {
    *a = Clone::clone(&b);
    //~^ assigning_clones
}

// ToOwned
fn owned_method_mut_ref(mut_string: &mut String, ref_str: &str) {
    *mut_string = ref_str.to_owned();
    //~^ assigning_clones
}

fn owned_method_val(mut mut_string: String, ref_str: &str) {
    mut_string = ref_str.to_owned();
    //~^ assigning_clones
}

struct HasDeref {
    a: String,
}

impl Deref for HasDeref {
    type Target = String;
    fn deref(&self) -> &Self::Target {
        &self.a
    }
}

impl DerefMut for HasDeref {
    fn deref_mut(&mut self) -> &mut Self::Target {
        &mut self.a
    }
}

fn owned_method_box(mut_box_string: &mut Box<String>, ref_str: &str) {
    **mut_box_string = ref_str.to_owned();
    //~^ assigning_clones
}

fn owned_method_deref(mut_box_string: &mut HasDeref, ref_str: &str) {
    **mut_box_string = ref_str.to_owned();
    //~^ assigning_clones
}

fn owned_function_mut_ref(mut_thing: &mut String, ref_str: &str) {
    *mut_thing = ToOwned::to_owned(ref_str);
    //~^ assigning_clones
}

fn owned_function_val(mut mut_thing: String, ref_str: &str) {
    mut_thing = ToOwned::to_owned(ref_str);
    //~^ assigning_clones
}

fn owned_method_macro() {
    let mut s = String::from("");
    s = format!("{} {}", "hello", "world").to_owned();
    //~^ assigning_clones
}

fn owned_function_macro() {
    let mut s = String::from("");
    s = ToOwned::to_owned(&format!("{} {}", "hello", "world"));
    //~^ assigning_clones
}

struct FakeToOwned;
impl FakeToOwned {
    /// This looks just like `ToOwned::to_owned`
    fn to_owned(&self) -> Self {
        FakeToOwned
    }
}

fn fake_to_owned() {
    let mut a = FakeToOwned;
    let b = FakeToOwned;
    // Should not be linted, since the ToOwned impl doesn't come from std
    a = b.to_owned();
}

fn main() {}

/// Trait implementation to allow producing a `Thing` with a low-precedence expression.
impl Add for HasCloneFrom {
    type Output = Self;
    fn add(self, _: HasCloneFrom) -> Self {
        self
    }
}
/// Trait implementation to allow producing a `&Thing` with a low-precedence expression.
impl<'a> Add for &'a HasCloneFrom {
    type Output = Self;
    fn add(self, _: &'a HasCloneFrom) -> Self {
        self
    }
}
/// Trait implementation to allow producing a `&mut Thing` with a low-precedence expression.
impl<'a> Add for &'a mut HasCloneFrom {
    type Output = Self;
    fn add(self, _: &'a mut HasCloneFrom) -> Self {
        self
    }
}

mod borrowck_conflicts {
    //! Cases where clone_into and friends cannot be used because the src/dest have conflicting
    //! borrows.
    use std::path::PathBuf;

    fn issue12444(mut name: String) {
        let parts = name.split(", ").collect::<Vec<_>>();
        let first = *parts.first().unwrap();
        name = first.to_owned();
    }

    fn issue12444_simple() {
        let mut s = String::new();
        let s2 = &s;
        s = s2.to_owned();
    }

    fn issue12444_nodrop_projections() {
        struct NoDrop;

        impl Clone for NoDrop {
            fn clone(&self) -> Self {
                todo!()
            }
            fn clone_from(&mut self, other: &Self) {
                todo!()
            }
        }

        let mut s = NoDrop;
        let s2 = &s;
        s = s2.clone();

        let mut s = (NoDrop, NoDrop);
        let s2 = &s.0;
        s.0 = s2.clone();

        // This *could* emit a warning, but PossibleBorrowerMap only works with locals so it
        // considers `s` fully borrowed
        let mut s = (NoDrop, NoDrop);
        let s2 = &s.1;
        s.0 = s2.clone();
    }

    fn issue12460(mut name: String) {
        if let Some(stripped_name) = name.strip_prefix("baz-") {
            name = stripped_name.to_owned();
        }
    }

    fn issue12749() {
        let mut path = PathBuf::from("/a/b/c");
        path = path.components().as_path().to_owned();
    }
}

struct DerefWrapper<T>(T);

impl<T> Deref for DerefWrapper<T> {
    type Target = T;

    fn deref(&self) -> &Self::Target {
        &self.0
    }
}

impl<T> DerefMut for DerefWrapper<T> {
    fn deref_mut(&mut self) -> &mut Self::Target {
        &mut self.0
    }
}

struct DerefWrapperWithClone<T>(T);

impl<T> Deref for DerefWrapperWithClone<T> {
    type Target = T;

    fn deref(&self) -> &Self::Target {
        &self.0
    }
}

impl<T> DerefMut for DerefWrapperWithClone<T> {
    fn deref_mut(&mut self) -> &mut Self::Target {
        &mut self.0
    }
}

impl<T: Clone> Clone for DerefWrapperWithClone<T> {
    fn clone(&self) -> Self {
        Self(self.0.clone())
    }

    fn clone_from(&mut self, source: &Self) {
        *self = Self(source.0.clone());
    }
}

#[cfg(test)]
mod test {
    #[derive(Default)]
    struct Data {
        field: String,
    }

    fn test_data() -> Data {
        Data {
            field: "default_value".to_string(),
        }
    }

    #[test]
    fn test() {
        let mut data = test_data();
        data.field = "override_value".to_owned();
    }
}