forked from rust-lang/rust-clippy
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
043cf97
commit f1ab302
Showing
6 changed files
with
148 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
use crate::utils::{snippet_opt, span_lint_and_help, span_lint_and_sugg}; | ||
use if_chain::if_chain; | ||
use rustc_ast::ast::{Item, ItemKind}; | ||
use rustc_errors::Applicability; | ||
use rustc_lint::{EarlyContext, EarlyLintPass}; | ||
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]` | ||
/// | ||
/// **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. | ||
/// | ||
/// **Known problems:** None. | ||
/// | ||
/// **Example:** | ||
/// | ||
/// ```rust | ||
/// enum Foo { | ||
/// Bar, | ||
/// Baz | ||
/// } | ||
/// ``` | ||
/// Use instead: | ||
/// ```rust | ||
/// #[non_exhaustive] | ||
/// enum Foo { | ||
/// Bar, | ||
/// Baz | ||
/// } /// ``` | ||
pub EXHAUSTIVE_ENUMS, | ||
restriction, | ||
"default lint description" | ||
} | ||
|
||
declare_lint_pass!(ExhaustiveEnums => [EXHAUSTIVE_ENUMS]); | ||
|
||
impl EarlyLintPass for ExhaustiveEnums { | ||
fn check_item(&mut self, cx: &EarlyContext<'_>, item: &Item) { | ||
if_chain! { | ||
if let ItemKind::Enum(..) = item.kind; | ||
if !item.attrs.iter().any(|a| a.has_name(sym::non_exhaustive)); | ||
then { | ||
if let Some(snippet) = snippet_opt(cx, item.span) { | ||
span_lint_and_sugg( | ||
cx, | ||
EXHAUSTIVE_ENUMS, | ||
item.span, | ||
"enums should not be exhaustive", | ||
"try adding #[non_exhaustive]", | ||
format!("#[non_exhaustive]\n{}", snippet), | ||
Applicability::MaybeIncorrect, | ||
); | ||
} else { | ||
span_lint_and_help( | ||
cx, | ||
EXHAUSTIVE_ENUMS, | ||
item.span, | ||
"enums should not be exhaustive", | ||
None, | ||
"try adding #[non_exhaustive]", | ||
); | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
// run-rustfix | ||
|
||
#![deny(clippy::exhaustive_enums)] | ||
#![allow(unused)] | ||
|
||
fn main() { | ||
// nop | ||
} | ||
|
||
#[non_exhaustive] | ||
enum Exhaustive { | ||
Foo, | ||
Bar, | ||
Baz, | ||
Quux(String), | ||
} | ||
|
||
#[non_exhaustive] | ||
enum NonExhaustive { | ||
Foo, | ||
Bar, | ||
Baz, | ||
Quux(String), | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
// run-rustfix | ||
|
||
#![deny(clippy::exhaustive_enums)] | ||
#![allow(unused)] | ||
|
||
fn main() { | ||
// nop | ||
} | ||
|
||
enum Exhaustive { | ||
Foo, | ||
Bar, | ||
Baz, | ||
Quux(String), | ||
} | ||
|
||
#[non_exhaustive] | ||
enum NonExhaustive { | ||
Foo, | ||
Bar, | ||
Baz, | ||
Quux(String), | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
error: enums should not be exhaustive | ||
--> $DIR/exhaustive_enums.rs:10:1 | ||
| | ||
LL | / enum Exhaustive { | ||
LL | | Foo, | ||
LL | | Bar, | ||
LL | | Baz, | ||
LL | | Quux(String), | ||
LL | | } | ||
| |_^ | ||
| | ||
note: the lint level is defined here | ||
--> $DIR/exhaustive_enums.rs:3:9 | ||
| | ||
LL | #![deny(clippy::exhaustive_enums)] | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^ | ||
help: try adding #[non_exhaustive] | ||
| | ||
LL | #[non_exhaustive] | ||
LL | enum Exhaustive { | ||
LL | Foo, | ||
LL | Bar, | ||
LL | Baz, | ||
LL | Quux(String), | ||
... | ||
|
||
error: aborting due to previous error | ||
|