From 0fb09d6b218b005b90fe5b3f3d5346a2b707e19e Mon Sep 17 00:00:00 2001 From: Manish Goregaokar Date: Mon, 1 Feb 2021 18:38:43 -0800 Subject: [PATCH 1/2] exhaustive_structs: don't trigger for structs with private fields --- clippy_lints/src/exhaustive_items.rs | 10 +++++++--- tests/ui/exhaustive_items.fixed | 25 +++++++++++++++++-------- tests/ui/exhaustive_items.rs | 23 ++++++++++++++++------- tests/ui/exhaustive_items.stderr | 4 ++-- 4 files changed, 42 insertions(+), 20 deletions(-) diff --git a/clippy_lints/src/exhaustive_items.rs b/clippy_lints/src/exhaustive_items.rs index 32b1299efce..e3988d0038c 100644 --- a/clippy_lints/src/exhaustive_items.rs +++ b/clippy_lints/src/exhaustive_items.rs @@ -75,10 +75,14 @@ impl LateLintPass<'_> for ExhaustiveItems { if cx.access_levels.is_exported(item.hir_id); if !item.attrs.iter().any(|a| a.has_name(sym::non_exhaustive)); then { - let (lint, msg) = if let ItemKind::Enum(..) = item.kind { - (EXHAUSTIVE_ENUMS, "exported enums should not be exhaustive") - } else { + let (lint, msg) = if let ItemKind::Struct(ref v, ..) = item.kind { + if v.fields().iter().any(|f| !f.vis.node.is_pub()) { + // skip structs with private fields + return; + } (EXHAUSTIVE_STRUCTS, "exported structs should not be exhaustive") + } else { + (EXHAUSTIVE_ENUMS, "exported enums should not be exhaustive") }; let suggestion_span = item.span.shrink_to_lo(); let indent = " ".repeat(indent_of(cx, item.span).unwrap_or(0)); diff --git a/tests/ui/exhaustive_items.fixed b/tests/ui/exhaustive_items.fixed index 8174a0175ab..383b3d85a66 100644 --- a/tests/ui/exhaustive_items.fixed +++ b/tests/ui/exhaustive_items.fixed @@ -56,27 +56,36 @@ pub mod enums { pub mod structs { #[non_exhaustive] pub struct Exhaustive { - foo: u8, - bar: String, + pub foo: u8, + pub bar: String, } // no warning, already non_exhaustive #[non_exhaustive] pub struct NonExhaustive { - foo: u8, - bar: String, + pub foo: u8, + pub bar: String, + } + + // no warning, private fields + pub struct ExhaustivePrivateFieldTuple(u8); + + // no warning, private fields + pub struct ExhaustivePrivateField { + pub foo: u8, + bar: String } // no warning, private struct ExhaustivePrivate { - foo: u8, - bar: String, + pub foo: u8, + pub bar: String, } // no warning, private #[non_exhaustive] struct NonExhaustivePrivate { - foo: u8, - bar: String, + pub foo: u8, + pub bar: String, } } diff --git a/tests/ui/exhaustive_items.rs b/tests/ui/exhaustive_items.rs index b476f09f8a0..6f59dbf2da5 100644 --- a/tests/ui/exhaustive_items.rs +++ b/tests/ui/exhaustive_items.rs @@ -53,27 +53,36 @@ pub mod enums { pub mod structs { pub struct Exhaustive { - foo: u8, - bar: String, + pub foo: u8, + pub bar: String, } // no warning, already non_exhaustive #[non_exhaustive] pub struct NonExhaustive { - foo: u8, + pub foo: u8, + pub bar: String, + } + + // no warning, private fields + pub struct ExhaustivePrivateFieldTuple(u8); + + // no warning, private fields + pub struct ExhaustivePrivateField { + pub foo: u8, bar: String, } // no warning, private struct ExhaustivePrivate { - foo: u8, - bar: String, + pub foo: u8, + pub bar: String, } // no warning, private #[non_exhaustive] struct NonExhaustivePrivate { - foo: u8, - bar: String, + pub foo: u8, + pub bar: String, } } diff --git a/tests/ui/exhaustive_items.stderr b/tests/ui/exhaustive_items.stderr index 7369fe75a4f..8fbab535a9b 100644 --- a/tests/ui/exhaustive_items.stderr +++ b/tests/ui/exhaustive_items.stderr @@ -41,8 +41,8 @@ error: exported structs should not be exhaustive --> $DIR/exhaustive_items.rs:55:5 | LL | / pub struct Exhaustive { -LL | | foo: u8, -LL | | bar: String, +LL | | pub foo: u8, +LL | | pub bar: String, LL | | } | |_____^ | From 4a13c8c22e17e74e5ae4ece07ae37ad32903dcae Mon Sep 17 00:00:00 2001 From: Philipp Krones Date: Tue, 2 Feb 2021 08:59:23 +0100 Subject: [PATCH 2/2] Fix test formatting --- tests/ui/exhaustive_items.fixed | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/ui/exhaustive_items.fixed b/tests/ui/exhaustive_items.fixed index 383b3d85a66..c209f5b4b72 100644 --- a/tests/ui/exhaustive_items.fixed +++ b/tests/ui/exhaustive_items.fixed @@ -73,7 +73,7 @@ pub mod structs { // no warning, private fields pub struct ExhaustivePrivateField { pub foo: u8, - bar: String + bar: String, } // no warning, private