fix: offer 'type_mismatch' some fixes inside macro#21952
fix: offer 'type_mismatch' some fixes inside macro#21952A4-Tacks merged 1 commit intorust-lang:masterfrom
Conversation
- 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)
}
}
```
There was a problem hiding this comment.
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); |
There was a problem hiding this comment.
Here too don't offer the fix if the file is not the current file, otherwise this can change unrelated macro code.
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
In diagnostics, this seems to have no precedent
There was a problem hiding this comment.
We can set the precedent :) But if you want, you can merge this as-is.
add_missing_ok_or_someandstr_ref_to_ownedExample
Before this PR
Invalid trigger range and edit range
After this PR
Before this PR
Invalid trigger range and edit range
After this PR