Skip to content
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

Don't suggest doc(hidden) or unstable variants in wildcard lint #7407

Merged
merged 4 commits into from
Jul 1, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 6 additions & 5 deletions clippy_lints/src/matches.rs
Original file line number Diff line number Diff line change
Expand Up @@ -992,9 +992,9 @@ impl CommonPrefixSearcher<'a> {
}
}

fn is_doc_hidden(cx: &LateContext<'_>, variant_def: &VariantDef) -> bool {
fn is_hidden(cx: &LateContext<'_>, variant_def: &VariantDef) -> bool {
let attrs = cx.tcx.get_attrs(variant_def.def_id);
clippy_utils::attrs::is_doc_hidden(attrs)
clippy_utils::attrs::is_doc_hidden(attrs) || clippy_utils::attrs::is_unstable(attrs)
}

#[allow(clippy::too_many_lines)]
Expand Down Expand Up @@ -1033,7 +1033,8 @@ fn check_wild_enum_match(cx: &LateContext<'_>, ex: &Expr<'_>, arms: &[Arm<'_>])

// Accumulate the variants which should be put in place of the wildcard because they're not
// already covered.
let mut missing_variants: Vec<_> = adt_def.variants.iter().collect();
let has_hidden = adt_def.variants.iter().any(|x| is_hidden(cx, x));
let mut missing_variants: Vec<_> = adt_def.variants.iter().filter(|x| !is_hidden(cx, x)).collect();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think here it would be good to differentiate between the enums origin. It would make sense to me to also cover hidden/unstable variations if they originate from the same crate. (I couldn't find a check for that in the current implementation)

I'm also debating if that should be configurable, but I believe there is no real reason for that.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this has to be done in this PR. I like this idea though. Can you open a good-first-issue issue for this, please?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

cc: #7419


let mut path_prefix = CommonPrefixSearcher::None;
for arm in arms {
Expand Down Expand Up @@ -1118,7 +1119,7 @@ fn check_wild_enum_match(cx: &LateContext<'_>, ex: &Expr<'_>, arms: &[Arm<'_>])

match missing_variants.as_slice() {
[] => (),
[x] if !adt_def.is_variant_list_non_exhaustive() && !is_doc_hidden(cx, x) => span_lint_and_sugg(
[x] if !adt_def.is_variant_list_non_exhaustive() && !has_hidden => span_lint_and_sugg(
cx,
MATCH_WILDCARD_FOR_SINGLE_VARIANTS,
wildcard_span,
Expand All @@ -1129,7 +1130,7 @@ fn check_wild_enum_match(cx: &LateContext<'_>, ex: &Expr<'_>, arms: &[Arm<'_>])
),
variants => {
let mut suggestions: Vec<_> = variants.iter().copied().map(format_suggestion).collect();
let message = if adt_def.is_variant_list_non_exhaustive() {
let message = if adt_def.is_variant_list_non_exhaustive() || has_hidden {
suggestions.push("_".into());
"wildcard matches known variants and will also match future added variants"
} else {
Expand Down
5 changes: 5 additions & 0 deletions clippy_utils/src/attrs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -157,3 +157,8 @@ pub fn is_doc_hidden(attrs: &[ast::Attribute]) -> bool {
.filter_map(ast::Attribute::meta_item_list)
.any(|l| attr::list_contains_name(&l, sym::hidden))
}

/// Return true if the attributes contain `#[unstable]`
pub fn is_unstable(attrs: &[ast::Attribute]) -> bool {
attrs.iter().any(|attr| attr.has_name(sym::unstable))
}
8 changes: 8 additions & 0 deletions tests/ui/auxiliary/non-exhaustive-enum.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// Stripped down version of the ErrorKind enum of std
#[non_exhaustive]
pub enum ErrorKind {
NotFound,
PermissionDenied,
#[doc(hidden)]
Uncategorized,
}
7 changes: 7 additions & 0 deletions tests/ui/match_wildcard_for_single_variants.fixed
Original file line number Diff line number Diff line change
Expand Up @@ -115,9 +115,16 @@ fn main() {
pub enum Enum {
A,
B,
C,
#[doc(hidden)]
__Private,
}
match Enum::A {
Enum::A => (),
Enum::B => (),
Enum::C => (),
_ => (),
}
match Enum::A {
Enum::A => (),
Enum::B => (),
Expand Down
7 changes: 7 additions & 0 deletions tests/ui/match_wildcard_for_single_variants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,9 +115,16 @@ fn main() {
pub enum Enum {
A,
B,
C,
#[doc(hidden)]
__Private,
}
match Enum::A {
Enum::A => (),
Enum::B => (),
Enum::C => (),
_ => (),
}
match Enum::A {
Enum::A => (),
Enum::B => (),
Expand Down
39 changes: 19 additions & 20 deletions tests/ui/wildcard_enum_match_arm.fixed
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// run-rustfix
// aux-build:non-exhaustive-enum.rs

#![deny(clippy::wildcard_enum_match_arm)]
#![allow(
Expand All @@ -11,7 +12,9 @@
clippy::diverging_sub_expression
)]

use std::io::ErrorKind;
extern crate non_exhaustive_enum;

use non_exhaustive_enum::ErrorKind;

#[derive(Clone, Copy, Debug, Eq, PartialEq)]
enum Color {
Expand Down Expand Up @@ -77,29 +80,25 @@ fn main() {
let error_kind = ErrorKind::NotFound;
match error_kind {
ErrorKind::NotFound => {},
ErrorKind::PermissionDenied | ErrorKind::ConnectionRefused | ErrorKind::ConnectionReset | ErrorKind::ConnectionAborted | ErrorKind::NotConnected | ErrorKind::AddrInUse | ErrorKind::AddrNotAvailable | ErrorKind::BrokenPipe | ErrorKind::AlreadyExists | ErrorKind::WouldBlock | ErrorKind::InvalidInput | ErrorKind::InvalidData | ErrorKind::TimedOut | ErrorKind::WriteZero | ErrorKind::Interrupted | ErrorKind::Other | ErrorKind::UnexpectedEof | ErrorKind::Unsupported | ErrorKind::OutOfMemory | _ => {},
ErrorKind::PermissionDenied | _ => {},
}
match error_kind {
ErrorKind::NotFound => {},
ErrorKind::PermissionDenied => {},
ErrorKind::ConnectionRefused => {},
ErrorKind::ConnectionReset => {},
ErrorKind::ConnectionAborted => {},
ErrorKind::NotConnected => {},
ErrorKind::AddrInUse => {},
ErrorKind::AddrNotAvailable => {},
ErrorKind::BrokenPipe => {},
ErrorKind::AlreadyExists => {},
ErrorKind::WouldBlock => {},
ErrorKind::InvalidInput => {},
ErrorKind::InvalidData => {},
ErrorKind::TimedOut => {},
ErrorKind::WriteZero => {},
ErrorKind::Interrupted => {},
ErrorKind::Other => {},
ErrorKind::UnexpectedEof => {},
ErrorKind::Unsupported => {},
ErrorKind::OutOfMemory => {},
_ => {},
}

{
#![allow(clippy::manual_non_exhaustive)]
pub enum Enum {
A,
B,
#[doc(hidden)]
__Private,
}
match Enum::A {
Enum::A => (),
Enum::B | _ => (),
}
}
}
37 changes: 18 additions & 19 deletions tests/ui/wildcard_enum_match_arm.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// run-rustfix
// aux-build:non-exhaustive-enum.rs

#![deny(clippy::wildcard_enum_match_arm)]
#![allow(
Expand All @@ -11,7 +12,9 @@
clippy::diverging_sub_expression
)]

use std::io::ErrorKind;
extern crate non_exhaustive_enum;

use non_exhaustive_enum::ErrorKind;

#[derive(Clone, Copy, Debug, Eq, PartialEq)]
enum Color {
Expand Down Expand Up @@ -82,24 +85,20 @@ fn main() {
match error_kind {
ErrorKind::NotFound => {},
ErrorKind::PermissionDenied => {},
ErrorKind::ConnectionRefused => {},
ErrorKind::ConnectionReset => {},
ErrorKind::ConnectionAborted => {},
ErrorKind::NotConnected => {},
ErrorKind::AddrInUse => {},
ErrorKind::AddrNotAvailable => {},
ErrorKind::BrokenPipe => {},
ErrorKind::AlreadyExists => {},
ErrorKind::WouldBlock => {},
ErrorKind::InvalidInput => {},
ErrorKind::InvalidData => {},
ErrorKind::TimedOut => {},
ErrorKind::WriteZero => {},
ErrorKind::Interrupted => {},
ErrorKind::Other => {},
ErrorKind::UnexpectedEof => {},
ErrorKind::Unsupported => {},
ErrorKind::OutOfMemory => {},
_ => {},
}

{
#![allow(clippy::manual_non_exhaustive)]
pub enum Enum {
A,
B,
#[doc(hidden)]
__Private,
}
match Enum::A {
Enum::A => (),
_ => (),
}
}
}
22 changes: 14 additions & 8 deletions tests/ui/wildcard_enum_match_arm.stderr
Original file line number Diff line number Diff line change
@@ -1,38 +1,44 @@
error: wildcard match will also match any future added variants
--> $DIR/wildcard_enum_match_arm.rs:39:9
--> $DIR/wildcard_enum_match_arm.rs:42:9
|
LL | _ => eprintln!("Not red"),
| ^ help: try this: `Color::Green | Color::Blue | Color::Rgb(..) | Color::Cyan`
|
note: the lint level is defined here
--> $DIR/wildcard_enum_match_arm.rs:3:9
--> $DIR/wildcard_enum_match_arm.rs:4:9
|
LL | #![deny(clippy::wildcard_enum_match_arm)]
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

error: wildcard match will also match any future added variants
--> $DIR/wildcard_enum_match_arm.rs:43:9
--> $DIR/wildcard_enum_match_arm.rs:46:9
|
LL | _not_red => eprintln!("Not red"),
| ^^^^^^^^ help: try this: `_not_red @ Color::Green | _not_red @ Color::Blue | _not_red @ Color::Rgb(..) | _not_red @ Color::Cyan`

error: wildcard match will also match any future added variants
--> $DIR/wildcard_enum_match_arm.rs:47:9
--> $DIR/wildcard_enum_match_arm.rs:50:9
|
LL | not_red => format!("{:?}", not_red),
| ^^^^^^^ help: try this: `not_red @ Color::Green | not_red @ Color::Blue | not_red @ Color::Rgb(..) | not_red @ Color::Cyan`

error: wildcard match will also match any future added variants
--> $DIR/wildcard_enum_match_arm.rs:63:9
--> $DIR/wildcard_enum_match_arm.rs:66:9
|
LL | _ => "No red",
| ^ help: try this: `Color::Red | Color::Green | Color::Blue | Color::Rgb(..) | Color::Cyan`

error: wildcard matches known variants and will also match future added variants
--> $DIR/wildcard_enum_match_arm.rs:80:9
--> $DIR/wildcard_enum_match_arm.rs:83:9
|
LL | _ => {},
| ^ help: try this: `ErrorKind::PermissionDenied | ErrorKind::ConnectionRefused | ErrorKind::ConnectionReset | ErrorKind::ConnectionAborted | ErrorKind::NotConnected | ErrorKind::AddrInUse | ErrorKind::AddrNotAvailable | ErrorKind::BrokenPipe | ErrorKind::AlreadyExists | ErrorKind::WouldBlock | ErrorKind::InvalidInput | ErrorKind::InvalidData | ErrorKind::TimedOut | ErrorKind::WriteZero | ErrorKind::Interrupted | ErrorKind::Other | ErrorKind::UnexpectedEof | ErrorKind::Unsupported | ErrorKind::OutOfMemory | _`
| ^ help: try this: `ErrorKind::PermissionDenied | _`

error: aborting due to 5 previous errors
error: wildcard matches known variants and will also match future added variants
--> $DIR/wildcard_enum_match_arm.rs:101:13
|
LL | _ => (),
| ^ help: try this: `Enum::B | _`
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I find it confusing that the lint suggests here to use the enum value in the same arm as the wildcard. That would mean that future additions would also run into this branch.

It would be good to either add a note to explain why a wildcard has to be present here or to make the suggestion to have two arms like this:

Enum::B => {},
_ => { /* Hidden values */ }

Adding a note that the enum includes hidden, or unstable values would still be good.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A X | _ pattern is already caught by another lint, so this shouldn't be an issue.


error: aborting due to 6 previous errors