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

Add wildcard_match_arm lint #3652

Merged
merged 12 commits into from Jan 29, 2019

Conversation

Projects
None yet
6 participants
@Aehmlo
Copy link
Contributor

Aehmlo commented Jan 10, 2019

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!

@flip1995
Copy link
Collaborator

flip1995 left a comment

LGTM!

It indeed would be great if you could add tests. You can look in the tests/ui/ folder. Just add a new file there, where you put patterns in where the lint should trigger and some where it shouldn't. One test case where it shouldn't trigger is for example an _ if some_cond => (), arm IMO. Make sure to add #![warn(clippy::wildcard_match_arm)] to the top of the file.

After you added this file run cargo test (you can restrict this to just your test, see CONTRIBUTING.md for this). Now your test should fail, because you need to generate a *.stderr file. You can do this by running the tests/ui/update-references.sh script. The exact command will be printed with the output of cargo test, so you can just c&p this.

Show resolved Hide resolved clippy_lints/src/matches.rs Outdated
Show resolved Hide resolved clippy_lints/src/matches.rs Outdated
@Aehmlo

This comment has been minimized.

Copy link
Contributor

Aehmlo commented Jan 15, 2019

Update: I've made some changes (including the above), but it'll be a few days until I can finish implementing the tests. I'll push everything once that's done.

@Aehmlo Aehmlo force-pushed the Aehmlo:where_the_wild_things_are branch from d1aa886 to a41ecc4 Jan 25, 2019

@Aehmlo Aehmlo closed this Jan 25, 2019

@Aehmlo Aehmlo reopened this Jan 25, 2019

@Aehmlo

This comment has been minimized.

Copy link
Contributor

Aehmlo commented Jan 25, 2019

Wrong button! I think this should be good to go now.

@Aehmlo Aehmlo changed the title Add match_wild lint. Add wildcard_match_arm lint Jan 25, 2019

@flip1995

This comment has been minimized.

Copy link
Collaborator

flip1995 commented Jan 26, 2019

Thanks!

Only a rustfmt run is missing, then this will be good to go.
https://travis-ci.com/rust-lang/rust-clippy/jobs/173071805#L1387-L1415

@Aehmlo

This comment has been minimized.

Copy link
Contributor

Aehmlo commented Jan 27, 2019

Ran rustfmt and pushed, but it seems that there are some other nightly-related breakages that popped up.

@flip1995

This comment has been minimized.

Copy link
Collaborator

flip1995 commented Jan 29, 2019

Thanks! That should be fixed by now.

@bors r+

@bors

This comment has been minimized.

Copy link
Contributor

bors commented Jan 29, 2019

📌 Commit fefe55e has been approved by flip1995

@bors

This comment has been minimized.

Copy link
Contributor

bors commented Jan 29, 2019

⌛️ Testing commit fefe55e with merge a70d648...

bors added a commit that referenced this pull request Jan 29, 2019

Auto merge of #3652 - Aehmlo:where_the_wild_things_are, r=flip1995
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

This comment has been minimized.

Copy link
Contributor

bors commented Jan 29, 2019

💔 Test failed - checks-travis

@flip1995

This comment has been minimized.

Copy link
Collaborator

flip1995 commented Jan 29, 2019

It seems that a formatting of the tests is missing. This should be the last chore.

https://travis-ci.com/rust-lang/rust-clippy/jobs/173709864#L1383-L1447

A cargo fmt --all inside the crate root should format everything

@Aehmlo Aehmlo force-pushed the Aehmlo:where_the_wild_things_are branch from 7056388 to f56e013 Jan 29, 2019

@Aehmlo

This comment has been minimized.

Copy link
Contributor

Aehmlo commented Jan 29, 2019

Neither cargo fmt --all nor cargo +nightly fmt --all was working to format the tests, so I just ran rustfmt on the file directly. Regardless, it looks like the build is now passing!

@phansch

This comment has been minimized.

Copy link
Collaborator

phansch commented Jan 29, 2019

@bors retry

@orium

This comment has been minimized.

Copy link
Member

orium commented Jan 29, 2019

I think it would be better just to emit a lint in cases of an enum. So

match 3 {
    4 => println!(),
    _ => println!(),
};

would not emit a warning.

Note also that it would need to check for matching in an enum on any nesting level. I.e.

match Some(Color::Red) {
    Some(Color::Blue) => println!(),
    Some(_) => println!(),
    None => println!(),
};

would emit a lint.

@phansch

This comment has been minimized.

Copy link
Collaborator

phansch commented Jan 29, 2019

@bors r- pending decision on linting non-enum matches

@Aehmlo

This comment has been minimized.

Copy link
Contributor

Aehmlo commented Jan 29, 2019

I agree re: linting only on enums. I've changed the behavior and updated the lint name accordingly. I'm not familiar enough with compiler internals to implement the nested check yet, so I added a note of this limitation to the known issues. I may try to resolve this in the future if I have time, or someone else can, but I won't be able to do it in the near future.

Thanks for the insightful feedback, @orium!

@phansch

This comment has been minimized.

Copy link
Collaborator

phansch commented Jan 29, 2019

@bors r+ LGTM now, thanks!

@bors

This comment has been minimized.

Copy link
Contributor

bors commented Jan 29, 2019

📌 Commit 97c7d28 has been approved by phansch

@bors

This comment has been minimized.

Copy link
Contributor

bors commented Jan 29, 2019

⌛️ Testing commit 97c7d28 with merge c254711...

bors added a commit that referenced this pull request Jan 29, 2019

Auto merge of #3652 - Aehmlo:where_the_wild_things_are, r=phansch
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 Aehmlo force-pushed the Aehmlo:where_the_wild_things_are branch from 97c7d28 to 7fa50fb Jan 29, 2019

@Aehmlo

This comment has been minimized.

Copy link
Contributor

Aehmlo commented Jan 29, 2019

Updated lint count; should actually work next time.

@matthiaskrgr

This comment has been minimized.

Copy link
Collaborator

matthiaskrgr commented Jan 29, 2019

@bors retry

@matthiaskrgr

This comment has been minimized.

Copy link
Collaborator

matthiaskrgr commented Jan 29, 2019

@bors ping

@bors

This comment has been minimized.

Copy link
Contributor

bors commented Jan 29, 2019

😪 I'm awake I'm awake

@matthiaskrgr

This comment has been minimized.

Copy link
Collaborator

matthiaskrgr commented Jan 29, 2019

@bors r=phansch

@bors

This comment has been minimized.

Copy link
Contributor

bors commented Jan 29, 2019

📌 Commit 7fa50fb has been approved by phansch

@bors

This comment has been minimized.

Copy link
Contributor

bors commented Jan 29, 2019

⌛️ Testing commit 7fa50fb with merge 6ce78d1...

bors added a commit that referenced this pull request Jan 29, 2019

Auto merge of #3652 - Aehmlo:where_the_wild_things_are, r=phansch
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

This comment has been minimized.

Copy link
Contributor

bors commented Jan 29, 2019

☀️ Test successful - checks-travis, status-appveyor
Approved by: phansch
Pushing 6ce78d1 to master...

@bors bors merged commit 7fa50fb into rust-lang:master Jan 29, 2019

2 checks passed

continuous-integration/appveyor/pr AppVeyor build succeeded
Details
homu Test successful
Details
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment