Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit d48498f

Browse files
committed
fix: better handling of SelfParam in inline_call
1 parent 7c94c29 commit d48498f

File tree

2 files changed

+49
-6
lines changed

2 files changed

+49
-6
lines changed

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

Lines changed: 46 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ use ide_db::{
1515
};
1616
use itertools::{izip, Itertools};
1717
use syntax::{
18-
ast::{self, edit::IndentLevel, edit_in_place::Indent, HasArgList, PathExpr},
18+
ast::{self, edit::IndentLevel, edit_in_place::Indent, HasArgList, Pat, PathExpr},
1919
ted, AstNode, NodeOrToken, SyntaxKind,
2020
};
2121

@@ -278,7 +278,7 @@ fn get_fn_params(
278278

279279
let mut params = Vec::new();
280280
if let Some(self_param) = param_list.self_param() {
281-
// FIXME this should depend on the receiver as well as the self_param
281+
// Keep `ref` and `mut` and transform them into `&` and `mut` later
282282
params.push((
283283
make::ident_pat(
284284
self_param.amp_token().is_some(),
@@ -409,16 +409,56 @@ fn inline(
409409
let mut let_stmts = Vec::new();
410410

411411
// Inline parameter expressions or generate `let` statements depending on whether inlining works or not.
412-
for ((pat, param_ty, _), usages, expr) in izip!(params, param_use_nodes, arguments) {
412+
for ((pat, param_ty, param), usages, expr) in izip!(params, param_use_nodes, arguments) {
413413
// izip confuses RA due to our lack of hygiene info currently losing us type info causing incorrect errors
414414
let usages: &[ast::PathExpr] = &usages;
415415
let expr: &ast::Expr = expr;
416416

417417
let mut insert_let_stmt = || {
418418
let ty = sema.type_of_expr(expr).filter(TypeInfo::has_adjustment).and(param_ty.clone());
419-
let_stmts.push(
420-
make::let_stmt(pat.clone(), ty, Some(expr.clone())).clone_for_update().into(),
421-
);
419+
420+
let is_self = param
421+
.name(sema.db)
422+
.and_then(|name| name.as_text())
423+
.is_some_and(|name| name == "self");
424+
425+
if is_self {
426+
let mut this_pat = make::ident_pat(false, false, make::name("this"));
427+
let mut expr = expr.clone();
428+
match pat {
429+
Pat::IdentPat(pat) => match (pat.ref_token(), pat.mut_token()) {
430+
// self => let this = obj
431+
(None, None) => {}
432+
// mut self => let mut this = obj
433+
(None, Some(_)) => {
434+
this_pat = make::ident_pat(false, true, make::name("this"));
435+
}
436+
// &self => let this = &obj
437+
(Some(_), None) => {
438+
expr = make::expr_ref(expr, false);
439+
}
440+
// let foo = &mut X; &mut self => let this = &mut obj
441+
// let mut foo = X; &mut self => let this = &mut *obj (reborrow)
442+
(Some(_), Some(_)) => {
443+
let should_reborrow = sema
444+
.type_of_expr(&expr)
445+
.map(|ty| ty.original.is_mutable_reference());
446+
expr = if let Some(true) = should_reborrow {
447+
make::expr_reborrow(expr)
448+
} else {
449+
make::expr_ref(expr, true)
450+
};
451+
}
452+
},
453+
_ => {}
454+
};
455+
let_stmts
456+
.push(make::let_stmt(this_pat.into(), ty, Some(expr)).clone_for_update().into())
457+
} else {
458+
let_stmts.push(
459+
make::let_stmt(pat.clone(), ty, Some(expr.clone())).clone_for_update().into(),
460+
);
461+
}
422462
};
423463

424464
// check if there is a local var in the function that conflicts with parameter

crates/syntax/src/ast/make.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -595,6 +595,9 @@ pub fn expr_macro_call(f: ast::Expr, arg_list: ast::ArgList) -> ast::Expr {
595595
pub fn expr_ref(expr: ast::Expr, exclusive: bool) -> ast::Expr {
596596
expr_from_text(&if exclusive { format!("&mut {expr}") } else { format!("&{expr}") })
597597
}
598+
pub fn expr_reborrow(expr: ast::Expr) -> ast::Expr {
599+
expr_from_text(&format!("&mut *{expr}"))
600+
}
598601
pub fn expr_closure(pats: impl IntoIterator<Item = ast::Param>, expr: ast::Expr) -> ast::Expr {
599602
let params = pats.into_iter().join(", ");
600603
expr_from_text(&format!("|{params}| {expr}"))

0 commit comments

Comments
 (0)