summaryrefslogtreecommitdiffstats
path: root/src/tools/clippy/tests/ui/misnamed_getters.fixed
blob: 70af604b2144804d91083edc7f4de7bc46c47d95 (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
#![allow(unused)]
#![allow(clippy::struct_field_names)]
#![warn(clippy::misnamed_getters)]

struct A {
    a: u8,
    b: u8,
    c: u8,
}

impl A {
    fn a(&self) -> &u8 {
        //~^ ERROR: getter function appears to return the wrong field
        //~| NOTE: `-D clippy::misnamed-getters` implied by `-D warnings`
        &self.a
    }
    fn a_mut(&mut self) -> &mut u8 {
        //~^ ERROR: getter function appears to return the wrong field
        &mut self.a
    }

    fn b(self) -> u8 {
        //~^ ERROR: getter function appears to return the wrong field
        self.b
    }

    fn b_mut(&mut self) -> &mut u8 {
        //~^ ERROR: getter function appears to return the wrong field
        &mut self.b
    }

    fn c(&self) -> &u8 {
        //~^ ERROR: getter function appears to return the wrong field
        &self.c
    }

    fn c_mut(&mut self) -> &mut u8 {
        //~^ ERROR: getter function appears to return the wrong field
        &mut self.c
    }
}

union B {
    a: u8,
    b: u8,
}

impl B {
    unsafe fn a(&self) -> &u8 {
        //~^ ERROR: getter function appears to return the wrong field
        &self.a
    }
    unsafe fn a_mut(&mut self) -> &mut u8 {
        //~^ ERROR: getter function appears to return the wrong field
        &mut self.a
    }

    unsafe fn b(self) -> u8 {
        //~^ ERROR: getter function appears to return the wrong field
        self.b
    }

    unsafe fn b_mut(&mut self) -> &mut u8 {
        //~^ ERROR: getter function appears to return the wrong field
        &mut self.b
    }

    unsafe fn c(&self) -> &u8 {
        &self.b
    }

    unsafe fn c_mut(&mut self) -> &mut u8 {
        &mut self.a
    }

    unsafe fn a_unchecked(&self) -> &u8 {
        //~^ ERROR: getter function appears to return the wrong field
        &self.a
    }
    unsafe fn a_unchecked_mut(&mut self) -> &mut u8 {
        //~^ ERROR: getter function appears to return the wrong field
        &mut self.a
    }

    unsafe fn b_unchecked(self) -> u8 {
        //~^ ERROR: getter function appears to return the wrong field
        self.b
    }

    unsafe fn b_unchecked_mut(&mut self) -> &mut u8 {
        //~^ ERROR: getter function appears to return the wrong field
        &mut self.b
    }

    unsafe fn c_unchecked(&self) -> &u8 {
        &self.b
    }

    unsafe fn c_unchecked_mut(&mut self) -> &mut u8 {
        &mut self.a
    }
}

struct D {
    d: u8,
    inner: A,
}

impl core::ops::Deref for D {
    type Target = A;
    fn deref(&self) -> &A {
        &self.inner
    }
}

impl core::ops::DerefMut for D {
    fn deref_mut(&mut self) -> &mut A {
        &mut self.inner
    }
}

impl D {
    fn a(&self) -> &u8 {
        //~^ ERROR: getter function appears to return the wrong field
        &self.a
    }
    fn a_mut(&mut self) -> &mut u8 {
        //~^ ERROR: getter function appears to return the wrong field
        &mut self.a
    }

    fn d(&self) -> &u8 {
        //~^ ERROR: getter function appears to return the wrong field
        &self.d
    }
    fn d_mut(&mut self) -> &mut u8 {
        //~^ ERROR: getter function appears to return the wrong field
        &mut self.d
    }
}

fn main() {
    // test code goes here
}