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

Improve match like matches lint #6216

Merged
merged 2 commits into from
Oct 27, 2020

Conversation

alex-700
Copy link
Contributor

fixes #6186

changelog: improve MATCH_LIKE_MATCHES_MACRO lint

@rust-highfive
Copy link

r? @ebroto

(rust_highfive has picked a reviewer for you, use r? to override)

@rust-highfive rust-highfive added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties label Oct 24, 2020
@alex-700 alex-700 force-pushed the improve-match-like-matches-lint branch from 6fe0035 to 4b0a88b Compare October 24, 2020 15:19
@alex-700
Copy link
Contributor Author

@ebroto, I remember about this, but don't know how to do it without global refactoring near these two lints. Do you have some other ideas? Is there a big issue for clippy: don't lint two different lints for the same code?

@ebroto
Copy link
Member

ebroto commented Oct 24, 2020

but don't know how to do it without global refactoring near these two lints.

One approach is to move match_same_arms into the matches module where match_like_matches_macro lives, and not trigger the former if the latter lints. I just did something similar in this commit for another lint.

In this case we would have to move some functions from the copies module to utils. Let me know if I explained my idea correctly :)

@ebroto ebroto added S-waiting-on-author Status: This is awaiting some action from the author. (Use `@rustbot ready` to update this status) and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties labels Oct 24, 2020
@ebroto
Copy link
Member

ebroto commented Oct 24, 2020

Is there a big issue for clippy: don't lint two different lints for the same code?

Giving two different suggestions for the same code can be misleading if they suggest different things, so we try to avoid it. Moreover, if both lints have auto-applicable suggestions, trying to apply the second one would fail.

@alex-700
Copy link
Contributor Author

Let me know if I explained my idea correctly

Yeap, I think that your idea is perfectly clear. It's the one I called "global refactoring near these two lints" :) The main problem that all examples in clippy code, I see, were straightforward methods, which call cx.span_* after "batch of ifs". For reusing the code for non-linting something I need some another approach. I'm choosing between the following:

  • introduce method which will return Option<SpanContext>, instead of calling cx.span_*, where SpanContext is some information that this lint gathered during "batch of ifs". But what should be stored in this SpanContext?..
  • like the first one, but returns Option<impl Fn()> or mb Option<dyn Fn()>, where call of this function is exactly "spanning the lint"
  • have some set of spans of other lints, which can conflict with my one (like in your example). In this case I need to track the order of checks (introduce common lint-group for this one), also I need to share between several fns information about which of these spans I saved into the set. IMHO, this option isn't good for following code support.

@ebroto did you get, what I'm trying to say? Is the third idea common for clippy codebase? If it is, can you point several others examples? Personally I like the second idea in cases where there is no some cpu-consuming calculations in this Option<...> evaluation.

Copy link
Member

@ebroto ebroto left a comment

Choose a reason for hiding this comment

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

The implementation LGTM, I've just suggested to add some tests.

About the interaction with match_same_arms, I apologize for not being more precise in my first comment. I pointed you to that commit to show that we can move a lint to another module, but the solution of storing the spans is very specific to that case because the conflicting lints start a different levels, one in check_block and the other one in check_expr.

In this case, I think it's easier because both lints work at the expression level, and if the expression is a match. So the solution should be applied in this section of the matches module and doing something like

let matches_applied = check_match_like_matches(cx, expr);
// ...
if !matches_applied {
    check_match_same_arms(cx, expr);
}

I want to say that every time we try to avoid conflicting lints, it's a hack. We need a more general solution, probably implemented in rustc itself.

tests/ui/match_expr_like_matches_macro.rs Show resolved Hide resolved
tests/ui/match_expr_like_matches_macro.rs Show resolved Hide resolved
tests/ui/match_expr_like_matches_macro.rs Show resolved Hide resolved
@alex-700 alex-700 force-pushed the improve-match-like-matches-lint branch from 4b0a88b to 287be36 Compare October 27, 2020 20:42
- add tests
- refactor match_same_arms lint
- prioritize match_expr_like_matches_macro over match_same_arms
@alex-700 alex-700 force-pushed the improve-match-like-matches-lint branch from 287be36 to 2b7dd31 Compare October 27, 2020 20:46
@alex-700 alex-700 requested a review from ebroto October 27, 2020 20:56
@ebroto
Copy link
Member

ebroto commented Oct 27, 2020

@bors r+

Thanks for taking care of this!

@bors
Copy link
Collaborator

bors commented Oct 27, 2020

📌 Commit 2b7dd31 has been approved by ebroto

@bors
Copy link
Collaborator

bors commented Oct 27, 2020

⌛ Testing commit 2b7dd31 with merge de83f09...

@ebroto ebroto removed the S-waiting-on-author Status: This is awaiting some action from the author. (Use `@rustbot ready` to update this status) label Oct 27, 2020
@bors
Copy link
Collaborator

bors commented Oct 27, 2020

☀️ Test successful - checks-action_dev_test, checks-action_remark_test, checks-action_test
Approved by: ebroto
Pushing de83f09 to master...

@bors bors merged commit de83f09 into rust-lang:master Oct 27, 2020
@alex-700 alex-700 deleted the improve-match-like-matches-lint branch December 29, 2020 11:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

match_like_matches_macro lint could be improved a bit more
4 participants