Skip to content

Fix/replace arith neg pow - #23236

Open
ksh368-bit wants to merge 2 commits into
rust-lang:masterfrom
ksh368-bit:fix/replace-arith-neg-pow
Open

Fix/replace arith neg pow#23236
ksh368-bit wants to merge 2 commits into
rust-lang:masterfrom
ksh368-bit:fix/replace-arith-neg-pow

Conversation

@ksh368-bit

@ksh368-bit ksh368-bit commented Aug 26, 2026

Copy link
Copy Markdown

Closes #23153

This PR extends the existing Replace arithmetic assist to support:

  1. unary negation on signed integers
  2. inherent integer .pow() calls

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:

  1. unsigned negation
  2. minimum-value literals such as -128i8
  3. direct macro operands that may hide such literals
  4. trait-provided .pow() methods
  5. primitive type names shadowed by user-defined types
    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:

  1. 20 related tests pass
  2. all ide-assists tests pass: 2819 passed, 3 ignored
  3. cargo fmt passes
  4. Clippy passes with -D warnings

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.

@rustbot rustbot added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Aug 26, 2026
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.

The replace arithmetic assist should be offered on negation and .pow()

2 participants