Skip to content

fix: check original type for replace_arith_op - #22225

Merged
A4-Tacks merged 2 commits into
rust-lang:masterfrom
A4-Tacks:arith-op-fixup
Aug 11, 2026
Merged

fix: check original type for replace_arith_op#22225
A4-Tacks merged 2 commits into
rust-lang:masterfrom
A4-Tacks:arith-op-fixup

Conversation

@A4-Tacks

@A4-Tacks A4-Tacks commented Apr 30, 2026

Copy link
Copy Markdown
Member

Fixup #22180

The old test did not include minicore, which resulted in the inability to actually apply it

Example

//- minicore: add, builtin_impls
fn main() {
    let mut x = 1;
    x $0+= 2;
}

Before this PR

Assist not applicable

After this PR

fn main() {
    let mut x = 1;
    x = x.saturating_add(2);
}

@rustbot rustbot added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Apr 30, 2026
@A4-Tacks
A4-Tacks force-pushed the arith-op-fixup branch 2 times, most recently from fdb5df7 to 409bab5 Compare May 3, 2026 11:00
@rustbot

This comment has been minimized.

@A4-Tacks
A4-Tacks requested a review from ChayimFriedman2 May 3, 2026 21:39
@A4-Tacks A4-Tacks changed the title minor: check original type for replace_arith_op fix: check original type for replace_arith_op May 6, 2026
@rustbot

This comment has been minimized.

@A4-Tacks

Copy link
Copy Markdown
Member Author

r? @ChayimFriedman2

check_assist(
replace_arith_with_checked,
r#"
//- minicore: add, builtin_impls

@Wilfred Wilfred Aug 11, 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.

Dumb question: shouldn't the test behaviour actually change? Why is it sufficient to to just change the minicore comments here?

View changes since the review

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

struct Foo;
struct Bar;
impl std::ops::AddAssign<i8> for Foo {}
fn main() {
    let foo = Foo;
    let bar = Bar;
    foo += 2;
  //^^^ original: Foo, adjusted: Some(&mut Foo)
    bar += 2;
  //^^^ original: Bar, adjusted: None
}

fn is_primitive_int(ctx: &AssistContext<'_, '_>, expr: &ast::Expr) -> bool {
match ctx.sema.type_of_expr(expr) {
Some(ty) => ty.adjusted().is_int_or_uint(),
Some(ty) => ty.adjusted().strip_references().is_int_or_uint(),

@ChayimFriedman2 ChayimFriedman2 Aug 11, 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
Some(ty) => ty.adjusted().strip_references().is_int_or_uint(),
Some(ty) => ty.adjusted().strip_reference().is_int_or_uint(),

You cannot add &&int to &&int.

View changes since the review

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.

Oh I see this is because adjusted is the input to add() - I think this means we need to take original() instead (but also strip one reference since Add<&int> for &int is implemented). And please add a test for adding references.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I think this means we need to take original()

Are there some strange cases where converting from non-integer or non-integer-references to integers has been rejected

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.

Adjustments can only change due to coercion (besides taking a reference for add()), which is not applicable here.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

And please add a test for adding references.

Does #23109 include these tests?

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.

Yes.

@A4-Tacks
A4-Tacks removed the request for review from Veykril August 11, 2026 12:42
Example
---
```rust
//- minicore: add, builtin_impls
fn main() {
    let mut x = 1;
    x $0+= 2;
}
```

**Before this PR**

Assist not applicable

**After this PR**

```rust
fn main() {
    let mut x = 1;
    x = x.saturating_add(2);
}
```
@rustbot

rustbot commented Aug 11, 2026

Copy link
Copy Markdown
Collaborator

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

@A4-Tacks
A4-Tacks added this pull request to the merge queue Aug 11, 2026
Merged via the queue into rust-lang:master with commit b2d445b Aug 11, 2026
18 checks passed
@A4-Tacks
A4-Tacks deleted the arith-op-fixup branch August 11, 2026 17:12
@rustbot rustbot removed the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Aug 11, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants