Skip to content

Commit

Permalink
Make exhaustive_enums only warn on exported items
Browse files Browse the repository at this point in the history
  • Loading branch information
Manishearth committed Jan 21, 2021
1 parent dc93188 commit f6cb96e
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 7 deletions.
3 changes: 2 additions & 1 deletion clippy_lints/src/exhaustive_enums.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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) {
Expand Down
22 changes: 20 additions & 2 deletions tests/ui/exhaustive_enums.fixed
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
22 changes: 20 additions & 2 deletions tests/ui/exhaustive_enums.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
4 changes: 2 additions & 2 deletions tests/ui/exhaustive_enums.stderr
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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,
Expand Down

0 comments on commit f6cb96e

Please sign in to comment.