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: redundant_locals #10885

Merged
merged 1 commit into from
Jul 22, 2023
Merged

Conversation

max-niederman
Copy link
Contributor

@max-niederman max-niederman commented Jun 4, 2023

This lint checks for code like the following:

let x = 1;
let x = x;

It checks (afaik) all cases where a binding is shadowed by its own value in the same block, including function parameters. This has no effect and is almost certainly accidental, so it's in the correctness category like self_assignment.

This also lays the groundwork for a more generalized version of #5102.

changelog: new lint: [redundant_local]

@rustbot
Copy link
Collaborator

rustbot commented Jun 4, 2023

Thanks for the pull request, and welcome! The Rust team is excited to review your changes, and you should hear from @Jarcho (or someone else) soon.

Please see the contribution instructions for more information. Namely, in order to ensure the minimum review times lag, PR authors and assigned reviewers should ensure that the review label (S-waiting-on-review and S-waiting-on-author) stays updated, invoking these commands when appropriate:

  • @rustbot author: the review is finished, PR author should check the comments and take action accordingly
  • @rustbot review: the author is ready for a review, this PR will be queued again in the reviewer's queue

@rustbot rustbot added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties label Jun 4, 2023
@max-niederman
Copy link
Contributor Author

max-niederman commented Jun 4, 2023

Okay, so I'm really confused on how the the redundant_locals lint is still being emitted for the popular-crates test even with the Span::from_expansion check. Anybody know why this could happen?

@Centri3
Copy link
Member

Centri3 commented Jun 4, 2023

Okay, so I'm really confused on how the the redundant_locals lint is still being emitted for the popular-crates test even with the Span::from_expansion check. Anybody know why this could happen?

This is likely because clap will itself redefine locals in its generated code (it does this in Args::augment_args, iirc), therefore you'll need to call is_from_proc_macro (which I think will require you to use a LateLintPass lint). afaik Span::from_expansion isn't enough to figure out if something's from a proc macro or not

@max-niederman
Copy link
Contributor Author

@rustbot review

@dswij
Copy link
Member

dswij commented Jun 8, 2023

@max-niederman thanks for the PR!

We follow a no merge-commit policy. Can you help to resolve the conflict by rebasing this to master branch and drop the merge-commit?

@Centri3 Do you want to help review this PR since you've looked at it?

@Centri3
Copy link
Member

Centri3 commented Jun 8, 2023

@Centri3 Do you want to help review this PR since you've looked at it?

I was actually just doing that :D

Copy link
Member

@Centri3 Centri3 left a comment

Choose a reason for hiding this comment

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

Looks pretty good so far, but it misses some redundant locals.

clippy_lints/src/redundant_local.rs Outdated Show resolved Hide resolved
clippy_lints/src/redundant_local.rs Outdated Show resolved Hide resolved
clippy_lints/src/redundant_local.rs Outdated Show resolved Hide resolved
@llogiq
Copy link
Contributor

llogiq commented Jun 8, 2023

Please add a test case with a type-ascribed local (e.g. let x: u8 = x;). Those should not be linted.

@Alexendoo
Copy link
Member

Similarly some test cases for let ref x = x; and let ref mut x = x; would be good

We might need to add a needs_ordered_drop check as removing the let statement can change the drop order, e.g.

let guard = mutex.lock().unwrap();

// ..

{
    let guard = guard;
    // ..
}

// mutex is now unlocked

@max-niederman
Copy link
Contributor Author

@Centri3 @Alexendoo I intentionally wrote the lint to only check redefinitions in the same block in order to prevent changes to the drop order, but I definitely think it would be better to check all redefinitions and use needs_ordered_drop now that I know that's an option. I'm going to rewrite as a late-pass lint and do that.

@max-niederman
Copy link
Contributor Author

I'll also disable linting on type-ascribed locals. Totally forgot about coercion 🤦.

Copy link
Member

@Centri3 Centri3 left a comment

Choose a reason for hiding this comment

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

Looks pretty good, I've left some comments on what can be improved.

clippy_lints/src/redundant_local.rs Outdated Show resolved Hide resolved
clippy_lints/src/redundant_local.rs Outdated Show resolved Hide resolved
clippy_lints/src/redundant_local.rs Outdated Show resolved Hide resolved
@bors
Copy link
Contributor

bors commented Jun 12, 2023

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

@max-niederman
Copy link
Contributor Author

I finally got around to working on this again and I'm pretty sure it's in a good state. It would be good to check for multiple-binding local statements, but that's way harder and definitely suffers from diminishing returns in terms of applicability.

@bors
Copy link
Contributor

bors commented Jun 19, 2023

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

Copy link
Member

@Centri3 Centri3 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 good, thanks! I may do the (x, y) idea as a followup if that's fine with you ^^

@dswij, can you take a look as well?

CHANGELOG.md Outdated Show resolved Hide resolved
clippy_lints/src/redundant_local.rs Outdated Show resolved Hide resolved
tests/ui/redundant_local.rs Outdated Show resolved Hide resolved
@max-niederman max-niederman requested a review from dswij June 19, 2023 21:03
@max-niederman max-niederman changed the title new lint: redundant_local new lint: redundant_locals Jun 21, 2023
Copy link
Member

@Centri3 Centri3 left a comment

Choose a reason for hiding this comment

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

I think this is about ready to be merged :D Just two minor nits, thanks!

tests/ui/redundant_locals.rs Show resolved Hide resolved
clippy_lints/src/redundant_locals.rs Outdated Show resolved Hide resolved
@bors
Copy link
Contributor

bors commented Jun 27, 2023

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

@bors
Copy link
Contributor

bors commented Jul 18, 2023

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

@Centri3
Copy link
Member

Centri3 commented Jul 21, 2023

Once you rebase and add the crate type this is ready :)

@Centri3
Copy link
Member

Centri3 commented Jul 22, 2023

Sorry, also please squash.

fix dogfood lints in `redundant_local`

keep `redundant_local` from running in proc macros

rewrite `redundant_local` as late pass

make redundant_local's `find_binding` more readable

pluralize `redundant_locals` name

add test for `redundant_locals` in macros

test `redundant_locals` in proc macros

use more destructuring in `redundant_locals`

fix: format redundant_locals.rs

ignore needless_pass_by_mut_ref in redundant_locals test
@Centri3
Copy link
Member

Centri3 commented Jul 22, 2023

Thanks! @bors r+

@bors
Copy link
Contributor

bors commented Jul 22, 2023

📌 Commit 008ba2b has been approved by Centri3

It is now in the queue for this repository.

@bors
Copy link
Contributor

bors commented Jul 22, 2023

⌛ Testing commit 008ba2b with merge a44dcf8...

@bors
Copy link
Contributor

bors commented Jul 22, 2023

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

@bors bors merged commit a44dcf8 into rust-lang:master Jul 22, 2023
@max-niederman max-niederman deleted the redundant_local branch July 25, 2023 06:33
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.

9 participants