Repository navigation
Keep opening '(' when suggesting to remove '&' from pattern - #166235
Conversation
|
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:
|
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
dfa7303 to
fc7cbc8
Compare
This comment has been minimized.
This comment has been minimized.
fc7cbc8 to
15d92e2
Compare
|
Hi @camelid, |
|
Reminder, once the PR becomes ready for a review, use |
15d92e2 to
d7d2636
Compare
|
@rustbot ready |
|
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. |
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
|
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 |
a835d30 to
d7d2636
Compare
|
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. |
|
82c95ee to
a7b1aa1
Compare
|
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. |
|
@rustbot ready |
|
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. |
a7b1aa1 to
0caf53c
Compare
|
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. |
|
@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 = | |||
There was a problem hiding this comment.
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".
|
@bors r+ |
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)|`.
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)|`.
…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)
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)|`.
…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)
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)
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)|`.
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)|.