about summary refs log tree commit diff
diff options
context:
space:
mode:
authorManish Goregaokar <manishsmail@gmail.com>2021-02-01 18:38:43 -0800
committerManish Goregaokar <manishsmail@gmail.com>2021-02-01 18:42:08 -0800
commit0fb09d6b218b005b90fe5b3f3d5346a2b707e19e (patch)
treeda977cb5a125675b261d2e9ae0160f4d1b8a4aa7
parentc5f3f9df3bb1d89ed8cd624c595af90396899360 (diff)
downloadrust-0fb09d6b218b005b90fe5b3f3d5346a2b707e19e.tar.gz
rust-0fb09d6b218b005b90fe5b3f3d5346a2b707e19e.zip
exhaustive_structs: don't trigger for structs with private fields
-rw-r--r--clippy_lints/src/exhaustive_items.rs10
-rw-r--r--tests/ui/exhaustive_items.fixed25
-rw-r--r--tests/ui/exhaustive_items.rs23
-rw-r--r--tests/ui/exhaustive_items.stderr4
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 | |     }
    | |_____^
    |