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

suggest the | for match_same_arms #1075

Closed
Apanatshka opened this issue Jul 9, 2016 · 5 comments · Fixed by #1082
Closed

suggest the | for match_same_arms #1075

Apanatshka opened this issue Jul 9, 2016 · 5 comments · Fixed by #1082
Labels
A-documentation Area: Adding or improving documentation C-enhancement Category: Enhancement of lints, like adding more cases or adding help messages

Comments

@Apanatshka
Copy link

I ran into match_same_arms and was quite confused. I got ready to report a false positive, but then I found #860 and figured out that I just needed to use | in my pattern.

In #860 in the comments having clippy suggest of | is mentioned, but no issue seems to have been opened. So I'm doing so now :)

@mcarton
Copy link
Member

mcarton commented Jul 9, 2016

I'm pretty sure it has already been reported that what the lint means (ie. use |) is unclear but I can't find an issue for that. Maybe it was on IRC. The wiki does mention | with a link to the book but that's clearly not enough as people still get confused.
I'm currently revamping suggestions, I'll look into it.

@mcarton mcarton added C-enhancement Category: Enhancement of lints, like adding more cases or adding help messages A-documentation Area: Adding or improving documentation labels Jul 9, 2016
@mcarton mcarton mentioned this issue Jul 10, 2016
@mcarton
Copy link
Member

mcarton commented Jul 10, 2016

Ok, I've open #1082. The error will now contain note: consider refactoring to blablah | foobar`` and the doc of the lint will be:

What it does: This lint checks for match with identical arm bodies.

Why is this bad? This is probably a copy & paste error. If arm bodies are the same on
purpose, you can factor them
using |.

Known problems: Known problems: False positive possible with order dependent match
(see issue #860).

Example:

 match foo {
     Bar => bar(),
     Quz => quz(),
     Baz => bar(), // <= oops
 }

This should probably be

 match foo {
     Bar => bar(),
     Quz => quz(),
     Baz => baz(), // <= fixed
 }

or if the original code was not a typo:

 match foo {
     Bar | Baz => bar(), // <= shows the intent better
     Quz => quz(),
 }

Is that enough?

@Apanatshka
Copy link
Author

Looks great! 👍 Will the note in the error use the code that was found? That would make it usable by tools like rustfix.

@Apanatshka
Copy link
Author

To answer my own question: kinda. It uses the matched code but probably doesn't give it in a way that rustfix can use atm.

@mcarton
Copy link
Member

mcarton commented Jul 10, 2016

Yes, but the suggestion won't be applicable by rustfix. There is no clean way to signal that we need to remove one of the arm.
On the other hand I'm working on improving usability with rustfix in general.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-documentation Area: Adding or improving documentation C-enhancement Category: Enhancement of lints, like adding more cases or adding help messages
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants