about summary refs log tree commit diff
path: root/src/test/ui/lint/dead-code/write-only-field.rs
blob: 7b3f1e9f5b6cb89e4f88042e1c43df9a48a3a7b9 (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
#![deny(dead_code)]

struct S {
    f: i32, //~ ERROR: field is never read
    sub: Sub, //~ ERROR: field is never read
}

struct Sub {
    f: i32, //~ ERROR: field is never read
}

fn field_write(s: &mut S) {
    s.f = 1;
    s.sub.f = 2;
}

fn main() {
    let mut s = S { f: 0, sub: Sub { f: 0 } };
    field_write(&mut s);

    auto_deref();
    nested_boxes();
}

fn auto_deref() {
    struct E {
        x: bool,
        y: bool, //~ ERROR: field is never read
    }

    struct P<'a> {
        e: &'a mut E
    }

    impl P<'_> {
        fn f(&mut self) {
            self.e.x = true;
            self.e.y = true;
        }
    }

    let mut e = E { x: false, y: false };
    let mut p = P { e: &mut e };
    p.f();
    assert!(e.x);
}

fn nested_boxes() {
    struct A {
        b: Box<B>,
    }

    struct B {
        c: Box<C>,
    }

    struct C {
        u: u32, //~ ERROR: field is never read
        v: u32, //~ ERROR: field is never read
    }

    let mut a = A {
        b: Box::new(B {
            c: Box::new(C { u: 0, v: 0 }),
        }),
    };
    a.b.c.v = 10;
    a.b.c = Box::new(C { u: 1, v: 2 });
}