Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit 8d82ac5

Browse files
committed
Auto merge of rust-lang#6661 - Manishearth:exhaustive-fix, r=flip1995
exhaustive_structs: don't trigger for structs with private fields changelog: Restrict `exhaustive_structs` to structs with all-public fields
2 parents c5f3f9d + 4a13c8c commit 8d82ac5

File tree

4 files changed

+41
-19
lines changed

4 files changed

+41
-19
lines changed

clippy_lints/src/exhaustive_items.rs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -75,10 +75,14 @@ impl LateLintPass<'_> for ExhaustiveItems {
7575
if cx.access_levels.is_exported(item.hir_id);
7676
if !item.attrs.iter().any(|a| a.has_name(sym::non_exhaustive));
7777
then {
78-
let (lint, msg) = if let ItemKind::Enum(..) = item.kind {
79-
(EXHAUSTIVE_ENUMS, "exported enums should not be exhaustive")
80-
} else {
78+
let (lint, msg) = if let ItemKind::Struct(ref v, ..) = item.kind {
79+
if v.fields().iter().any(|f| !f.vis.node.is_pub()) {
80+
// skip structs with private fields
81+
return;
82+
}
8183
(EXHAUSTIVE_STRUCTS, "exported structs should not be exhaustive")
84+
} else {
85+
(EXHAUSTIVE_ENUMS, "exported enums should not be exhaustive")
8286
};
8387
let suggestion_span = item.span.shrink_to_lo();
8488
let indent = " ".repeat(indent_of(cx, item.span).unwrap_or(0));

tests/ui/exhaustive_items.fixed

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -56,27 +56,36 @@ pub mod enums {
5656
pub mod structs {
5757
#[non_exhaustive]
5858
pub struct Exhaustive {
59-
foo: u8,
60-
bar: String,
59+
pub foo: u8,
60+
pub bar: String,
6161
}
6262

6363
// no warning, already non_exhaustive
6464
#[non_exhaustive]
6565
pub struct NonExhaustive {
66-
foo: u8,
66+
pub foo: u8,
67+
pub bar: String,
68+
}
69+
70+
// no warning, private fields
71+
pub struct ExhaustivePrivateFieldTuple(u8);
72+
73+
// no warning, private fields
74+
pub struct ExhaustivePrivateField {
75+
pub foo: u8,
6776
bar: String,
6877
}
6978

7079
// no warning, private
7180
struct ExhaustivePrivate {
72-
foo: u8,
73-
bar: String,
81+
pub foo: u8,
82+
pub bar: String,
7483
}
7584

7685
// no warning, private
7786
#[non_exhaustive]
7887
struct NonExhaustivePrivate {
79-
foo: u8,
80-
bar: String,
88+
pub foo: u8,
89+
pub bar: String,
8190
}
8291
}

tests/ui/exhaustive_items.rs

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -53,27 +53,36 @@ pub mod enums {
5353

5454
pub mod structs {
5555
pub struct Exhaustive {
56-
foo: u8,
57-
bar: String,
56+
pub foo: u8,
57+
pub bar: String,
5858
}
5959

6060
// no warning, already non_exhaustive
6161
#[non_exhaustive]
6262
pub struct NonExhaustive {
63-
foo: u8,
63+
pub foo: u8,
64+
pub bar: String,
65+
}
66+
67+
// no warning, private fields
68+
pub struct ExhaustivePrivateFieldTuple(u8);
69+
70+
// no warning, private fields
71+
pub struct ExhaustivePrivateField {
72+
pub foo: u8,
6473
bar: String,
6574
}
6675

6776
// no warning, private
6877
struct ExhaustivePrivate {
69-
foo: u8,
70-
bar: String,
78+
pub foo: u8,
79+
pub bar: String,
7180
}
7281

7382
// no warning, private
7483
#[non_exhaustive]
7584
struct NonExhaustivePrivate {
76-
foo: u8,
77-
bar: String,
85+
pub foo: u8,
86+
pub bar: String,
7887
}
7988
}

tests/ui/exhaustive_items.stderr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,8 @@ error: exported structs should not be exhaustive
4141
--> $DIR/exhaustive_items.rs:55:5
4242
|
4343
LL | / pub struct Exhaustive {
44-
LL | | foo: u8,
45-
LL | | bar: String,
44+
LL | | pub foo: u8,
45+
LL | | pub bar: String,
4646
LL | | }
4747
| |_____^
4848
|

0 commit comments

Comments
 (0)