Fix/replace arith neg pow - #23236
Open
ksh368-bit wants to merge 2 commits into
Open
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #23153
This PR extends the existing Replace arithmetic assist to support:
For example, the wrapping variant transforms:
'-x' into 'i32::wrapping_neg(x)'
'x.pow(e) into 'x.wrapping_pow(e)'
References are explicitly dereferenced when needed:
" -r // r: &i32"
" r.pow(e) // r: &&i32"
becomes:
"i32::wrapping_neg(*r)"
"(**r).wrapping_pow(e)"
The same applies to the checked, saturating, and strict variants.
Implementation notes
Unary negation uses primitive-qualified UFCS such as:
"i32::wrapping_neg(x)"
instead of x.wrapping_neg() because integer literals may still have an ambiguous receiver type during method lookup.
The implementation uses localized syntax edits to preserve existing comments and formatting.
The assist is not offered for cases where the rewrite could be invalid or unintended, including:
The assist also selects only the nearest arithmetic expression at the cursor.
Tests
Added coverage for type inference, references, minimum-value literals, macros, type-name shadowing, comments, trait .pow() methods, and expression selection.
Validation:
AI assistance
I used an AI coding assistant to help investigate the existing implementation and consider edge cases. I reviewed and modified the suggestions myself and verified the final implementation with test, formatting, and Clippy.