Skip to content

lint: Avoid unused_parens warning for ref patterns binding an identifier - #165948

Merged
rust-bors[bot] merged 4 commits into
rust-lang:mainfrom
hardikxk:lint/unused-parens
Nov 19, 2026
Merged

rust-bors[bot] merged 4 commits into
rust-lang:mainfrom
hardikxk:lint/unused-parens

Conversation

@hardikxk

@hardikxk hardikxk commented Nov 14, 2026 •

Copy link
Copy Markdown
Contributor

For reference patterns like &(v @ S{a, b}) the compiler emits the unused parens warning. Without the parenthesis the pattern becomes &v @ S{a, b} which makes the expression look a little confusing and it seems like v is being binded by reference.

Add an explicit pattern matcher to catch all such patterns that would involve an identifier binding with any pattern and avoid emitting the unused_parens diagnostic for this.

closes #165636

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Nov 14, 2026
@rustbot

rustbot commented Nov 14, 2026

Copy link
Copy Markdown
Collaborator

r? @folkertdev

rustbot has assigned @folkertdev.
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: compiler
  • compiler expanded to 79 candidates
  • Random selection from 21 candidates

Comment thread compiler/rustc_lint/src/unused.rs Outdated
Comment on lines +949 to +953
&& let Ident(_, _, Some(ref inner_pat)) = pat.kind
&& matches!(
inner_pat.kind,
Struct(..) | Tuple(..) | TupleStruct(..) | Slice(..)
)

@Jules-Bertholet Jules-Bertholet Nov 14, 2026 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Suggested change
&& let Ident(_, _, Some(ref inner_pat)) = pat.kind
&& matches!(
inner_pat.kind,
Struct(..) | Tuple(..) | TupleStruct(..) | Slice(..)
)
&& let Ident(_, _, Some(_)) = pat.kind

Should we perhaps suppress it for all ident @ … patterns?

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.

I suppose it could help. One could argue &v @ w is also a little confusing?

Yea can't think of a case where it might be unnecessary to have parenthesis. Thanks I'll update it then!

@hardikxk hardikxk changed the title lint: Avoid unused_parens warning for ref patterns with compound types lint: Avoid unused_parens warning for ref patterns binding an identifier Nov 14, 2026
@rust-log-analyzer

This comment has been minimized.

Comment on lines +946 to +953
// Avoid linting on patterns like `&(x @ _)`, as without parens `&x @ _`
// it creates confusion and seems like `x` is binding by reference, #165636.
if let Paren(ref pat) = p.kind
&& *pinned == Pinnedness::Not
&& let Ident(_, _, Some(..)) = pat.kind
{
return;
}

@folkertdev folkertdev Nov 16, 2026 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Can you add some test cases, we have a bunch at tests/ui/lint/unused_parens_*.

Also why the special logic for pinned?

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.

Can you add some test cases, we have a bunch at tests/ui/lint/unused_parens_*.

Yes of course!

Also why the special logic for pinned?

The existing pattern matching allows linting on pinned reference patterns and I think something like &pin const (mut _x @ _) doesn't really need parenthesis at all and is just as fine if written as &pin const mut _x @ _ ?
Also the existing tests for pinned reference patterns lints such patterns and warns against using parens. So removing the pinned check would also require modifying those tests. I guess its debatable whether or not parens are helpful for &pin() patterns, personally it feels fine but writing code with a bit of explicit semantic meaning never hurts.

@rustbot rustbot added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Nov 16, 2026
@rustbot

rustbot commented Nov 16, 2026

Copy link
Copy Markdown
Collaborator

Reminder, once the PR becomes ready for a review, use @rustbot ready.

For reference patterns like `&(v @ S{a, b})` the compiler emits the
unused parens warning. Without the parenthesis the pattern becomes
`&v @ S{a, b}` which makes the expression look a little confusing and
it seems like `v` is being binded by reference.

Add an explicit pattern matcher to catch such a pattern that would
involve an identifier binding with a compound type and avoid emitting
the `unused_parens` diagnostics for this.

Signed-off-by: Hardik Kumar <[email protected]>
Signed-off-by: Hardik Kumar <[email protected]>
@rustbot

rustbot commented Nov 17, 2026

Copy link
Copy Markdown
Collaborator

This PR was rebased onto a different main 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.

@hardikxk

Copy link
Copy Markdown
Contributor Author

@rustbot ready

@rustbot rustbot 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 (such as code changes or more information) from the author. labels Nov 17, 2026

@folkertdev folkertdev left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

@rust-bors

rust-bors Bot commented Nov 18, 2026

Copy link
Copy Markdown
Contributor

📌 Commit 4ec3020 has been approved by folkertdev

It is now in the queue for this repository.

@rust-bors rust-bors Bot added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Nov 18, 2026
rust-bors Bot pushed a commit that referenced this pull request Nov 18, 2026
…uwer

Rollup of 11 pull requests

Successful merges:

 - #165913 (MirPatch: reduce new statement insertion complexity)
 - #166081 (test `feature(asm_const_ptr)` with a vtable)
 - #166094 (Chain the spans of qualified paths instead of having a first-to-last scheme )
 - #166113 (Fix dead-code marking dead enum variants as live)
 - #166118 (rustc_target: catch up with LLVM 24 movement on ppc64le bsd data layout)
 - #166155 (windows seek_write: document interaction with append mode)
 - #165935 (Duplicate attribute diagnostic cleanups)
 - #165948 (lint: Avoid unused_parens warning for ref patterns binding an identifier)
 - #166018 (`rustc_mir_build`: de-duplicate `local_temp_lifetime()`)
 - #166111 (Add `cd` alias to `std::env::set_current_dir`)
 - #166157 (Don't recover invalid literals in attribute parsing)
rust-bors Bot pushed a commit that referenced this pull request Nov 19, 2026
…uwer

Rollup of 11 pull requests

Successful merges:

 - #165913 (MirPatch: reduce new statement insertion complexity)
 - #166081 (test `feature(asm_const_ptr)` with a vtable)
 - #166094 (Chain the spans of qualified paths instead of having a first-to-last scheme )
 - #166113 (Fix dead-code marking dead enum variants as live)
 - #166118 (rustc_target: catch up with LLVM 24 movement on ppc64le bsd data layout)
 - #166155 (windows seek_write: document interaction with append mode)
 - #165935 (Duplicate attribute diagnostic cleanups)
 - #165948 (lint: Avoid unused_parens warning for ref patterns binding an identifier)
 - #166018 (`rustc_mir_build`: de-duplicate `local_temp_lifetime()`)
 - #166111 (Add `cd` alias to `std::env::set_current_dir`)
 - #166157 (Don't recover invalid literals in attribute parsing)
@rust-bors
rust-bors Bot merged commit 4f64560 into rust-lang:main Nov 19, 2026
14 checks passed
@rustbot rustbot added this to the 1.102.0 milestone Nov 19, 2026
rust-bors Bot pushed a commit that referenced this pull request Nov 19, 2026
Rollup merge of #165948 - hardikxk:lint/unused-parens, r=folkertdev

lint: Avoid unused_parens warning for ref patterns binding an identifier

For reference patterns like `&(v @ S{a, b})` the compiler emits the unused parens warning. Without the parenthesis the pattern becomes `&v @ S{a, b}` which makes the expression look a little confusing and it seems like `v` is being binded by reference.

Add an explicit pattern matcher to catch all such patterns that would involve an identifier binding with any pattern and avoid emitting the `unused_parens` diagnostic for this.

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

Labels

S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

unused_parens should probably not trigger for &(v @ ...) for clarity

5 participants