Skip to content

Commit 3449222

Browse files
committed
fix: self type replacement in inline-function
1 parent 94af6c6 commit 3449222

File tree

1 file changed

+63
-11
lines changed

1 file changed

+63
-11
lines changed

crates/ide-assists/src/handlers/inline_call.rs

Lines changed: 63 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -315,17 +315,6 @@ fn inline(
315315
} else {
316316
fn_body.clone_for_update()
317317
};
318-
if let Some(imp) = body.syntax().ancestors().find_map(ast::Impl::cast) {
319-
if !node.syntax().ancestors().any(|anc| &anc == imp.syntax()) {
320-
if let Some(t) = imp.self_ty() {
321-
body.syntax()
322-
.descendants_with_tokens()
323-
.filter_map(NodeOrToken::into_token)
324-
.filter(|tok| tok.kind() == SyntaxKind::SELF_TYPE_KW)
325-
.for_each(|tok| ted::replace(tok, t.syntax()));
326-
}
327-
}
328-
}
329318
let usages_for_locals = |local| {
330319
Definition::Local(local)
331320
.usages(sema)
@@ -381,6 +370,25 @@ fn inline(
381370
}
382371
}
383372

373+
// We should place the following code after last usage of `usages_for_locals`
374+
// because `ted::replace` will change the offset in syntax tree, which makes
375+
// `FileReference` incorrect
376+
if let Some(imp) = body.syntax().ancestors().find_map(ast::Impl::cast) {
377+
if !node.syntax().ancestors().any(|anc| &anc == imp.syntax()) {
378+
if let Some(t) = imp.self_ty() {
379+
while let Some(self_tok) = body
380+
.syntax()
381+
.descendants_with_tokens()
382+
.filter_map(NodeOrToken::into_token)
383+
.find(|tok| tok.kind() == SyntaxKind::SELF_TYPE_KW)
384+
{
385+
let replace_with = t.clone_subtree().syntax().clone_for_update();
386+
ted::replace(self_tok, replace_with);
387+
}
388+
}
389+
}
390+
}
391+
384392
let mut func_let_vars: BTreeSet<String> = BTreeSet::new();
385393

386394
// grab all of the local variable declarations in the function
@@ -1510,4 +1518,48 @@ fn main() {
15101518
"#,
15111519
);
15121520
}
1521+
1522+
#[test]
1523+
fn inline_call_with_multiple_self_types_eq() {
1524+
check_assist(
1525+
inline_call,
1526+
r#"
1527+
#[derive(PartialEq, Eq)]
1528+
enum Enum {
1529+
A,
1530+
B,
1531+
}
1532+
1533+
impl Enum {
1534+
fn a_or_b_eq(&self) -> bool {
1535+
self == &Self::A || self == &Self::B
1536+
}
1537+
}
1538+
1539+
fn a() -> bool {
1540+
Enum::A.$0a_or_b_eq()
1541+
}
1542+
"#,
1543+
r#"
1544+
#[derive(PartialEq, Eq)]
1545+
enum Enum {
1546+
A,
1547+
B,
1548+
}
1549+
1550+
impl Enum {
1551+
fn a_or_b_eq(&self) -> bool {
1552+
self == &Self::A || self == &Self::B
1553+
}
1554+
}
1555+
1556+
fn a() -> bool {
1557+
{
1558+
let ref this = Enum::A;
1559+
this == &Enum::A || this == &Enum::B
1560+
}
1561+
}
1562+
"#,
1563+
)
1564+
}
15131565
}

0 commit comments

Comments
 (0)