Skip to content

Commit 7bb0e9c

Browse files
committed
Auto merge of rust-lang#12099 - GuillaumeGomez:struct-field-names-bool, r=llogiq
Don't emit `struct_field_names` lint if all fields are booleans and don't start with the type's name Fixes rust-lang#11936. I only checked that all fields are booleans and not the prefix (nor the suffix) because when I started to list accepted prefixes (like "is", "has", "should", "could", etc), the list was starting to get a bit too long and I thought it was not really worth for such a small change. r? `@llogiq` changelog: Don't emit `struct_field_names` lint if all fields are booleans and don't start with the type's name
2 parents 394f63f + 7aa4624 commit 7bb0e9c

File tree

3 files changed

+46
-9
lines changed

3 files changed

+46
-9
lines changed

clippy_lints/src/item_name_repetitions.rs

+5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
//! lint on enum variants that are prefixed or suffixed by the same characters
22
33
use clippy_utils::diagnostics::{span_lint, span_lint_and_help, span_lint_hir};
4+
use clippy_utils::is_bool;
45
use clippy_utils::macros::span_is_local;
56
use clippy_utils::source::is_present_in_source;
67
use clippy_utils::str_utils::{camel_case_split, count_match_end, count_match_start, to_camel_case, to_snake_case};
@@ -231,6 +232,10 @@ fn check_fields(cx: &LateContext<'_>, threshold: u64, item: &Item<'_>, fields: &
231232
(false, _) => ("pre", prefix),
232233
(true, false) => ("post", postfix),
233234
};
235+
if fields.iter().all(|field| is_bool(field.ty)) {
236+
// If all fields are booleans, we don't want to emit this lint.
237+
return;
238+
}
234239
span_lint_and_help(
235240
cx,
236241
STRUCT_FIELD_NAMES,

tests/ui/struct_fields.rs

+18-4
Original file line numberDiff line numberDiff line change
@@ -39,29 +39,29 @@ struct DataStruct {
3939
struct DoublePrefix {
4040
//~^ ERROR: all fields have the same prefix: `some_data`
4141
some_data_a: bool,
42-
some_data_b: bool,
42+
some_data_b: i8,
4343
some_data_c: bool,
4444
}
4545

4646
struct DoublePostfix {
4747
//~^ ERROR: all fields have the same postfix: `some_data`
4848
a_some_data: bool,
49-
b_some_data: bool,
49+
b_some_data: i8,
5050
c_some_data: bool,
5151
}
5252

5353
#[allow(non_snake_case)]
5454
struct NotSnakeCase {
5555
//~^ ERROR: all fields have the same postfix: `someData`
5656
a_someData: bool,
57-
b_someData: bool,
57+
b_someData: i8,
5858
c_someData: bool,
5959
}
6060
#[allow(non_snake_case)]
6161
struct NotSnakeCase2 {
6262
//~^ ERROR: all fields have the same prefix: `someData`
6363
someData_c: bool,
64-
someData_b: bool,
64+
someData_b: i8,
6565
someData_a_b: bool,
6666
}
6767

@@ -328,4 +328,18 @@ external! {
328328

329329
}
330330

331+
// Should not warn
332+
struct Config {
333+
use_foo: bool,
334+
use_bar: bool,
335+
use_baz: bool,
336+
}
337+
338+
struct Use {
339+
use_foo: bool,
340+
//~^ ERROR: field name starts with the struct's name
341+
use_bar: bool,
342+
use_baz: bool,
343+
}
344+
331345
fn main() {}

tests/ui/struct_fields.stderr

+23-5
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ error: all fields have the same prefix: `some_data`
4545
LL | / struct DoublePrefix {
4646
LL | |
4747
LL | | some_data_a: bool,
48-
LL | | some_data_b: bool,
48+
LL | | some_data_b: i8,
4949
LL | | some_data_c: bool,
5050
LL | | }
5151
| |_^
@@ -58,7 +58,7 @@ error: all fields have the same postfix: `some_data`
5858
LL | / struct DoublePostfix {
5959
LL | |
6060
LL | | a_some_data: bool,
61-
LL | | b_some_data: bool,
61+
LL | | b_some_data: i8,
6262
LL | | c_some_data: bool,
6363
LL | | }
6464
| |_^
@@ -71,7 +71,7 @@ error: all fields have the same postfix: `someData`
7171
LL | / struct NotSnakeCase {
7272
LL | |
7373
LL | | a_someData: bool,
74-
LL | | b_someData: bool,
74+
LL | | b_someData: i8,
7575
LL | | c_someData: bool,
7676
LL | | }
7777
| |_^
@@ -84,7 +84,7 @@ error: all fields have the same prefix: `someData`
8484
LL | / struct NotSnakeCase2 {
8585
LL | |
8686
LL | | someData_c: bool,
87-
LL | | someData_b: bool,
87+
LL | | someData_b: i8,
8888
LL | | someData_a_b: bool,
8989
LL | | }
9090
| |_^
@@ -261,5 +261,23 @@ LL | mk_struct_full_def!(PrefixData, some_data, some_meta, some_other);
261261
= help: remove the prefixes
262262
= note: this error originates in the macro `mk_struct_full_def` (in Nightly builds, run with -Z macro-backtrace for more info)
263263

264-
error: aborting due to 21 previous errors
264+
error: field name starts with the struct's name
265+
--> $DIR/struct_fields.rs:339:5
266+
|
267+
LL | use_foo: bool,
268+
| ^^^^^^^^^^^^^
269+
270+
error: field name starts with the struct's name
271+
--> $DIR/struct_fields.rs:341:5
272+
|
273+
LL | use_bar: bool,
274+
| ^^^^^^^^^^^^^
275+
276+
error: field name starts with the struct's name
277+
--> $DIR/struct_fields.rs:342:5
278+
|
279+
LL | use_baz: bool,
280+
| ^^^^^^^^^^^^^
281+
282+
error: aborting due to 24 previous errors
265283

0 commit comments

Comments
 (0)