Skip to content

fix: offer 'type_mismatch' some fixes inside macro#21952

Merged
A4-Tacks merged 1 commit intorust-lang:masterfrom
A4-Tacks:macro-call-ty-mismatch-fixes
Apr 6, 2026
Merged

fix: offer 'type_mismatch' some fixes inside macro#21952
A4-Tacks merged 1 commit intorust-lang:masterfrom
A4-Tacks:macro-call-ty-mismatch-fixes

Conversation

@A4-Tacks
Copy link
Copy Markdown
Member

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

  • Supports macro for add_missing_ok_or_some and str_ref_to_owned

Example

macro_rules! identity { ($($t:tt)*) => ($($t)*) }
identity! {
    fn test() -> String {
        "a"$0
    }
}

Before this PR

Invalid trigger range and edit range

After this PR

macro_rules! identity { ($($t:tt)*) => ($($t)*) }
identity! {
    fn test() -> String {
        "a".to_owned()
    }
}

macro_rules! identity { ($($t:tt)*) => ($($t)*) }
identity! {
    fn div(x: i32, y: i32) -> Result<i32, ()> {
        if y == 0 { return Err(()); }
        x / y$0
    }
}

Before this PR

Invalid trigger range and edit range

After this PR

macro_rules! identity { ($($t:tt)*) => ($($t)*) }
identity! {
    fn div(x: i32, y: i32) -> Result<i32, ()> {
        if y == 0 { return Err(()); }
        Ok(x / y)
    }
}

- Supports macro for `add_missing_ok_or_some` and `str_ref_to_owned`

Example
---
```rust
macro_rules! identity { ($($t:tt)*) => ($($t)*) }
identity! {
    fn test() -> String {
        "a"$0
    }
}
```

**Before this PR**

Invalid trigger range and edit range

**After this PR**

```rust
macro_rules! identity { ($($t:tt)*) => ($($t)*) }
identity! {
    fn test() -> String {
        "a".to_owned()
    }
}
```

---

```rust
macro_rules! identity { ($($t:tt)*) => ($($t)*) }
identity! {
    fn div(x: i32, y: i32) -> Result<i32, ()> {
        if y == 0 { return Err(()); }
        x / y$0
    }
}
```

**Before this PR**

Invalid trigger range and edit range

**After this PR**

```rust
macro_rules! identity { ($($t:tt)*) => ($($t)*) }
identity! {
    fn div(x: i32, y: i32) -> Result<i32, ()> {
        if y == 0 { return Err(()); }
        Ok(x / y)
    }
}
```
@rustbot rustbot added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Apr 4, 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.

Need to check the file id, don't offer the fix if it's different from the current file.

);
acc.push(fix("str_ref_to_owned", "Add .to_owned() here", source_change, expr_range));
let edit = TextEdit::insert(range.end(), to_owned);
let source_change = SourceChange::from_text_edit(file_id.file_id(ctx.sema.db), edit);
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.

Here too don't offer the fix if the file is not the current file, otherwise this can change unrelated macro code.

Copy link
Copy Markdown
Member Author

@A4-Tacks A4-Tacks Apr 6, 2026

Choose a reason for hiding this comment

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

otherwise this can change unrelated macro code.

Checking file_id doesn't seem to cure it, if the macro is defined in the same file

Here too don't offer the fix if the file is not the current file

If it's not the current file, the fix seems difficult to apply

Fix is a code-action that is separate from diagnostics. If they are not in the same location, they will not be detected

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.

Checking file_id doesn't seem to cure it, if the macro is defined in the same file

Right, true. It is easier to notice though. You can check if it's from the macro by checking if the SyntaxContext is root.

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.

In diagnostics, this seems to have no precedent

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.

We can set the precedent :) But if you want, you can merge this as-is.

@A4-Tacks A4-Tacks enabled auto-merge April 6, 2026 05:13
@A4-Tacks A4-Tacks added this pull request to the merge queue Apr 6, 2026
Merged via the queue into rust-lang:master with commit c7e4b1a Apr 6, 2026
17 checks passed
@A4-Tacks A4-Tacks deleted the macro-call-ty-mismatch-fixes branch April 6, 2026 15:19
@rustbot rustbot removed the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Apr 6, 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.

3 participants