fork/rust

git clone https://git.julia.blue/fork/rust

borsAuto merge of #103046 - JanBeh:PR_clarify_cmp_terminology, r=workingjubilee3bcad65fbf6

master
1.5 KiB71 linesraw
1#![allow(clippy::non_canonical_clone_impl, unused)]
2#![warn(clippy::unnecessary_struct_initialization)]
3
4struct S {
5    f: String,
6}
7
8#[derive(Clone, Copy)]
9struct T {
10    f: u32,
11}
12
13struct U {
14    f: u32,
15}
16
17impl Clone for U {
18    fn clone(&self) -> Self {
19        // Do not lint: `Self` does not implement `Copy`
20        Self { ..*self }
21    }
22}
23
24#[derive(Copy)]
25struct V {
26    f: u32,
27}
28
29impl Clone for V {
30    fn clone(&self) -> Self {
31        // Lint: `Self` implements `Copy`
32        *self
33    }
34}
35
36fn main() {
37    // Should lint: `a` would be consumed anyway
38    let a = S { f: String::from("foo") };
39    let mut b = a;
40
41    // Should lint: `b` would be consumed, and is mutable
42    let c = &mut b;
43
44    // Should not lint as `d` is not mutable
45    let d = S { f: String::from("foo") };
46    let e = &mut S { ..d };
47
48    // Should lint as `f` would be consumed anyway
49    let f = S { f: String::from("foo") };
50    let g = &f;
51
52    // Should lint: the result of an expression is mutable
53    let h = &mut *Box::new(S { f: String::from("foo") });
54
55    // Should not lint: `m` would be both alive and borrowed
56    let m = T { f: 17 };
57    let n = &T { ..m };
58
59    // Should not lint: `m` should not be modified
60    let o = &mut T { ..m };
61    o.f = 32;
62    assert_eq!(m.f, 17);
63
64    // Should not lint: `m` should not be modified
65    let o = &mut T { ..m } as *mut T;
66    unsafe { &mut *o }.f = 32;
67    assert_eq!(m.f, 17);
68
69    // Should lint: the result of an expression is mutable and temporary
70    let p = &mut *Box::new(T { f: 5 });
71}