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 any() not taking reference in search_is_some lint #7463

Merged
merged 25 commits into from
Dec 2, 2021

Conversation

ThibsG
Copy link
Contributor

@ThibsG ThibsG commented Jul 14, 2021

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

@rust-highfive
Copy link

r? @Manishearth

(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 Jul 14, 2021
Copy link
Member

@Manishearth Manishearth left a comment

Choose a reason for hiding this comment

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

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

clippy_lints/src/methods/search_is_some.rs Outdated Show resolved Hide resolved
@Manishearth
Copy link
Member

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?

@camsteffen
Copy link
Contributor

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.

@ThibsG
Copy link
Contributor Author

ThibsG commented Jul 19, 2021

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?

@camsteffen
Copy link
Contributor

For me, explicit deref is removed here:

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

tests/ui/search_is_some.stderr Outdated Show resolved Hide resolved
@Manishearth
Copy link
Member

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!

@xFrednet
Copy link
Member

Sure, I can pick this PR up! 🙃

I'll have a quick scan over it now and then try to do the proper review tomorrow 🙃. 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

Choose a reason for hiding this comment

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

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. 👍

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. 🙃

clippy_lints/src/methods/search_is_some.rs Outdated Show resolved Hide resolved
clippy_lints/src/methods/search_is_some.rs Outdated Show resolved Hide resolved
clippy_lints/src/methods/search_is_some.rs Outdated Show resolved Hide resolved
clippy_lints/src/methods/search_is_some.rs Outdated Show resolved Hide resolved
clippy_lints/src/methods/search_is_some.rs Outdated Show resolved Hide resolved
clippy_lints/src/methods/search_is_some.rs Outdated Show resolved Hide resolved
tests/ui/search_is_some.rs Outdated Show resolved Hide resolved
@xFrednet xFrednet 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 Aug 16, 2021
Copy link
Member

@xFrednet xFrednet left a comment

Choose a reason for hiding this comment

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

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 ✨ 😉

// 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 Aug 25, 2021

Choose a reason for hiding this comment

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

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 🙃

Copy link
Member

Choose a reason for hiding this comment

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

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 🙃

Copy link
Member

Choose a reason for hiding this comment

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

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 🤔

clippy_lints/src/methods/search_is_some.rs Outdated Show resolved Hide resolved
clippy_lints/src/methods/search_is_some.rs Outdated Show resolved Hide resolved
clippy_lints/src/methods/search_is_some.rs Outdated Show resolved Hide resolved
clippy_lints/src/methods/search_is_some.rs Outdated Show resolved Hide resolved
clippy_lints/src/methods/search_is_some.rs Outdated Show resolved Hide resolved
clippy_lints/src/methods/search_is_some.rs Outdated Show resolved Hide resolved
clippy_lints/src/methods/search_is_some.rs Outdated Show resolved Hide resolved
tests/ui/search_is_some_fixable_none.rs Show resolved Hide resolved
clippy_lints/src/methods/search_is_some.rs Outdated Show resolved Hide resolved
@ThibsG
Copy link
Contributor Author

ThibsG commented Sep 7, 2021

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

Choose a reason for hiding this comment

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

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

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
#![allow(dead_code)]
#![warn(clippy::search_is_some)]

fn please(x: &u32) -> bool {
    *x == 9
}

fn main() {
    let x = 19;
    let ppx: &u32 = &x;
    let _ = [ppx].iter().find(|ppp_x: &&&u32| please(**ppp_x)).is_some();
    // Lint suggestion:  any(|ppp_x: &&&u32| please(&ppp_x))
    // - The closure type is wrong, this is something I also missed until now.
    // - The suggestion to replace the two derefs with on ref is also suspicius

    // The applied suggestion with the correct type:
    let _ = [ppx].iter().any(|ppp_x: &&u32| please(&ppp_x));
    // This triggers `clippy::needless_borrow` and suggests only using `ppp_x`,
    // while this would be okay, it would be nicer if we can avoid this

    let _ = [String::from("Hey hey")].iter().find(|s| s.len() == 2).is_some();
    // Lint suggestion:                      any(|s| &s.len() == 2)
    // - The first argument of a method call expression is the caller in this case s.
}

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

clippy_lints/src/methods/search_is_some.rs Outdated Show resolved Hide resolved
// 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

Choose a reason for hiding this comment

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

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 🙃

@xFrednet
Copy link
Member

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 🙃

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

@xFrednet xFrednet left a comment

Choose a reason for hiding this comment

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

Heyho, thank you for the update and all the additional work you're putting in after my suggestion to use Span-magic ✨ ! 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. 🙃

clippy_lints/src/methods/search_is_some.rs Outdated Show resolved Hide resolved
clippy_lints/src/methods/search_is_some.rs Outdated Show resolved Hide resolved
clippy_lints/src/methods/search_is_some.rs Outdated Show resolved Hide resolved
clippy_lints/src/methods/search_is_some.rs Outdated Show resolved Hide resolved
clippy_lints/src/methods/search_is_some.rs Outdated Show resolved Hide resolved
tests/ui/search_is_some_fixable_none.rs Show resolved Hide resolved
@xFrednet xFrednet removed the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties label Sep 15, 2021
@ThibsG ThibsG force-pushed the find_any_7392 branch 2 times, most recently from db90e2c to 4393aa3 Compare November 20, 2021 14:42
@ThibsG
Copy link
Contributor Author

ThibsG commented Nov 20, 2021

Alright should be good for final review.

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

@xFrednet
Copy link
Member

xFrednet commented Nov 21, 2021

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 ! 😉

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 🙃

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

@xFrednet xFrednet left a comment

Choose a reason for hiding this comment

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

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! 🙃

clippy_utils/src/sugg.rs Outdated Show resolved Hide resolved
closure_arg_is_double_ref,
next_pos: search_expr.span.lo(),
suggestion_start: String::new(),
applicability: Applicability::MaybeIncorrect,
Copy link
Member

Choose a reason for hiding this comment

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

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 Hide resolved
clippy_utils/src/sugg.rs Show resolved Hide resolved
clippy_utils/src/sugg.rs Outdated Show resolved Hide resolved
clippy_utils/src/sugg.rs Outdated Show resolved Hide resolved
clippy_utils/src/sugg.rs Outdated Show resolved Hide resolved
clippy_utils/src/sugg.rs Outdated Show resolved Hide resolved
@xFrednet xFrednet 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 Nov 27, 2021
@xFrednet
Copy link
Member

xFrednet commented Dec 2, 2021

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 🎉

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+

@bors
Copy link
Contributor

bors commented Dec 2, 2021

📌 Commit a8e7fed has been approved by xFrednet

@bors
Copy link
Contributor

bors commented Dec 2, 2021

⌛ Testing commit a8e7fed with merge d5d830a...

@bors
Copy link
Contributor

bors commented Dec 2, 2021

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

@bors bors merged commit d5d830a into rust-lang:master Dec 2, 2021
@ThibsG ThibsG deleted the find_any_7392 branch December 2, 2021 18:13
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.

clippy fix fails: warning: failed to automatically apply fixes suggested by rustc to crate
6 participants