Fix incorrect Self path expand for inline_call

Example
---
```rust
trait Trait {
    fn f() -> Self;
}
struct Foo<'a>(&'a ());
impl<'a> Trait for Foo<'a> {
    fn f() -> Self { Self(&()) }
}
impl Foo<'_> {
    fn new() -> Self {
        Self::$0f()
    }
}
```

**Before this PR**

```rust
trait Trait {
    fn f() -> Self;
}
struct Foo<'a>(&'a ());
impl<'a> Trait for Foo<'a> {
    fn f() -> Self { Self(&()) }
}
impl Foo<'_> {
    fn new() -> Self {
        Foo<'a>(&())
    }
}
```

**After this PR**

```rust
trait Trait {
    fn f() -> Self;
}
struct Foo<'a>(&'a ());
impl<'a> Trait for Foo<'a> {
    fn f() -> Self { Self(&()) }
}
impl Foo<'_> {
    fn new() -> Self {
        Foo(&())
    }
}
```
This commit is contained in:
A4-Tacks 2026-01-01 22:21:57 +08:00
parent 6d0f23cbdc
commit e79fef4086
No known key found for this signature in database
GPG key ID: 9E63F956E66DD9C7

View file

@ -403,6 +403,12 @@ fn inline(
.find(|tok| tok.kind() == SyntaxKind::SELF_TYPE_KW)
{
let replace_with = t.clone_subtree().syntax().clone_for_update();
if !is_in_type_path(&self_tok)
&& let Some(ty) = ast::Type::cast(replace_with.clone())
&& let Some(generic_arg_list) = ty.generic_arg_list()
{
ted::remove(generic_arg_list.syntax());
}
ted::replace(self_tok, replace_with);
}
}
@ -588,6 +594,17 @@ fn inline(
}
}
fn is_in_type_path(self_tok: &syntax::SyntaxToken) -> bool {
self_tok
.parent_ancestors()
.skip_while(|it| !ast::Path::can_cast(it.kind()))
.map_while(ast::Path::cast)
.last()
.and_then(|it| it.syntax().parent())
.and_then(ast::PathType::cast)
.is_some()
}
fn path_expr_as_record_field(usage: &PathExpr) -> Option<ast::RecordExprField> {
let path = usage.path()?;
let name_ref = path.as_single_name_ref()?;
@ -1694,6 +1711,41 @@ fn main() {
)
}
#[test]
fn inline_trait_method_call_with_lifetimes() {
check_assist(
inline_call,
r#"
trait Trait {
fn f() -> Self;
}
struct Foo<'a>(&'a ());
impl<'a> Trait for Foo<'a> {
fn f() -> Self { Self(&()) }
}
impl Foo<'_> {
fn new() -> Self {
Self::$0f()
}
}
"#,
r#"
trait Trait {
fn f() -> Self;
}
struct Foo<'a>(&'a ());
impl<'a> Trait for Foo<'a> {
fn f() -> Self { Self(&()) }
}
impl Foo<'_> {
fn new() -> Self {
Foo(&())
}
}
"#,
)
}
#[test]
fn method_by_reborrow() {
check_assist(