-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
Conversation
r? @llogiq (rust-highfive has picked a reviewer for you, use r? to override) |
r? @flip1995 |
Andre gave some good feedback to the lint, most specifically:
I skimmed |
That would be an |
There was a problem hiding this 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.
) { | ||
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"; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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:
- extract the closure bodies from
_inspect_args[1]
and_for_each_args[1]
- 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 - 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 thefor_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.
There was a problem hiding this comment.
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 Many thanks for your suggestions! commit 32405b8 reflects all the suggestions you made. commit ff1229a incorporates feedback from @llogiq Let me know which you prefer and I can adjust again! |
There was a problem hiding this 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.
ff1229a
to
32405b8
Compare
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. |
There was a problem hiding this 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
Thanks! Can you squash some of your commits, please? After that, this is ready to merge. |
55c1016
to
3269070
Compare
Just did 😃 Manual squashing gets easier each time one does it haha. Thank you! |
@bors r+ Thanks! |
📌 Commit 3269070 has been approved by |
☀️ Test successful - checks-action_dev_test, checks-action_remark_test, checks-action_test |
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.
.stderr
file)cargo test
passes locallycargo dev update_lints
cargo dev fmt
changelog: Add [
inspect_for_each
] lint for the use ofinspect().for_each()
onIterators
.