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

Disallow non-exhaustive matches #3649

Closed
Aehmlo opened this issue Jan 10, 2019 · 4 comments
Closed

Disallow non-exhaustive matches #3649

Aehmlo opened this issue Jan 10, 2019 · 4 comments
Labels
A-lint Area: New lints good-first-issue These issues are a good way to get started with Clippy

Comments

@Aehmlo
Copy link
Contributor

Aehmlo commented Jan 10, 2019

I'm not sure if this is better suited as a language feature or a clippy lint (though I assume clippy is an easier route to pursue), but it would be really nice to be able to either mark an enum (lang?) or a region of code (clippy?) as disallowing non-exhaustive matches. I know this might seem weird and/or bad, but I have a project wherein I want to make sure that all possible variants are covered, no wildcards, and I would like to have clippy be able to take care of checking for this on PRs instead of having to check it myself (clippy's way less likely to miss it than I am!).

Let's say my library has an internal enum giving movement types, and a function that performs an action based on this.

enum Movement {
    Fly,
    Run,
}
// Somewhere else
fn respond(movement: Movement) {
    match movement {
        Movement::Fly => fly(),
        // Disallow _ as a pattern
        Movement::Run => run(),
    };
}

Then, if I (or someone else) later add(s) an enum variant, I can be sure, if the tests pass, that the new movement type is being appropriately handled (without the wildcard pattern as a crutch).

enum Movement {
    Dig,
    Fly,
    Run,
}
// Somewhere else
fn respond(movement: Movement) {
    match movement {
        Movement::Fly => fly(),
        Movement::Run => run(),
        // Since we disallowed the wildcard _, we know Dig is being handled without looking
        Movement::Dig => dig(),
    }
}
@flip1995 flip1995 added good-first-issue These issues are a good way to get started with Clippy A-lint Area: New lints labels Jan 10, 2019
@flip1995
Copy link
Member

Clippy is the right place for this! This lint would go in one of the categories pedantic/restriction, so it would be an opt-in lint.

If someone is interested in implementing this lint, the best point to start would be the clippy_lints/src/matches.rs file.

@Aehmlo
Copy link
Contributor Author

Aehmlo commented Jan 10, 2019

I think I've implemented this locally. I'll test it out when I get back and send a PR once it's good!

@ghost
Copy link

ghost commented Jan 11, 2019

This is definitely a restriction lint. It would have too many problems to be applied generally.

  • The case where you have an enum with 25 variants and you want to treat 23 the same.
  • Libraries that specifically don't what callers to do exhaustive matching e.g. regex

bors added a commit that referenced this issue Jan 29, 2019
Add wildcard_match_arm lint

This lint prevents using a wildcard in a match arm. Implemented as a restriction currently, because this is pretty much an edge case. See #3649 for more information.

Didn't add any tests because I wasn't sure how, but if someone wants to point me in the right direction, I'd be happy to!
bors added a commit that referenced this issue Jan 29, 2019
Add wildcard_match_arm lint

This lint prevents using a wildcard in a match arm. Implemented as a restriction currently, because this is pretty much an edge case. See #3649 for more information.

Didn't add any tests because I wasn't sure how, but if someone wants to point me in the right direction, I'd be happy to!
bors added a commit that referenced this issue Jan 29, 2019
Add wildcard_match_arm lint

This lint prevents using a wildcard in a match arm. Implemented as a restriction currently, because this is pretty much an edge case. See #3649 for more information.

Didn't add any tests because I wasn't sure how, but if someone wants to point me in the right direction, I'd be happy to!
@Aehmlo
Copy link
Contributor Author

Aehmlo commented Jan 30, 2019

Closed in #3652.

@Aehmlo Aehmlo closed this as completed Jan 30, 2019
g-bartoszek pushed a commit to g-bartoszek/rust-clippy that referenced this issue Feb 7, 2019
This lint prevents using a wildcard in a match.
flip1995 pushed a commit to flip1995/rust-clippy that referenced this issue May 5, 2020
Changes:
````
wildcard_match_arm: Update lint count.
wildcard_match_arm: add nesting issue to known.
wildcard_match_arm: lint only enum matches.
wildcard_match_arm: update ui test stderr
wildcard_match_arm: format test.
wilcard_match_arm: run rustfmt.
wildcard_match_arm: add lint properly.
wildcard_match_arm: rename function.
wildcard_match_arm: add simple ui test.
wildcard_match_arm: expand lint scope.
Change match_wild lint name to WILDCARD_MATCH_ARM.
Add match_wild lint (rust-lang#3649).
fetch_prs_between: add .sh file ending
cargo fmt
Update various docs
Use built-in entry_fn detection over self-built
cargo fmt
Reorganize conditionals: Run faster checks first
Maybe fix ICE?
Add initial version of const_fn lint
Fix `unit_arg` false positive
Rustfmt
Check hypothetically failing conversion
Remove tests for deprecated items
Update more changed iterator paths
Atomics constants are now handled by the deprecation lint
Update changed iterator paths
Update const slice processing
update test stderr
run cargo fmt
rustup rust-lang/rust#57907
Fix documentation for `slow_vector_initialization`
rustup rust-lang/rust#57726
Remove unsafe_vector_initialization from added lints
Prevent incorrect cast_lossless suggestion in const_fn
Incorporate review suggestions
Fix dogfood tests on Appveyor
test(versioncheck): Use .no_deps()
test(versioncheck): Fix version equality check
chore(cargo/dependencies/cargo-metadata): Upgrade to 0.7.1
dependencies: update itertools from 0.7 to 0.8
Add script to fetch GitHub PRs between two commits
gitattributes: Treat .fixed files as rust files
Update changelog with all changes since 0.0.212
Fix `expect_fun_call` lint suggestions
````
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-lint Area: New lints good-first-issue These issues are a good way to get started with Clippy
Projects
None yet
Development

No branches or pull requests

2 participants