Skip to content

extend needless_borrowed_reference to lint mutable ref patterns - #17012

Open
Gri-ffin wants to merge 2 commits into
rust-lang:masterfrom
Gri-ffin:fix-16022
Open

extend needless_borrowed_reference to lint mutable ref patterns#17012
Gri-ffin wants to merge 2 commits into
rust-lang:masterfrom
Gri-ffin:fix-16022

Conversation

@Gri-ffin

@Gri-ffin Gri-ffin commented May 14, 2026

Copy link
Copy Markdown
Contributor

closes #16022

The lint should now also fire over &mut ref mut patterns when the matched reference can be moved instead of reborrowed. This is a bit conservative for now as it only handles cases where:

  • the scrutinee is a simple local variable,
  • the local is not used after the enclosing match/if let,
  • and the pattern is not part of an or pattern or behind another reference pattern

There may be other valid cases but this should cover the straightforward ones without risking incorrect suggestions or complexities.

changelog: [needless_borrowed_reference] now also lints certain &mut ref mut patterns

@rustbot rustbot added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties label May 14, 2026
@rustbot

rustbot commented May 14, 2026

Copy link
Copy Markdown
Collaborator

r? @samueltardieu

rustbot has assigned @samueltardieu.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

Why was this reviewer chosen?

The reviewer was selected based on:

  • Owners of files modified in this PR: 7 candidates
  • 7 candidates expanded to 7 candidates
  • Random selection from Jarcho, llogiq, samueltardieu

@Gri-ffin

Gri-ffin commented May 14, 2026

Copy link
Copy Markdown
Contributor Author

forgot to run dogfood, fixed.

@blyxyas

blyxyas commented Aug 13, 2026

Copy link
Copy Markdown
Member

r? @blyxyas

@rustbot rustbot assigned blyxyas and unassigned samueltardieu Aug 13, 2026
@rustbot

This comment has been minimized.

@blyxyas blyxyas left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

How does this interact with type inference? Is there any scenario in which removing a &ref mut could change the inferred type, and thus, cause a compiler error?

Everything else looks good! It's just that little doubt !! (ㅇㅅㅇ❀)

View changes since this review

@Gri-ffin

Copy link
Copy Markdown
Contributor Author

This shouldn't change inferred binding types AFAIK, the lint is conservative here and the it should only fire when the type is already known and the move is valid. I'll add an inference focused test to be safe.

@rustbot

rustbot commented Aug 28, 2026

Copy link
Copy Markdown
Collaborator

This PR was rebased onto a different master commit. Here's a range-diff highlighting what actually changed.

Rebasing is a normal part of keeping PRs up to date, so no action is needed—this note is just to help reviewers.

Comment on lines +151 to +155
fn should_not_lint_mut_ref_drop(x: WithDrop<'_>) {
if let Some(&mut ref mut x) = x.0 {
*x = 0;
}
}

@blyxyas blyxyas Aug 28, 2026

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Just a question, where are we checking for that significant drop?

View changes since the review

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

It's not checked explicitly, we only allow a bare local as the scrutinee, so it bails before drop-ness even matters.

@blyxyas blyxyas left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Looking good, just a little question ❤️

View changes since this review

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.

needless_borrowed_reference does not handle mutable references

4 participants