summaryrefslogtreecommitdiffstats
path: root/src/tools/clippy/tests/ui/struct_fields.rs
blob: 8b1a1446e3c85d96c5c5a96bd096fc9abd13b614 (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
//@aux-build:proc_macros.rs

#![warn(clippy::struct_field_names)]
#![allow(unused)]

#[macro_use]
extern crate proc_macros;

struct Data1 {
    field_data1: u8,
    //~^ ERROR: field name ends with the struct's name
    another: u8,
    foo: u8,
    bar: u8,
}

struct Data2 {
    another: u8,
    foo: u8,
    data2_field: u8,
    //~^ ERROR: field name starts with the struct's name
    bar: u8,
}

struct StructData {
    //~^ ERROR: all fields have the same postfix: `data`
    movable_data: u8,
    fixed_data: u8,
    invisible_data: u8,
}

struct DataStruct {
    //~^ ERROR: all fields have the same prefix: `data`
    data_movable: u8,
    data_fixed: u8,
    data_invisible: u8,
}

struct DoublePrefix {
    //~^ ERROR: all fields have the same prefix: `some_data`
    some_data_a: bool,
    some_data_b: bool,
    some_data_c: bool,
}

struct DoublePostfix {
    //~^ ERROR: all fields have the same postfix: `some_data`
    a_some_data: bool,
    b_some_data: bool,
    c_some_data: bool,
}

#[allow(non_snake_case)]
struct NotSnakeCase {
    //~^ ERROR: all fields have the same postfix: `someData`
    a_someData: bool,
    b_someData: bool,
    c_someData: bool,
}
#[allow(non_snake_case)]
struct NotSnakeCase2 {
    //~^ ERROR: all fields have the same prefix: `someData`
    someData_c: bool,
    someData_b: bool,
    someData_a_b: bool,
}

// no error, threshold is 3 fiels by default
struct Fooo {
    foo: u8,
    bar: u8,
}

struct NonCaps {
    //~^ ERROR: all fields have the same prefix: `prefix`
    prefix_的: u8,
    prefix_tea: u8,
    prefix_cake: u8,
}

// should not lint
#[allow(clippy::struct_field_names)]
pub mod allowed {
    pub struct PubAllowed {
        some_this: u8,
        some_that: u8,
        some_other_what: u8,
    }
}

// should not lint
struct SomeData {
    foo: u8,
    bar: bool,
    path: u8,
    answer: u8,
}

// should not lint
pub struct NetworkLayer {
    layer1: Vec<u8>,
    layer2: Vec<u8>,
    layer3: Vec<u8>,
    layer4: Vec<u8>,
}

//should not lint
struct North {
    normal: u8,
    no_left: u8,
    no_right: u8,
}

mod issue8324_from_enum_variant_names {
    // 8324: enum_variant_names warns even if removing the suffix would leave an empty string
    struct Phase {
        pre_lookup: u8,
        lookup: u8,
        post_lookup: u8,
    }
}

mod issue9018_from_enum_variant_names {
    struct DoLint {
        //~^ ERROR: all fields have the same prefix: `_type`
        _type_create: u8,
        _type_read: u8,
        _type_update: u8,
        _type_destroy: u8,
    }

    struct DoLint2 {
        //~^ ERROR: all fields have the same prefix: `__type`
        __type_create: u8,
        __type_read: u8,
        __type_update: u8,
        __type_destroy: u8,
    }

    struct DoLint3 {
        //~^ ERROR: all fields have the same prefix: `___type`
        ___type_create: u8,
        ___type_read: u8,
        ___type_update: u8,
        ___type_destroy: u8,
    }

    struct DoLint4 {
        //~^ ERROR: all fields have the same postfix: `_`
        create_: u8,
        read_: u8,
        update_: u8,
        destroy_: u8,
    }

    struct DoLint5 {
        //~^ ERROR: all fields have the same postfix: `__`
        create__: u8,
        read__: u8,
        update__: u8,
        destroy__: u8,
    }

    struct DoLint6 {
        //~^ ERROR: all fields have the same postfix: `___`
        create___: u8,
        read___: u8,
        update___: u8,
        destroy___: u8,
    }

    struct DoLintToo {
        //~^ ERROR: all fields have the same postfix: `type`
        _create_type: u8,
        _update_type: u8,
        _delete_type: u8,
    }

    struct DoNotLint {
        _foo: u8,
        _bar: u8,
        _baz: u8,
    }

    struct DoNotLint2 {
        __foo: u8,
        __bar: u8,
        __baz: u8,
    }
}

mod allow_attributes_on_fields {
    struct Struct {
        #[allow(clippy::struct_field_names)]
        struct_starts_with: u8,
        #[allow(clippy::struct_field_names)]
        ends_with_struct: u8,
        foo: u8,
    }
}

// food field should not lint
struct Foo {
    food: i32,
    a: i32,
    b: i32,
}

struct Proxy {
    proxy: i32,
    //~^ ERROR: field name starts with the struct's name
    unrelated1: bool,
    unrelated2: bool,
}

// should not lint
pub struct RegexT {
    __buffer: i32,
    __allocated: i32,
    __used: i32,
}

mod macro_tests {
    macro_rules! mk_struct {
        () => {
            struct MacroStruct {
                some_a: i32,
                some_b: i32,
                some_c: i32,
            }
        };
    }
    mk_struct!();
    //~^ ERROR: all fields have the same prefix: `some`

    macro_rules! mk_struct2 {
        () => {
            struct Macrobaz {
                macrobaz_a: i32,
                some_b: i32,
                some_c: i32,
            }
        };
    }
    mk_struct2!();
    //~^ ERROR: field name starts with the struct's name

    macro_rules! mk_struct_with_names {
        ($struct_name:ident, $field:ident) => {
            struct $struct_name {
                $field: i32,
                other_something: i32,
                other_field: i32,
            }
        };
    }
    // expands to `struct Foo { foo: i32, ... }`
    mk_struct_with_names!(Foo, foo);
    //~^ ERROR: field name starts with the struct's name

    // expands to a struct with all fields starting with `other` but should not
    // be linted because some fields come from the macro definition and the other from the input
    mk_struct_with_names!(Some, other_data);

    // should not lint when names come from different places
    macro_rules! mk_struct_with_field_name {
        ($field_name:ident) => {
            struct Baz {
                one: i32,
                two: i32,
                $field_name: i32,
            }
        };
    }
    mk_struct_with_field_name!(baz_three);

    // should not lint when names come from different places
    macro_rules! mk_struct_with_field_name {
        ($field_name:ident) => {
            struct Bazilisk {
                baz_one: i32,
                baz_two: i32,
                $field_name: i32,
            }
        };
    }
    mk_struct_with_field_name!(baz_three);

    macro_rules! mk_struct_full_def {
        ($struct_name:ident, $field1:ident, $field2:ident, $field3:ident) => {
            struct $struct_name {
                $field1: i32,
                $field2: i32,
                $field3: i32,
            }
        };
    }
    mk_struct_full_def!(PrefixData, some_data, some_meta, some_other);
    //~^ ERROR: all fields have the same prefix: `some`
}

// should not lint on external code
external! {
    struct DataExternal {
        field_data1: u8,
        another: u8,
        foo: u8,
        bar: u8,
    }

    struct NotSnakeCaseExternal {
        someData_c: bool,
        someData_b: bool,
        someData_a_b: bool,
    }

    struct DoublePrefixExternal {
        some_data_a: bool,
        some_data_b: bool,
        some_data_c: bool,
    }

    struct StructDataExternal {
        movable_data: u8,
        fixed_data: u8,
        invisible_data: u8,
    }

}

fn main() {}