5

Fix `any()` not taking reference in `search_is_some` lint by ThibsG · Pull Reque...

 2 years ago
source link: https://github.com/rust-lang/rust-clippy/pull/7463
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

New issue

Fix any() not taking reference in search_is_some lint #7463

Conversation

Copy link

Contributor

ThibsG commented on Jul 14

edited

find gives reference to the item, but any does not, so suggestion is broken in some specific cases.

Fixes: #7392

changelog: [search_is_some] Fix suggestion for any() not taking item by reference

Copy link

Collaborator

rust-highfive commented on Jul 14

r? @Manishearth

(rust-highfive has picked a reviewer for you, use r? to override)

Copy link

Member

@Manishearth Manishearth left a comment

I'm not a big fan of direct string replacing, unsure if there's a better way to do it

Copy link

Member

Manishearth commented on Jul 14

cc @camsteffen got ideas for how to do this nicely without string replacing? Perhaps using a multipart suggestion to replace specific spans after running an ExprUseVisitor?

Copy link

Collaborator

camsteffen commented on Jul 14

Perhaps using a multipart suggestion to replace specific spans after running an ExprUseVisitor?

Yes I would do that. String replacement could go wrong in a lot of ways. Also, we should handle cases where the variable is derefed. Autoderef requires no change. Explicit deref can be removed.

Copy link

Contributor

Author

ThibsG commented on Jul 19

I agree about string replacement, I never used multipart suggestion, it's time to experience it !

For me, explicit deref is removed here: https://github.com/rust-lang/rust-clippy/pull/7463/files#diff-c7288bb6febe070c5809cdc2ebe9fc138a849a1ded710011daf9b363888a744bR51
Are you talking about different situation?

Copy link

Collaborator

camsteffen commented on Jul 19

For me, explicit deref is removed here:

Yes, right idea, but should not use String::replace.

Copy link

Member

Manishearth commented on Aug 10

r? @xFrednet

I might not have time to complete this review in a timely manner at the moment, hoping someone else can pick this up!

Copy link

Member

xFrednet commented on Aug 10

Sure, I can pick this PR up! upside_down_face

I'll have a quick scan over it now and then try to do the proper review tomorrow upside_down_face. ThibsG could you please also include the lint name as part of your changelog entry. A format like this is usually very helpful:

changelog: [`<lint_name>`] message

Copy link

Member

@xFrednet xFrednet left a comment

Hey, I've marked some points that can be improved IMO. I'm impressed by this work. The API you used is a bit confusing and not as well documented, but you used it correctly from what I can tell. +1

It would be nice to directly construct the search closure. I suggested a possible solution here, that should in theory work. However, that might require some more work and the PR as it is would already be a great improvement. Therefore, I would also be happy to merge it with the newly added help messages. You can decide what you prefer.

Thank you for the work, and sorry that the review took so long. upside_down_face

Copy link

Member

@xFrednet xFrednet left a comment •

edited

The new suggestions are already looking really nice, thank you!

Looking at the .stderr files, I think we can shorten the lint message a bit by replacing the use ... instead: with a try:. This shortens the message a lot, but might be a bit harder to understand. I'm not entirely sure if it's worth it. What are your thoughts about it?


Side note: I've done a quick local test, and this suggestion building could in theory also be used to create suggestion over multiple lines:

error: called `is_none()` after searching an `Iterator` with `find`
  --> $DIR/aaa.rs:6:13
   |
LL |       let _ = (0..17).find(|x| {
   |  _____________^
LL | |         *x == 7
LL | |         || *x == 9
LL | |         || *x == 17
LL | |     }).is_none();
   | |________________^
   |
   = note: `-D clippy::search-is-some` implied by `-D warnings`
help: use `!_.any()` instead
   |
LL ~     let _ = !(0..17).any(|x| {
LL +         x == 7
LL +         || x == 9
LL +         || x == 17
LL ~     });
   |

In my tests I've put the line limit to 5 which ensures that the suggestion is still displayed to the user. This would be a nice feature but should be added in a followup PR, as it would also require dealing with Delegate::mutate. Just an idea if you started to enjoy working with Span-Magic sparkleswink

// Build suggestion gradually by handling closure arg specific usages,

// such as explicit deref and borrowing cases.

// Returns `None` if no such use cases have been triggered in closure body

fn get_closure_suggestion<'tcx>(

Copy link

Member

@xFrednet xFrednet on Aug 25

edited

This is a nice function, where I can image that it will become useful to other lints as well. Could you move it to clippy_utils (probably the sugg module) and also add an example to the doc comment?

It should also be noted that it currently doesn't support mutations upside_down_face

The refactorings are looking good. I'm guessing we'll leave this comment open until the suggestion is build correctly, so we can keep the review comments upside_down_face

This function should get a more descriptive name for inside clippy_utils::sugg maybe something like create_closure_deref_args_sugg or something similar, it's hard to find a good descriptive name. I'm also not happy with my suggestion yet thinking

Copy link

Contributor

Author

ThibsG commented on Sep 7

Looking at the .stderr files, I think we can shorten the lint message a bit by replacing the use ... instead: with a try:. This shortens the message a lot, but might be a bit harder to understand. I'm not entirely sure if it's worth it. What are your thoughts about it?

We can move the reference to the use of !_.any() in message instead. But I don't have strong opinion about this, so we can also remove it completely or ask Clippy's team their feeling about it.

Copy link

Member

@xFrednet xFrednet left a comment

Hey, I've reviewed your recent changes, which already improved the suggestion building +1

I've also done some testing on your branch and found a few instances where the suggestion can be improved. These would also be great test cases, along tests for the other projection types.

My test cases

Thank you for putting in this work. I've underestimated the complexity a bit sweat_smile. If we get this working, it will be a nice addition to Clippy IMO upside_down_face. Also thank you for waiting on the review, university has again taken up a lot of time which made the review take longer upside_down_face

// Build suggestion gradually by handling closure arg specific usages,

// such as explicit deref and borrowing cases.

// Returns `None` if no such use cases have been triggered in closure body

fn get_closure_suggestion<'tcx>(

The refactorings are looking good. I'm guessing we'll leave this comment open until the suggestion is build correctly, so we can keep the review comments upside_down_face

Copy link

Member

xFrednet commented on Sep 11

We can move the reference to the use of !_.any() in message instead. But I don't have strong opinion about this, so we can also remove it completely or ask Clippy's team their feeling about it.

Good point that we should mention it somewhere. On second thought, I would say it's good where it is right now. But I'll let it run through my head a bit more upside_down_face

Copy link

Member

@xFrednet xFrednet left a comment •

edited

Heyho, thank you for the update and all the additional work you're putting in after my suggestion to use Span-magic sparkles ! I've reviewed your code and found some edge cases. One of them made me wonder if it would be simpler to use a normal Visitor and manually handle the projections etc. See comment I'm not sure if that's the best course of action, though. This was just an idea that popped in my head.

I've now mainly reviewed the seach_is_some.rs file. I've skimmed over the tests, which already looks good, but there might be some NITs in the next review. upside_down_face

ThibsG

force-pushed the find_any_7392

branch 2 times, most recently from db90e2c to 4393aa3 21 days ago

Copy link

Contributor

Author

ThibsG commented 21 days ago

Alright should be good for final review.

I am also looking forward to see this merged into Clippy smiley
Thank you for your valuable guidance and patience ! wink

Copy link

Member

xFrednet commented 20 days ago

edited

Alright should be good for final review.

Fantastic, it's on my to-do list for text week.

Thank you for your valuable guidance and patience ! wink

Same back to you! I wanted to say something similar once this gets merged. Your patience with my reviews has also helped, it looks like we're both happy with how it turns out. I've also learned a lot while reviewing this upside_down_face

Copy link

Member

@xFrednet xFrednet left a comment

Alright, I'm done with an in-depth review. The number of tests in this PR are remarkable and frightening at once. The output looks excellent, though. I marked some code pieces that could be adjusted a bit more (Sorry ^^). And that's it, I hope that this is not too much, and we can get this merged soon! upside_down_face

clippy_utils/src/sugg.rs

Outdated Show resolved

closure_arg_is_double_ref,

next_pos: search_expr.span.lo(),

suggestion_start: String::new(),

applicability: Applicability::MaybeIncorrect,

Reminder @xFrednet, create an issue to set this to Applicability::MashineApplicable in 1.59.0 or 1.60.0

clippy_utils/src/sugg.rs

Outdated Show resolved

clippy_utils/src/sugg.rs

Show resolved

clippy_utils/src/sugg.rs

Outdated Show resolved

clippy_utils/src/sugg.rs

Outdated Show resolved

clippy_utils/src/sugg.rs

Outdated Show resolved

clippy_utils/src/sugg.rs

Outdated Show resolved

Copy link

Member

xFrednet commented 9 days ago

Alright, I believe that this PR is ready to be merged. It has grown quite a bit and I might have missed something, but the tests and previous reviews give me confidence that it works. And even in the worth case, this will only affect the suggestion.

@ThibsG Thank you very much for all the changes and the tremendous amount of tests! This has been the largest PR I've ever reviewed tada

I'll create a new issue to have this suggestion be MachineApplicable in one or two releases.


And now, after 111 comments on this PR, I'm happy to add this one with:

@bors r+

Copy link

Contributor

bors commented 9 days ago

pushpin Commit a8e7fed has been approved by xFrednet

Copy link

Contributor

bors commented 9 days ago

hourglass Testing commit a8e7fed with merge d5d830a...

bors

merged commit d5d830a into

rust-lang:master 9 days ago

5 checks passed

ThibsG

deleted the find_any_7392 branch

9 days ago

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Assignees

xFrednet

Projects

None yet

Milestone

No milestone

Linked issues

Successfully merging this pull request may close these issues.

6 participants

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK