-
Notifications
You must be signed in to change notification settings - Fork 1.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
New lint: exhaustive_enums, exhaustive_structs #6617
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
f1ab302
New lint: exhaustive_enums
Manishearth dc93188
Make exhaustive_enums a late pass
Manishearth f6cb96e
Make exhaustive_enums only warn on exported items
Manishearth 09d4d49
ExhaustiveEnums -> ExhaustiveItems
Manishearth 8cb7e85
Add exhaustive_structs lint
Manishearth 752274e
Fix indentation of suggestion
Manishearth 65d003a
Clean up suggestion span; clarify help message
Manishearth e0ae980
Better suggestion span
Manishearth 3e3dff7
Add test with attrs
Manishearth File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,102 @@ | ||
use crate::utils::{indent_of, span_lint_and_then}; | ||
use if_chain::if_chain; | ||
use rustc_errors::Applicability; | ||
use rustc_hir::{Item, ItemKind}; | ||
use rustc_lint::{LateContext, LateLintPass}; | ||
use rustc_session::{declare_lint_pass, declare_tool_lint}; | ||
use rustc_span::sym; | ||
|
||
declare_clippy_lint! { | ||
/// **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 exported 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, | ||
"detects exported enums that have not been marked #[non_exhaustive]" | ||
} | ||
|
||
declare_clippy_lint! { | ||
/// **What it does:** Warns on any exported `structs`s that are not tagged `#[non_exhaustive]` | ||
/// | ||
/// **Why is this bad?** Exhaustive structs are typically fine, but a project which does | ||
/// not wish to make a stability commitment around exported structs may wish to | ||
/// disable them by default. | ||
/// | ||
/// **Known problems:** None. | ||
/// | ||
/// **Example:** | ||
/// | ||
/// ```rust | ||
/// struct Foo { | ||
/// bar: u8, | ||
/// baz: String, | ||
/// } | ||
/// ``` | ||
/// Use instead: | ||
/// ```rust | ||
/// #[non_exhaustive] | ||
/// struct Foo { | ||
/// bar: u8, | ||
/// baz: String, | ||
/// } | ||
/// ``` | ||
pub EXHAUSTIVE_STRUCTS, | ||
restriction, | ||
"detects exported structs that have not been marked #[non_exhaustive]" | ||
} | ||
|
||
declare_lint_pass!(ExhaustiveItems => [EXHAUSTIVE_ENUMS, EXHAUSTIVE_STRUCTS]); | ||
|
||
impl LateLintPass<'_> for ExhaustiveItems { | ||
fn check_item(&mut self, cx: &LateContext<'_>, item: &Item<'_>) { | ||
if_chain! { | ||
if let ItemKind::Enum(..) | ItemKind::Struct(..) = item.kind; | ||
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 { | ||
(EXHAUSTIVE_STRUCTS, "exported structs should not be exhaustive") | ||
}; | ||
let suggestion_span = item.span.shrink_to_lo(); | ||
let indent = " ".repeat(indent_of(cx, item.span).unwrap_or(0)); | ||
span_lint_and_then( | ||
cx, | ||
lint, | ||
item.span, | ||
msg, | ||
|diag| { | ||
let sugg = format!("#[non_exhaustive]\n{}", indent); | ||
diag.span_suggestion(suggestion_span, | ||
"try adding #[non_exhaustive]", | ||
sugg, | ||
Applicability::MaybeIncorrect); | ||
} | ||
); | ||
|
||
} | ||
} | ||
} | ||
} |
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,82 @@ | ||
// run-rustfix | ||
|
||
#![deny(clippy::exhaustive_enums, clippy::exhaustive_structs)] | ||
#![allow(unused)] | ||
|
||
fn main() { | ||
// nop | ||
} | ||
|
||
pub mod enums { | ||
#[non_exhaustive] | ||
pub enum Exhaustive { | ||
Foo, | ||
Bar, | ||
Baz, | ||
Quux(String), | ||
} | ||
|
||
/// Some docs | ||
#[repr(C)] | ||
#[non_exhaustive] | ||
pub enum ExhaustiveWithAttrs { | ||
Foo, | ||
Bar, | ||
Baz, | ||
Quux(String), | ||
} | ||
|
||
// no warning, already non_exhaustive | ||
#[non_exhaustive] | ||
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, | ||
Quux(String), | ||
} | ||
} | ||
|
||
pub mod structs { | ||
#[non_exhaustive] | ||
pub struct Exhaustive { | ||
foo: u8, | ||
bar: String, | ||
} | ||
|
||
// no warning, already non_exhaustive | ||
#[non_exhaustive] | ||
pub struct NonExhaustive { | ||
foo: u8, | ||
bar: String, | ||
} | ||
|
||
// no warning, private | ||
struct ExhaustivePrivate { | ||
foo: u8, | ||
bar: String, | ||
} | ||
|
||
// no warning, private | ||
#[non_exhaustive] | ||
struct NonExhaustivePrivate { | ||
foo: u8, | ||
bar: 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,79 @@ | ||
// run-rustfix | ||
|
||
#![deny(clippy::exhaustive_enums, clippy::exhaustive_structs)] | ||
#![allow(unused)] | ||
|
||
fn main() { | ||
// nop | ||
} | ||
|
||
pub mod enums { | ||
pub enum Exhaustive { | ||
Foo, | ||
Bar, | ||
Baz, | ||
Quux(String), | ||
} | ||
|
||
/// Some docs | ||
#[repr(C)] | ||
pub enum ExhaustiveWithAttrs { | ||
Foo, | ||
Bar, | ||
Baz, | ||
Quux(String), | ||
} | ||
|
||
// no warning, already non_exhaustive | ||
#[non_exhaustive] | ||
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, | ||
Quux(String), | ||
} | ||
} | ||
|
||
pub mod structs { | ||
pub struct Exhaustive { | ||
foo: u8, | ||
bar: String, | ||
} | ||
|
||
// no warning, already non_exhaustive | ||
#[non_exhaustive] | ||
pub struct NonExhaustive { | ||
foo: u8, | ||
bar: String, | ||
} | ||
|
||
// no warning, private | ||
struct ExhaustivePrivate { | ||
foo: u8, | ||
bar: String, | ||
} | ||
|
||
// no warning, private | ||
#[non_exhaustive] | ||
struct NonExhaustivePrivate { | ||
foo: u8, | ||
bar: 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,61 @@ | ||
error: exported enums should not be exhaustive | ||
--> $DIR/exhaustive_items.rs:11:5 | ||
| | ||
LL | / pub enum Exhaustive { | ||
LL | | Foo, | ||
LL | | Bar, | ||
LL | | Baz, | ||
LL | | Quux(String), | ||
LL | | } | ||
| |_____^ | ||
| | ||
note: the lint level is defined here | ||
--> $DIR/exhaustive_items.rs:3:9 | ||
| | ||
LL | #![deny(clippy::exhaustive_enums, clippy::exhaustive_structs)] | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^ | ||
help: try adding #[non_exhaustive] | ||
| | ||
LL | #[non_exhaustive] | ||
LL | pub enum Exhaustive { | ||
| | ||
|
||
error: exported enums should not be exhaustive | ||
--> $DIR/exhaustive_items.rs:20:5 | ||
| | ||
LL | / pub enum ExhaustiveWithAttrs { | ||
LL | | Foo, | ||
LL | | Bar, | ||
LL | | Baz, | ||
LL | | Quux(String), | ||
LL | | } | ||
| |_____^ | ||
| | ||
help: try adding #[non_exhaustive] | ||
| | ||
LL | #[non_exhaustive] | ||
LL | pub enum ExhaustiveWithAttrs { | ||
| | ||
|
||
error: exported structs should not be exhaustive | ||
--> $DIR/exhaustive_items.rs:55:5 | ||
| | ||
LL | / pub struct Exhaustive { | ||
LL | | foo: u8, | ||
LL | | bar: String, | ||
LL | | } | ||
| |_____^ | ||
| | ||
note: the lint level is defined here | ||
--> $DIR/exhaustive_items.rs:3:35 | ||
| | ||
LL | #![deny(clippy::exhaustive_enums, clippy::exhaustive_structs)] | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
help: try adding #[non_exhaustive] | ||
| | ||
LL | #[non_exhaustive] | ||
LL | pub struct Exhaustive { | ||
| | ||
|
||
error: aborting due to 3 previous errors | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you also make sure that
pub(crate)
structs and enums don't require#[non_exhaustive]
?If there's a way to exclude
pub
structs and enums that are inside a non-pub
module, that would be nice, too. But at least we should excludepub(crate)
.Basically, the check should only complain about truly public types.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, that's already how this is implemented, it's for exported types only