summaryrefslogtreecommitdiffstats
path: root/src/tools/clippy/tests/ui/inconsistent_struct_constructor.rs
blob: ba96e1e330f5fc62f3e408036a3897d2d6d28a24 (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
// run-rustfix
#![warn(clippy::inconsistent_struct_constructor)]
#![allow(clippy::redundant_field_names)]
#![allow(clippy::unnecessary_operation)]
#![allow(clippy::no_effect)]
#![allow(dead_code)]

#[derive(Default)]
struct Foo {
    x: i32,
    y: i32,
    z: i32,
}

macro_rules! new_foo {
    () => {
        let x = 1;
        let y = 1;
        let z = 1;
        Foo { y, x, z }
    };
}

mod without_base {
    use super::Foo;

    fn test() {
        let x = 1;
        let y = 1;
        let z = 1;

        // Should lint.
        Foo { y, x, z };

        // Should NOT lint.
        // issue #7069.
        new_foo!();

        // Should NOT lint because the order is the same as in the definition.
        Foo { x, y, z };

        // Should NOT lint because z is not a shorthand init.
        Foo { y, x, z: z };
    }
}

mod with_base {
    use super::Foo;

    fn test() {
        let x = 1;
        let z = 1;

        // Should lint.
        Foo {
            z,
            x,
            ..Default::default()
        };

        // Should NOT lint because the order is consistent with the definition.
        Foo {
            x,
            z,
            ..Default::default()
        };

        // Should NOT lint because z is not a shorthand init.
        Foo {
            z: z,
            x,
            ..Default::default()
        };
    }
}

fn main() {}