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

New Lint: inspect_then_for_each #6577

Merged
merged 1 commit into from
Jan 19, 2021

Conversation

nahuakang
Copy link
Contributor

@nahuakang nahuakang commented Jan 11, 2021

Work In Progress

This PR addresses Issue 5209 and adds a new lint called inspect_then_for_each.

Current seek some guidance.

If you added a new lint, here's a checklist for things that will be
checked during review or continuous integration.

  • Followed lint naming conventions
  • Added passing UI tests (including committed .stderr file)
  • cargo test passes locally
  • Executed cargo dev update_lints
  • Added lint documentation
  • Run cargo dev fmt

changelog: Add [inspect_for_each] lint for the use of inspect().for_each() on Iterators.

@rust-highfive
Copy link

r? @llogiq

(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 Jan 11, 2021
@nahuakang
Copy link
Contributor Author

r? @flip1995

@rust-highfive rust-highfive assigned flip1995 and unassigned llogiq Jan 12, 2021
@nahuakang
Copy link
Contributor Author

nahuakang commented Jan 15, 2021

Andre gave some good feedback to the lint, most specifically:

Note however that we could cover the simple case where the patterns are exactly equal (with a spanless_eq, search clippy's internal utils).

I skimmed utils/hir_utils but, unfortunately, I still don't know how I could get Pat in this situation to compare the closure parameter patterns. Would appreciate some further hints if anyone sees this comment 🙏

@llogiq
Copy link
Contributor

llogiq commented Jan 16, 2021

That would be an ExprKind::Closure from which you can get the BodyId. Then use the ctx to look up the Body, which has a slice of Params, each of which has a Pat.

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.

This PR LGTM. The suggestion generation is a whole other topic, which I would address in a separate PR.

The span and the help message has to be adapted and this should be good to go.

clippy_lints/src/methods/mod.rs Outdated Show resolved Hide resolved
clippy_lints/src/methods/inspect_then_for_each.rs Outdated Show resolved Hide resolved
) {
if match_trait_method(cx, expr, &paths::ITERATOR) {
let msg = "called `inspect(..).for_each(..)` on an `Iterator`";
let hint = "this is more succinctly expressed by calling ` for_each(..)` instead";
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
let hint = "this is more succinctly expressed by calling ` for_each(..)` instead";
let hint = "move the code from `inspect(..)` to `for_each(..)` and remove the `inspect(..)`";

I think a span_lint_and_help is enough here for now. Building a good looking suggestion by combining the closures into one is way more involved (getting the indentation right and whatnot).


Some guidance how this can be done:

  1. extract the closure bodies from _inspect_args[1] and _for_each_args[1]
  2. check if the closure body of for_each is already a block
    a. If it is, get the span of everything inside the block.
    b. If not, just get the span of the expression
  3. build the suggestion with a block around it, where the body of inspect is placed at the top and is indented the same as everything inside the for_each function.

This will involve much fiddling. I done this before and utils has many methods to achieve this (search for usages of indent_of in the code base). It'll be mostly trial and error though.

Anyway, this should probably be done in a separate PR.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

You're right that it could be done in a separate PR. In any case, I've taken notes about your suggestions :)

@flip1995 flip1995 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 Jan 17, 2021
@nahuakang
Copy link
Contributor Author

@flip1995 Many thanks for your suggestions!

commit 32405b8 reflects all the suggestions you made.

commit ff1229a incorporates feedback from @llogiq
and includes some additional checks for the two closure's parameter patterns before running the lint.

Let me know which you prefer and I can adjust again!

@nahuakang nahuakang marked this pull request as ready for review January 17, 2021 20:05
@nahuakang nahuakang requested a review from flip1995 January 17, 2021 20:09
@nahuakang nahuakang changed the title (WIP): New Lint: inspect_then_for_each New Lint: inspect_then_for_each Jan 17, 2021
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.

Why is it important that the patterns of the closures are the same? I can see that this may cause problems when producing suggestions, but for now this shouldn't matter. Am I missing something?

Please also add a test for this.

clippy_lints/src/methods/inspect_then_for_each.rs Outdated Show resolved Hide resolved
@nahuakang nahuakang force-pushed the inspect_then_for_each branch from ff1229a to 32405b8 Compare January 18, 2021 19:19
@nahuakang
Copy link
Contributor Author

It does make only sense to check for the closure patterns if we aim to produce suggestions. In this scope, you're right we should keep the lint as simple as possible. The change is reverted now.

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.

Just the lint name

clippy_lints/src/methods/mod.rs Outdated Show resolved Hide resolved
@flip1995
Copy link
Member

Thanks! Can you squash some of your commits, please? After that, this is ready to merge.

@nahuakang nahuakang force-pushed the inspect_then_for_each branch from 55c1016 to 3269070 Compare January 19, 2021 10:32
@nahuakang
Copy link
Contributor Author

Just did 😃 Manual squashing gets easier each time one does it haha. Thank you!

@flip1995
Copy link
Member

@bors r+

Thanks!

@bors
Copy link
Contributor

bors commented Jan 19, 2021

📌 Commit 3269070 has been approved by flip1995

@bors
Copy link
Contributor

bors commented Jan 19, 2021

⌛ Testing commit 3269070 with merge d71dea4...

@bors
Copy link
Contributor

bors commented Jan 19, 2021

☀️ Test successful - checks-action_dev_test, checks-action_remark_test, checks-action_test
Approved by: flip1995
Pushing d71dea4 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.

5 participants