Skip to content

Commit 62c3107

Browse files
committed
Auto merge of rust-lang#13061 - ice1k:master, r=Veykril
feat: Improved inline_call to replace `Self` Fixes rust-lang#13060
2 parents a2ad3d0 + 5a6c51e commit 62c3107

File tree

1 file changed

+34
-1
lines changed

1 file changed

+34
-1
lines changed

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

+34-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use ide_db::{
1313
use itertools::{izip, Itertools};
1414
use syntax::{
1515
ast::{self, edit_in_place::Indent, HasArgList, PathExpr},
16-
ted, AstNode,
16+
ted, AstNode, NodeOrToken, SyntaxKind,
1717
};
1818

1919
use crate::{
@@ -311,6 +311,13 @@ fn inline(
311311
} else {
312312
fn_body.clone_for_update()
313313
};
314+
if let Some(t) = body.syntax().ancestors().find_map(ast::Impl::cast).and_then(|i| i.self_ty()) {
315+
body.syntax()
316+
.descendants_with_tokens()
317+
.filter_map(NodeOrToken::into_token)
318+
.filter(|tok| tok.kind() == SyntaxKind::SELF_TYPE_KW)
319+
.for_each(|tok| ted::replace(tok, t.syntax()));
320+
}
314321
let usages_for_locals = |local| {
315322
Definition::Local(local)
316323
.usages(sema)
@@ -345,6 +352,7 @@ fn inline(
345352
}
346353
})
347354
.collect();
355+
348356
if function.self_param(sema.db).is_some() {
349357
let this = || make::name_ref("this").syntax().clone_for_update();
350358
if let Some(self_local) = params[0].2.as_local(sema.db) {
@@ -1188,6 +1196,31 @@ fn bar() -> u32 {
11881196
x
11891197
}
11901198
}
1199+
"#,
1200+
)
1201+
}
1202+
1203+
#[test]
1204+
fn inline_call_with_self_type() {
1205+
check_assist(
1206+
inline_call,
1207+
r#"
1208+
struct A(u32);
1209+
impl A {
1210+
fn f() -> Self { Self(114514) }
1211+
}
1212+
fn main() {
1213+
A::f$0();
1214+
}
1215+
"#,
1216+
r#"
1217+
struct A(u32);
1218+
impl A {
1219+
fn f() -> Self { Self(114514) }
1220+
}
1221+
fn main() {
1222+
A(114514);
1223+
}
11911224
"#,
11921225
)
11931226
}

0 commit comments

Comments
 (0)