Skip to content

Keep opening '(' when suggesting to remove '&' from pattern - #166235

Merged
rust-bors[bot] merged 1 commit into
rust-lang:mainfrom
BartSimpson001:fix-166198
Nov 27, 2026
Merged

rust-bors[bot] merged 1 commit into
rust-lang:mainfrom
BartSimpson001:fix-166198

Conversation

@BartSimpson001

@BartSimpson001 BartSimpson001 commented Nov 20, 2026 •

Copy link
Copy Markdown

View all comments

Fixes #166198

Explanation

When encountering a parenthesized pattern such as &(mut num), PatKind::Paren is stripped during AST-to-HIR lowering, meaning inner.span starts at mut num.

Previously, pat.span.until(inner.span) spanned from & across (, replacing &( with "" and leaving an unmatched closing parenthesis ) (e.g., suggesting |mut num)|).

This PR ensures the suggestion span stops before any opening parenthesis (, properly suggesting |(mut num)|.

@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 20, 2026
@rustbot

rustbot commented Nov 20, 2026

Copy link
Copy Markdown
Collaborator

Thanks for the pull request, and welcome! The Rust Project has assigned @camelid (or someone else) to review your changes, you should hear from them (or someone else) within the next two weeks.

Please see the contribution instructions and our LLM policy for more information.

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 20 candidates

@rustbot

This comment has been minimized.

@rust-log-analyzer

This comment has been minimized.

@rust-log-analyzer

This comment has been minimized.

@BartSimpson001

Copy link
Copy Markdown
Author

Hi @camelid,
The CI test failures were caused by existing tests (pattern-errors.rs and well-typed-edition-2024.rs) whose .stderr files previously expected the buggy
unclosed parenthesis (mut x)). With this fix, they now correctly suggest(mut│ x). I have verified the tests locally and blessed the affected .stderr files

@Kivooeo Kivooeo 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.

This PR as far as I see will break suggestions with comments like this:

fn main() {
    let mut nums = vec![2, 3, 5];
    nums.iter_mut().for_each(|& /* ( */ num| {});
}

View changes since this review

@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 20, 2026
@rustbot

rustbot commented Nov 20, 2026

Copy link
Copy Markdown
Collaborator

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

@BartSimpson001

Copy link
Copy Markdown
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 21, 2026
@BartSimpson001

Copy link
Copy Markdown
Author

Thanks @Kivooeo! I've updated the implementation to scan for ( while skipping any line or block comments (including nested comments), so comments containing parentheses won't be mistaken for pattern parentheses. I also added your example as a test case in the UI test suite.

@rustbot

This comment has been minimized.

@rustbot

This comment has been minimized.

@rustbot rustbot added has-merge-commits PR has merge commits, merge with caution. S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Nov 21, 2026
@Kivooeo

Kivooeo commented Nov 21, 2026

Copy link
Copy Markdown
Member

Hi again, I was testing you PR locally and did something terrible with git, can you please try to resolve it (I think you'd need to remove last commit I added), just because I don't want to make anything even worse ^^ I also found a new bug in your code but let's first resolve this

@rustbot

rustbot commented Nov 21, 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.

@rustbot

rustbot commented Nov 21, 2026

Copy link
Copy Markdown
Collaborator

⚠️ Warning ⚠️

@rustbot rustbot removed has-merge-commits PR has merge commits, merge with caution. S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Nov 21, 2026
@BartSimpson001

Copy link
Copy Markdown
Author

Thanks @Kivooeo!

That was indeed caused by the inner comment loop not updating next, which caused it to miss */ and consequently miss the ( following it. With next properly updated on each iteration inside the inner loop, it now correctly suggests (mut num). I've added your test case to remove-ref-paren-issue-166198.rs and pushed the update.

@BartSimpson001

Copy link
Copy Markdown
Author

@rustbot ready

@estebank

Copy link
Copy Markdown
Contributor

I think you can simplify the logic: only extend the span if the snippet for the span between them is all whitespace, otherwise only remove the &. The result would be always syntactically correct.

@BartSimpson001

Copy link
Copy Markdown
Author

Thanks @estebank! That simplified the logic a lot. I've updated the implementation to only extend the span if the remaining text after & is all whitespace, and otherwise only remove the &. All test cases are passing cleanly now.

@camelid camelid assigned Kivooeo and unassigned camelid Nov 23, 2026
@BartSimpson001

BartSimpson001 commented Nov 26, 2026 •

Copy link
Copy Markdown
Author

@Kivooeo Updated the code as suggested,All test cases are passing now. Ready for review.

@@ -1470,8 +1469,27 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
}
hir::Node::Param(_) | hir::Node::Arm(_) | hir::Node::Pat(_) => {
// rely on match ergonomics or it might be nested `&&pat`
let sugg_span =

@estebank estebank Nov 26, 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.

When doing this kind of thing it is a good idea to add a comment explaining why the code is there, something like "opportunistically make a span containing all the whitespace that would need to be removed, leaving behind only the span for the & on its own as a fallback".

View changes since the review

@estebank

Copy link
Copy Markdown
Contributor

@bors r+

@rust-bors

rust-bors Bot commented Nov 26, 2026

Copy link
Copy Markdown
Contributor

📌 Commit 0caf53c has been approved by estebank

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 26, 2026
JonathanBrouwer added a commit to JonathanBrouwer/rust that referenced this pull request Nov 26, 2026
Keep opening '(' when suggesting to remove '&' from pattern

Fixes rust-lang#166198

## Explanation

When encountering a parenthesized pattern such as &(mut num), PatKind::Paren is stripped during AST-to-HIR lowering, meaning inner.span starts at mut num.

Previously, pat.span.until(inner.span) spanned from `&` across `(`, replacing `&(` with `""` and leaving an unmatched closing parenthesis `)` (e.g., suggesting `|mut num)|`).

This PR ensures the suggestion span stops before any opening parenthesis `(`, properly suggesting `|(mut num)|`.
JonathanBrouwer added a commit to JonathanBrouwer/rust that referenced this pull request Nov 26, 2026
Keep opening '(' when suggesting to remove '&' from pattern

Fixes rust-lang#166198

## Explanation

When encountering a parenthesized pattern such as &(mut num), PatKind::Paren is stripped during AST-to-HIR lowering, meaning inner.span starts at mut num.

Previously, pat.span.until(inner.span) spanned from `&` across `(`, replacing `&(` with `""` and leaving an unmatched closing parenthesis `)` (e.g., suggesting `|mut num)|`).

This PR ensures the suggestion span stops before any opening parenthesis `(`, properly suggesting `|(mut num)|`.
rust-bors Bot pushed a commit that referenced this pull request Nov 26, 2026
…uwer

Rollup of 14 pull requests

Successful merges:

 - #166235 (Keep opening '(' when suggesting to remove '&' from pattern)
 - #166516 (Fix ICE in iterator method suggestion when the receiver is a block)
 - #166586 (abby test dsl: one binder to rule them all)
 - #164289 (Add regression test for self-referential GAT with missing lifetime argument)
 - #165859 (Add codegen test for MaybeUninit array built in the return slot)
 - #166092 (add fake borrow tests for divergent match guards)
 - #166292 (Don't suggest closure braces for errors inside the body)
 - #166435 (Encode direct const mir)
 - #166447 (Document allocator behaviour of BTreeMap::append and BTreeSet::append)
 - #166558 (triagebot: Send "in progress" message when nomination is in progress pt. 2)
 - #166574 (bump libffi)
 - #166584 (Arg pos const mismatch ice)
 - #166599 (add alias-bound test)
 - #166606 (llvm-wrapper: Pass LibraryOptionsContext to computeDeadSymbolsWithConstProp etc.)

Failed merges:

 - #166583 (abby dsl tests: the passing test should check-pass)
JonathanBrouwer added a commit to JonathanBrouwer/rust that referenced this pull request Nov 26, 2026
Keep opening '(' when suggesting to remove '&' from pattern

Fixes rust-lang#166198

## Explanation

When encountering a parenthesized pattern such as &(mut num), PatKind::Paren is stripped during AST-to-HIR lowering, meaning inner.span starts at mut num.

Previously, pat.span.until(inner.span) spanned from `&` across `(`, replacing `&(` with `""` and leaving an unmatched closing parenthesis `)` (e.g., suggesting `|mut num)|`).

This PR ensures the suggestion span stops before any opening parenthesis `(`, properly suggesting `|(mut num)|`.
rust-bors Bot pushed a commit that referenced this pull request Nov 26, 2026
…uwer

Rollup of 17 pull requests

Successful merges:

 - #165572 (Run the `FatLtoCleanup` pass to lower `llvm.type.checked.load`)
 - #166235 (Keep opening '(' when suggesting to remove '&' from pattern)
 - #166516 (Fix ICE in iterator method suggestion when the receiver is a block)
 - #166586 (abby test dsl: one binder to rule them all)
 - #166604 (fix: embed-source-dwarf test with remapped debuginfo)
 - #166620 (Change wording of overwrite span in `unused_assignments` lint)
 - #164289 (Add regression test for self-referential GAT with missing lifetime argument)
 - #165859 (Add codegen test for MaybeUninit array built in the return slot)
 - #166092 (add fake borrow tests for divergent match guards)
 - #166292 (Don't suggest closure braces for errors inside the body)
 - #166435 (Encode direct const mir)
 - #166447 (Document allocator behaviour of BTreeMap::append and BTreeSet::append)
 - #166558 (triagebot: Send "in progress" message when nomination is in progress pt. 2)
 - #166584 (Arg pos const mismatch ice)
 - #166599 (add alias-bound test)
 - #166606 (llvm-wrapper: Pass LibraryOptionsContext to computeDeadSymbolsWithConstProp etc.)
 - #166623 (Correct feature name for `is_cased()` method)

Failed merges:

 - #166583 (abby dsl tests: the passing test should check-pass)
rust-bors Bot pushed a commit that referenced this pull request Nov 27, 2026
Rollup of 20 pull requests

Successful merges:

 - #164161 (`-Znext-solver` Support a bit more types for rustdoc auto traits)
 - #165572 (Run the `FatLtoCleanup` pass to lower `llvm.type.checked.load`)
 - #166235 (Keep opening '(' when suggesting to remove '&' from pattern)
 - #166335 (A few fixes in x86 std_detect)
 - #166516 (Fix ICE in iterator method suggestion when the receiver is a block)
 - #166586 (abby test dsl: one binder to rule them all)
 - #166604 (fix: embed-source-dwarf test with remapped debuginfo)
 - #166620 (Change wording of overwrite span in `unused_assignments` lint)
 - #166626 (Remove some outdated comments about None terminator)
 - #164289 (Add regression test for self-referential GAT with missing lifetime argument)
 - #165859 (Add codegen test for MaybeUninit array built in the return slot)
 - #166092 (add fake borrow tests for divergent match guards)
 - #166292 (Don't suggest closure braces for errors inside the body)
 - #166435 (Encode direct const mir)
 - #166447 (Document allocator behaviour of BTreeMap::append and BTreeSet::append)
 - #166558 (triagebot: Send "in progress" message when nomination is in progress pt. 2)
 - #166584 (Arg pos const mismatch ice)
 - #166599 (add alias-bound test)
 - #166606 (llvm-wrapper: Pass LibraryOptionsContext to computeDeadSymbolsWithConstProp etc.)
 - #166623 (Correct feature name for `is_cased()` method)

Failed merges:

 - #166583 (abby dsl tests: the passing test should check-pass)
@rust-bors
rust-bors Bot merged commit 4afdad1 into rust-lang:main Nov 27, 2026
14 checks passed
@rustbot rustbot added this to the 1.102.0 milestone Nov 27, 2026
rust-bors Bot pushed a commit that referenced this pull request Nov 27, 2026
Rollup merge of #166235 - BartSimpson001:fix-166198, r=estebank

Keep opening '(' when suggesting to remove '&' from pattern

Fixes #166198

## Explanation

When encountering a parenthesized pattern such as &(mut num), PatKind::Paren is stripped during AST-to-HIR lowering, meaning inner.span starts at mut num.

Previously, pat.span.until(inner.span) spanned from `&` across `(`, replacing `&(` with `""` and leaving an unmatched closing parenthesis `)` (e.g., suggesting `|mut num)|`).

This PR ensures the suggestion span stops before any opening parenthesis `(`, properly suggesting `|(mut num)|`.
@Kivooeo Kivooeo assigned estebank and unassigned Kivooeo Nov 27, 2026
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.

Remove unnecessary parentheses when suggesting to remove '&' from a pattern

7 participants