Skip to content

Commit f6cb96e

Browse files
committed
Make exhaustive_enums only warn on exported items
1 parent dc93188 commit f6cb96e

File tree

4 files changed

+44
-7
lines changed

4 files changed

+44
-7
lines changed

clippy_lints/src/exhaustive_enums.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use rustc_session::{declare_lint_pass, declare_tool_lint};
77
use rustc_span::sym;
88

99
declare_clippy_lint! {
10-
/// **What it does:** Warns on any `enum`s that are not tagged `#[non_exhaustive]`
10+
/// **What it does:** Warns on any exported `enum`s that are not tagged `#[non_exhaustive]`
1111
///
1212
/// **Why is this bad?** Exhaustive enums are typically fine, but a project which does
1313
/// not wish to make a stability commitment around enums may wish to disable them by default.
@@ -40,6 +40,7 @@ impl LateLintPass<'_> for ExhaustiveEnums {
4040
fn check_item(&mut self, cx: &LateContext<'_>, item: &Item<'_>) {
4141
if_chain! {
4242
if let ItemKind::Enum(..) = item.kind;
43+
if cx.access_levels.is_exported(item.hir_id);
4344
if !item.attrs.iter().any(|a| a.has_name(sym::non_exhaustive));
4445
then {
4546
if let Some(snippet) = snippet_opt(cx, item.span) {

tests/ui/exhaustive_enums.fixed

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,33 @@ fn main() {
88
}
99

1010
#[non_exhaustive]
11-
enum Exhaustive {
11+
pub enum Exhaustive {
1212
Foo,
1313
Bar,
1414
Baz,
1515
Quux(String),
1616
}
1717

18+
// no warning, already non_exhaustive
1819
#[non_exhaustive]
19-
enum NonExhaustive {
20+
pub enum NonExhaustive {
21+
Foo,
22+
Bar,
23+
Baz,
24+
Quux(String),
25+
}
26+
27+
// no warning, private
28+
enum ExhaustivePrivate {
29+
Foo,
30+
Bar,
31+
Baz,
32+
Quux(String),
33+
}
34+
35+
// no warning, private
36+
#[non_exhaustive]
37+
enum NonExhaustivePrivate {
2038
Foo,
2139
Bar,
2240
Baz,

tests/ui/exhaustive_enums.rs

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,33 @@ fn main() {
77
// nop
88
}
99

10-
enum Exhaustive {
10+
pub enum Exhaustive {
1111
Foo,
1212
Bar,
1313
Baz,
1414
Quux(String),
1515
}
1616

17+
// no warning, already non_exhaustive
1718
#[non_exhaustive]
18-
enum NonExhaustive {
19+
pub enum NonExhaustive {
20+
Foo,
21+
Bar,
22+
Baz,
23+
Quux(String),
24+
}
25+
26+
// no warning, private
27+
enum ExhaustivePrivate {
28+
Foo,
29+
Bar,
30+
Baz,
31+
Quux(String),
32+
}
33+
34+
// no warning, private
35+
#[non_exhaustive]
36+
enum NonExhaustivePrivate {
1937
Foo,
2038
Bar,
2139
Baz,

tests/ui/exhaustive_enums.stderr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
error: enums should not be exhaustive
22
--> $DIR/exhaustive_enums.rs:10:1
33
|
4-
LL | / enum Exhaustive {
4+
LL | / pub enum Exhaustive {
55
LL | | Foo,
66
LL | | Bar,
77
LL | | Baz,
@@ -17,7 +17,7 @@ LL | #![deny(clippy::exhaustive_enums)]
1717
help: try adding #[non_exhaustive]
1818
|
1919
LL | #[non_exhaustive]
20-
LL | enum Exhaustive {
20+
LL | pub enum Exhaustive {
2121
LL | Foo,
2222
LL | Bar,
2323
LL | Baz,

0 commit comments

Comments
 (0)