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

match_like_matches_macro lint could be improved a bit more #6186

Closed
GuillaumeGomez opened this issue Oct 17, 2020 · 5 comments · Fixed by #6216
Closed

match_like_matches_macro lint could be improved a bit more #6186

GuillaumeGomez opened this issue Oct 17, 2020 · 5 comments · Fixed by #6216
Labels
C-enhancement Category: Enhancement of lints, like adding more cases or adding help messages good-first-issue These issues are a good way to get started with Clippy

Comments

@GuillaumeGomez
Copy link
Member

For example, this case doesn't warn:

match self {
    WorkMode::Normal => true,
    WorkMode::Sys => true,
    _ => false,
}

But it could be improved using the matches! macro:

matches!(self, WorkMode::Normal | WorkMode::Sys)
@ebroto
Copy link
Member

ebroto commented Oct 18, 2020

This case will overlap with match_same_arms. Since match_like_matches_macro is warn-by-default, it should have priority, so the former should not trigger.

@ebroto ebroto added C-enhancement Category: Enhancement of lints, like adding more cases or adding help messages good-first-issue These issues are a good way to get started with Clippy hacktoberfest labels Oct 18, 2020
@alex-700
Copy link
Contributor

alex-700 commented Oct 19, 2020

Hi, I'd like to hack on this.

I think that every match, where every arm expression is a bool literal, is a good candidate for matches! (yeah, I remember about some special cases (where I need to suppress this), like redundant_pattern_matching. But at the same time there are additional questions with exhaustiveness, e.g. where match enumerate all enum variants, and matches! can't do this.

Now clippy suggests matches! only in the case when we have a wildcard pattern.

@ebroto what do you think about this trade-off?

@ebroto
Copy link
Member

ebroto commented Oct 19, 2020

I think that every match, where every arm expression is a bool literal, is a good candidate for matches!

I think you're right. The main problem I can see with targeting more than two arms where one is wildcard is that we can have multiple if guards that could be difficult to "merge" in order to be used in matches!(). We probably should not lint there.

But at the same time there are additional questions with exhaustiveness, e.g. where match enumerate all enum variants, and matches! can't do this

Not sure I got you here. You mean that in some cases match should be preferred because of the exhaustiveness check?

@alex-700
Copy link
Contributor

We probably should not lint there.

Yeap, patterns with if guards are evil for this lint.

You mean that in some cases match should be preferred because of the exhaustiveness check?

Yeap, you got it right!

I thought more and came to an idea that there are some issues about an order of patterns too. And I need a hard machinery from rustc (to check that I can reorder some patterns), it's hard to use from clippy.

So the plan is triggering this lint for the following.

match <...> {
  <pat_0> => b0,
  <pat_1> => b0,
  ...
  <pat_k> => b0,
  <pat_k+1> => b1,
  ...
  <pat_n> => b1,
  _ => b1,
}

where b0 and b1 are different bool literals, (and maybe even const bool 🤔).

And the question from my previous comment was about the last _ => b1. @ebroto what do you think: should we make it optional and lint even in the case, when there are no wildcard pattern?

@ebroto
Copy link
Member

ebroto commented Oct 21, 2020

@alex-700 given that this is a warn-by-default lint, I think the safest approach for the moment would be a generalization of the initial comment in this issue:

match <...> {
    <pat_0> => b0,
    <pat_1> => b0,
    ...
    <pat_n> => b0,
    _ => b1,
}

What do you think?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-enhancement Category: Enhancement of lints, like adding more cases or adding help messages good-first-issue These issues are a good way to get started with Clippy
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants