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

feat(new lint): new lint manual_retain #8972

Merged
merged 10 commits into from
Jun 27, 2022
Merged

Conversation

kyoto7250
Copy link
Contributor

@kyoto7250 kyoto7250 commented Jun 8, 2022

close #8097

This PR is a new lint implementation.
This lint checks if the retain method is available.

Thank you in advance.

changelog: add new [`manual_retain`] lint

@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 Jun 8, 2022
@kyoto7250 kyoto7250 force-pushed the use_retain branch 2 times, most recently from 1f94ae4 to 983c443 Compare June 8, 2022 15:48
@kyoto7250 kyoto7250 marked this pull request as ready for review June 8, 2022 16:01
Copy link
Contributor

@llogiq llogiq left a comment

Choose a reason for hiding this comment

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

This looks pretty good. I only have some small-ish nits regarding code style and one suggestion for extending the test. With those out of the way this is good to merge.

clippy_lints/src/use_retain.rs Outdated Show resolved Hide resolved
/// ```
#[clippy::version = "1.63.0"]
pub USE_RETAIN,
style,
Copy link
Contributor

Choose a reason for hiding this comment

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

I don't think this is a question of style. I'd place it in either performance (because it helps avoid allocating a new container) or complexity (because it simplifies the code) with a small preference to the former.


impl<'tcx> LateLintPass<'tcx> for UseRetain {
fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx hir::Expr<'_>) {
if_chain! {
Copy link
Contributor

Choose a reason for hiding this comment

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

We can now use "let chains". That is we can if let ... && let ... && foo ... without any macro. Just replace the second and following ifs by &&, lose the ; in the end and the then and it should work.

left_expr: &hir::Expr<'_>,
target_expr: &hir::Expr<'_>,
) {
if_chain! {
Copy link
Contributor

Choose a reason for hiding this comment

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

Feel free to also use let chains here.

left_expr: &hir::Expr<'_>,
target_expr: &hir::Expr<'_>,
) {
if_chain! {
Copy link
Contributor

Choose a reason for hiding this comment

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

and here.

left_expr: &hir::Expr<'_>,
target_expr: &hir::Expr<'_>,
) {
if_chain! {
Copy link
Contributor

Choose a reason for hiding this comment

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

need I say more? 😄

}

fn suggest(cx: &LateContext<'_>, parent_expr: &hir::Expr<'_>, left_expr: &hir::Expr<'_>, filter_expr: &hir::Expr<'_>) {
if_chain! {
Copy link
Contributor

Choose a reason for hiding this comment

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

Another one

Comment on lines 223 to 225
return ACCEPTABLE_METHODS
.iter()
.any(|&method| match_def_path(cx, collect_def_id, method));
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
return ACCEPTABLE_METHODS
.iter()
.any(|&method| match_def_path(cx, collect_def_id, method));
ACCEPTABLE_METHODS
.iter()
.any(|&method| match_def_path(cx, collect_def_id, method))

Why wasn't this caught by needless-return!?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I made the bug ticket, I'll check when I have time.

#8978

Comment on lines 230 to 232
return ACCEPTABLE_TYPES
.iter()
.any(|&ty| is_type_diagnostic_item(cx, expr_ty, ty));
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
return ACCEPTABLE_TYPES
.iter()
.any(|&ty| is_type_diagnostic_item(cx, expr_ty, ty));
ACCEPTABLE_TYPES
.iter()
.any(|&ty| is_type_diagnostic_item(cx, expr_ty, ty))

same here

// https://github.com/rust-lang/rust/issues/71503
let mut heap = BinaryHeap::from([1, 2, 3]);
heap = heap.into_iter().filter(|x| x % 2 == 0).collect();
heap = heap.iter().filter(|&x| x % 2 == 0).copied().collect();
Copy link
Contributor

Choose a reason for hiding this comment

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

Perhaps also one case with .cloned() instead of .copied() (allowing the respective lint of course)?

@ghost
Copy link

ghost commented Jun 9, 2022

The name does follow the naming guidelines. It should probably be called something like 'manual_retain' or 'slow retain'.

--

I ran this on the top 1000 (more or less) crates of crates.io.

I got one warning and the fix worked so that is looking good.

---> annotate-snippets-0.9.1/src/display_list/from_snippet.rs:344:9   
warning: this expression can be written more simply using `.retain()` 
   --> src/display_list/from_snippet.rs:344:9
    |
344 | /         annotations = annotations
345 | |             .into_iter()
346 | |             .filter(|annotation| {
347 | |                 let body_idx = idx + annotation_line_count;
...   |
507 | |             })
508 | |             .collect();
    | |______________________^
    |
    = note: requested on the command line with `-W clippy::use-retain`
    = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#use_retain
help: consider calling `.retain()` instead
    |
344 ~         annotations.retain(|annotation| {
345 +                 let body_idx = idx + annotation_line_count;
346 +                 let annotation_type = match annotation.annotation_type {
347 +                     snippet::AnnotationType::Error => DisplayAnnotationType::None,
348 +                     snippet::AnnotationType::Warning => DisplayAnnotationType::None,
349 +                     _ => DisplayAnnotationType::from(annotation.annotation_type),
  ...
annotate-snippets-0.9.1 - fix succeeded

@kyoto7250
Copy link
Contributor Author

@mikerite

The name does follow the naming guidelines. It should probably be called something like 'manual_retain' or 'slow retain'.

yes, I thinkmanual_retain would be better.
I'm going to rename, thanks for letting me know!


I ran this on the top 1000 (more or less) crates of crates.io.
I got one warning and the fix worked so that is looking good.

Thanks for this too!

@kyoto7250
Copy link
Contributor Author

kyoto7250 commented Jun 10, 2022

@llogiq

I added some commits for your suggestions and I changed the lint name to manual_retain.

In addition to, I noticed that this lint needs to check msrv, so I added this commit.

Please let me know if we need git squash :)

@kyoto7250 kyoto7250 requested a review from llogiq June 12, 2022 12:24
@bors
Copy link
Contributor

bors commented Jun 15, 2022

☔ The latest upstream changes (presumably #8964) made this pull request unmergeable. Please resolve the merge conflicts.

@bors
Copy link
Contributor

bors commented Jun 17, 2022

☔ The latest upstream changes (presumably #8989) made this pull request unmergeable. Please resolve the merge conflicts.

@bors
Copy link
Contributor

bors commented Jun 24, 2022

☔ The latest upstream changes (presumably #9031) made this pull request unmergeable. Please resolve the merge conflicts.

@llogiq llogiq changed the title feat(new lint): new lint use_retain feat(new lint): new lint manual_retain Jun 25, 2022
@llogiq
Copy link
Contributor

llogiq commented Jun 25, 2022

Sorry, I had a lot to do this week, so I didn't get to review stuff. This looks good and can be merged after a rebase.

@kyoto7250
Copy link
Contributor Author

kyoto7250 commented Jun 26, 2022

This looks good and can be merged after a rebase.

I rebased latest master.

Do We need a git squash?

Sorry, I had a lot to do this week, so I didn't get to review stuff.

I don't mind.

Thanks again for your reviews :)

@llogiq
Copy link
Contributor

llogiq commented Jun 27, 2022

@bors r+

@bors
Copy link
Contributor

bors commented Jun 27, 2022

📌 Commit 676af45 has been approved by llogiq

@bors
Copy link
Contributor

bors commented Jun 27, 2022

⌛ Testing commit 676af45 with merge eaa03ea...

@bors
Copy link
Contributor

bors commented Jun 27, 2022

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-review Status: Awaiting review from the assignee but also interested parties
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Suggest Vec::retain() in cases of assigning a chain of .filter().collect() to the original variable.
4 participants