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

fix redundant_pattern_matching lint #5483

Merged
merged 1 commit into from
Apr 17, 2020

Conversation

alex-700
Copy link
Contributor

@alex-700 alex-700 commented Apr 17, 2020

changelog: Fix suggestion in redundant_pattern_matching and also apply this lint to the while let case

@alex-700
Copy link
Contributor Author

alex-700 commented Apr 17, 2020

And I have a couple questions. (so PR is WIP for now)

  • this lint for while let "conflicts" with the while-let-on-iterator lint. What should we do with this? It seems not okay to give out two lints about one piece of code.

  • before this PR the most of suggestions from this lint was completely wrong. It seems that we need replace only while/if let PATS = EXPR part, but not whole expression/statement. But this way can generate a code like if o.is_some() { true } else { false } that should be fix by another lint. What should we do with this?

Thanks.

@flip1995
Copy link
Member

  • This is kind of a problem, but can be fixed quite easily. You can just copy this check

    if method_path.ident.name == sym!(next)
    && match_trait_method(cx, match_expr, &paths::ITERATOR)
    to this lint and return if the match_expr is a Iterator::next method call. match_expr is in this case op and the method_path you can get by matching if let ExprKind::MethodCall(method_path) = op.kind.

  • Thanks for fixing this suggestion! When a suggestion leads to another lint triggering, it is not a problem as long as it doesn't become a cycling suggestion, which is not the case here.

@flip1995 flip1995 added the S-waiting-on-author Status: This is awaiting some action from the author. (Use `@rustbot ready` to update this status) label Apr 17, 2020
diag.span_suggestion(
span,
"try this",
format!("{}.{}{}", snippet(cx, op.span, "_"), good_method, maybe_semi),
format!("{} {}.{}", keyword, snippet(cx, op.span, "_"), good_method),
Applicability::MaybeIncorrect, // snippet
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 this is

Suggested change
Applicability::MaybeIncorrect, // snippet
Applicability::MachineApplicable, // snippet

now

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Agree. Fixed.

@alex-700
Copy link
Contributor Author

@flip1995

  • I'll do this, but it looks strange a bit. Is it a usual situation to check that we don't trigger lint in the code of the other lint? Maybe is there a better machinery for it?
  • Is it ok to allow some "triggered after fixit" lints in tests (as I did here)?

@flip1995
Copy link
Member

flip1995 commented Apr 17, 2020

  • Lints overlapping is pretty unusual, so we don't have anything to prevent this. Normally, if lints overlap, they are pretty similar and therefore are implemented in the same module, where it is easier to separate them ("if a -> trigger lint 1; if b -> trigger lint 2"). I think just introducing this check here (with a comment why it is there) is the easiest way to do this in this case.

  • Yes, that is the standard approach for this.

- now it handles `while let` case
- better suggestions in `if let` case
@alex-700 alex-700 force-pushed the fix-redundant-pattern-matching branch from e21b881 to 092c459 Compare April 17, 2020 18:51
@alex-700 alex-700 changed the title WIP: fix redundant_pattern_matching lint fix redundant_pattern_matching lint Apr 17, 2020
@alex-700 alex-700 requested a review from flip1995 April 17, 2020 19:08
Copy link
Member

@flip1995 flip1995 left a comment

Choose a reason for hiding this comment

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

Thanks!

@flip1995
Copy link
Member

@bors r+

@bors
Copy link
Collaborator

bors commented Apr 17, 2020

📌 Commit 092c459 has been approved by flip1995

@bors
Copy link
Collaborator

bors commented Apr 17, 2020

⌛ Testing commit 092c459 with merge 5d549c8...

bors added a commit that referenced this pull request Apr 17, 2020
…1995

fix redundant_pattern_matching lint

- now it handles `while let` case  (related to #5462)
- better suggestions in `if let` case
@bors
Copy link
Collaborator

bors commented Apr 17, 2020

💔 Test failed - checks-action_test

@alex-700
Copy link
Contributor Author

@flip1995 sorry, should I fix something here? Changelog? What should be written in changelog?

And I think that I fix #4344 too.

@flip1995
Copy link
Member

Don't worry about the changelog, this only means, that the PR body must contain a line changelog: ... which describes, what this PR does in one sentence. I added that for you.

@bors retry

@bors
Copy link
Collaborator

bors commented Apr 17, 2020

⌛ Testing commit 092c459 with merge 1c0e4e5...

@bors
Copy link
Collaborator

bors commented Apr 17, 2020

☀️ Test successful - checks-action_dev_test, checks-action_remark_test, checks-action_test
Approved by: flip1995
Pushing 1c0e4e5 to master...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-author Status: This is awaiting some action from the author. (Use `@rustbot ready` to update this status)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants