about summary refs log tree commit diff
diff options
context:
space:
mode:
authorManish Goregaokar <manishsmail@gmail.com>2021-01-21 13:34:44 -0800
committerManish Goregaokar <manishsmail@gmail.com>2021-01-21 13:34:44 -0800
commitf6cb96ef07ac6197dac5be16adbe2b7950c82d99 (patch)
treef7e4eb526b94fa190a0c14d4dd4eee0d9d1ad352
parentdc93188805ac20fbccd7bd616a6b114680e9303a (diff)
downloadrust-f6cb96ef07ac6197dac5be16adbe2b7950c82d99.tar.gz
rust-f6cb96ef07ac6197dac5be16adbe2b7950c82d99.zip
Make exhaustive_enums only warn on exported items
-rw-r--r--clippy_lints/src/exhaustive_enums.rs3
-rw-r--r--tests/ui/exhaustive_enums.fixed22
-rw-r--r--tests/ui/exhaustive_enums.rs22
-rw-r--r--tests/ui/exhaustive_enums.stderr4
4 files changed, 44 insertions, 7 deletions
diff --git a/clippy_lints/src/exhaustive_enums.rs b/clippy_lints/src/exhaustive_enums.rs
index 1391ebd9e31..2e1c0728d2c 100644
--- a/clippy_lints/src/exhaustive_enums.rs
+++ b/clippy_lints/src/exhaustive_enums.rs
@@ -7,7 +7,7 @@ use rustc_session::{declare_lint_pass, declare_tool_lint};
 use rustc_span::sym;
 
 declare_clippy_lint! {
-    /// **What it does:** Warns on any `enum`s that are not tagged `#[non_exhaustive]`
+    /// **What it does:** Warns on any exported `enum`s that are not tagged `#[non_exhaustive]`
     ///
     /// **Why is this bad?** Exhaustive enums are typically fine, but a project which does
     /// not wish to make a stability commitment around enums may wish to disable them by default.
@@ -40,6 +40,7 @@ impl LateLintPass<'_> for ExhaustiveEnums {
     fn check_item(&mut self, cx: &LateContext<'_>, item: &Item<'_>) {
         if_chain! {
             if let ItemKind::Enum(..) = item.kind;
+            if cx.access_levels.is_exported(item.hir_id);
             if !item.attrs.iter().any(|a| a.has_name(sym::non_exhaustive));
             then {
                 if let Some(snippet) = snippet_opt(cx, item.span) {
diff --git a/tests/ui/exhaustive_enums.fixed b/tests/ui/exhaustive_enums.fixed
index 2d5f0474bc0..71c4a251e3b 100644
--- a/tests/ui/exhaustive_enums.fixed
+++ b/tests/ui/exhaustive_enums.fixed
@@ -8,15 +8,33 @@ fn main() {
 }
 
 #[non_exhaustive]
-enum Exhaustive {
+pub enum Exhaustive {
     Foo,
     Bar,
     Baz,
     Quux(String),
 }
 
+// no warning, already non_exhaustive
 #[non_exhaustive]
-enum NonExhaustive {
+pub enum NonExhaustive {
+    Foo,
+    Bar,
+    Baz,
+    Quux(String),
+}
+
+// no warning, private
+enum ExhaustivePrivate {
+    Foo,
+    Bar,
+    Baz,
+    Quux(String),
+}
+
+// no warning, private
+#[non_exhaustive]
+enum NonExhaustivePrivate {
     Foo,
     Bar,
     Baz,
diff --git a/tests/ui/exhaustive_enums.rs b/tests/ui/exhaustive_enums.rs
index 5c88454ae61..45af6851dd1 100644
--- a/tests/ui/exhaustive_enums.rs
+++ b/tests/ui/exhaustive_enums.rs
@@ -7,15 +7,33 @@ fn main() {
     // nop
 }
 
-enum Exhaustive {
+pub enum Exhaustive {
     Foo,
     Bar,
     Baz,
     Quux(String),
 }
 
+// no warning, already non_exhaustive
 #[non_exhaustive]
-enum NonExhaustive {
+pub enum NonExhaustive {
+    Foo,
+    Bar,
+    Baz,
+    Quux(String),
+}
+
+// no warning, private
+enum ExhaustivePrivate {
+    Foo,
+    Bar,
+    Baz,
+    Quux(String),
+}
+
+// no warning, private
+#[non_exhaustive]
+enum NonExhaustivePrivate {
     Foo,
     Bar,
     Baz,
diff --git a/tests/ui/exhaustive_enums.stderr b/tests/ui/exhaustive_enums.stderr
index ee5a1836267..280c40b00aa 100644
--- a/tests/ui/exhaustive_enums.stderr
+++ b/tests/ui/exhaustive_enums.stderr
@@ -1,7 +1,7 @@
 error: enums should not be exhaustive
   --> $DIR/exhaustive_enums.rs:10:1
    |
-LL | / enum Exhaustive {
+LL | / pub enum Exhaustive {
 LL | |     Foo,
 LL | |     Bar,
 LL | |     Baz,
@@ -17,7 +17,7 @@ LL | #![deny(clippy::exhaustive_enums)]
 help: try adding #[non_exhaustive]
    |
 LL | #[non_exhaustive]
-LL | enum Exhaustive {
+LL | pub enum Exhaustive {
 LL |     Foo,
 LL |     Bar,
 LL |     Baz,