2021-01-01 23:47:35 -06:00
|
|
|
use ast::make;
|
2021-09-25 11:39:43 -05:00
|
|
|
use hir::{db::HirDatabase, HasSource, PathResolution, Semantics, TypeInfo};
|
|
|
|
use ide_db::{
|
2021-09-26 07:38:58 -05:00
|
|
|
base_db::{FileId, FileRange},
|
|
|
|
defs::Definition,
|
|
|
|
path_transform::PathTransform,
|
|
|
|
search::{FileReference, SearchScope},
|
2021-09-25 11:39:43 -05:00
|
|
|
RootDatabase,
|
|
|
|
};
|
2021-07-03 13:05:00 -05:00
|
|
|
use itertools::izip;
|
2021-01-01 23:47:35 -06:00
|
|
|
use syntax::{
|
2021-08-24 10:18:43 -05:00
|
|
|
ast::{self, edit_in_place::Indent, ArgListOwner},
|
2021-09-25 11:39:43 -05:00
|
|
|
ted, AstNode, SyntaxNode,
|
2021-01-01 23:47:35 -06:00
|
|
|
};
|
|
|
|
|
|
|
|
use crate::{
|
|
|
|
assist_context::{AssistContext, Assists},
|
|
|
|
AssistId, AssistKind,
|
|
|
|
};
|
|
|
|
|
2021-09-25 11:39:43 -05:00
|
|
|
// Assist: inline_into_callers
|
|
|
|
//
|
|
|
|
// Inline a function or method body into all of its callers where possible, creating a `let` statement per parameter
|
|
|
|
// unless the parameter can be inlined. The parameter will be inlined either if it the supplied argument is a simple local
|
|
|
|
// or if the parameter is only accessed inside the function body once.
|
|
|
|
// If all calls can be inlined the function will be removed.
|
|
|
|
//
|
|
|
|
// ```
|
|
|
|
// fn print(_: &str) {}
|
|
|
|
// fn foo$0(word: &str) {
|
|
|
|
// if !word.is_empty() {
|
|
|
|
// print(word);
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
// fn bar() {
|
|
|
|
// foo("안녕하세요");
|
|
|
|
// foo("여러분");
|
|
|
|
// }
|
|
|
|
// ```
|
|
|
|
// ->
|
|
|
|
// ```
|
|
|
|
// fn print(_: &str) {}
|
|
|
|
//
|
|
|
|
// fn bar() {
|
|
|
|
// {
|
|
|
|
// let word = "안녕하세요";
|
|
|
|
// if !word.is_empty() {
|
|
|
|
// print(word);
|
|
|
|
// }
|
|
|
|
// };
|
|
|
|
// {
|
|
|
|
// let word = "여러분";
|
|
|
|
// if !word.is_empty() {
|
|
|
|
// print(word);
|
|
|
|
// }
|
|
|
|
// };
|
|
|
|
// }
|
|
|
|
// ```
|
|
|
|
pub(crate) fn inline_into_callers(acc: &mut Assists, ctx: &AssistContext) -> Option<()> {
|
2021-09-26 07:38:58 -05:00
|
|
|
let def_file = ctx.frange.file_id;
|
2021-09-25 11:39:43 -05:00
|
|
|
let name = ctx.find_node_at_offset::<ast::Name>()?;
|
2021-09-26 07:38:58 -05:00
|
|
|
let ast_func = name.syntax().parent().and_then(ast::Fn::cast)?;
|
|
|
|
let func_body = ast_func.body()?;
|
|
|
|
let param_list = ast_func.param_list()?;
|
|
|
|
|
|
|
|
let function = ctx.sema.to_def(&ast_func)?;
|
|
|
|
|
2021-09-25 11:39:43 -05:00
|
|
|
let params = get_fn_params(ctx.sema.db, function, ¶m_list)?;
|
|
|
|
|
|
|
|
let usages = Definition::ModuleDef(hir::ModuleDef::Function(function)).usages(&ctx.sema);
|
|
|
|
if !usages.at_least_one() {
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
|
2021-09-26 07:38:58 -05:00
|
|
|
let is_recursive_fn = usages
|
|
|
|
.clone()
|
|
|
|
.in_scope(SearchScope::file_range(FileRange {
|
|
|
|
file_id: def_file,
|
|
|
|
range: func_body.syntax().text_range(),
|
|
|
|
}))
|
|
|
|
.at_least_one();
|
|
|
|
if is_recursive_fn {
|
|
|
|
cov_mark::hit!(inline_into_callers_recursive);
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
|
2021-09-25 11:39:43 -05:00
|
|
|
acc.add(
|
|
|
|
AssistId("inline_into_callers", AssistKind::RefactorInline),
|
|
|
|
"Inline into all callers",
|
|
|
|
name.syntax().text_range(),
|
|
|
|
|builder| {
|
|
|
|
let mut usages = usages.all();
|
|
|
|
let current_file_usage = usages.references.remove(&def_file);
|
|
|
|
|
2021-09-26 07:38:58 -05:00
|
|
|
let mut remove_def = true;
|
|
|
|
let mut inline_refs_for_file = |file_id, refs: Vec<FileReference>| {
|
2021-09-25 11:39:43 -05:00
|
|
|
builder.edit_file(file_id);
|
|
|
|
let count = refs.len();
|
|
|
|
let name_refs = refs.into_iter().filter_map(|file_ref| match file_ref.name {
|
|
|
|
ast::NameLike::NameRef(name_ref) => Some(name_ref),
|
|
|
|
_ => None,
|
|
|
|
});
|
|
|
|
let call_infos = name_refs.filter_map(|name_ref| {
|
|
|
|
let parent = name_ref.syntax().parent()?;
|
|
|
|
if let Some(call) = ast::MethodCallExpr::cast(parent.clone()) {
|
|
|
|
let receiver = call.receiver()?;
|
|
|
|
let mut arguments = vec![receiver];
|
|
|
|
arguments.extend(call.arg_list()?.args());
|
|
|
|
Some(CallInfo {
|
|
|
|
generic_arg_list: call.generic_arg_list(),
|
|
|
|
node: CallExprNode::MethodCallExpr(call),
|
|
|
|
arguments,
|
|
|
|
})
|
|
|
|
} else if let Some(segment) = ast::PathSegment::cast(parent) {
|
|
|
|
let path = segment.syntax().parent().and_then(ast::Path::cast)?;
|
|
|
|
let path = path.syntax().parent().and_then(ast::PathExpr::cast)?;
|
|
|
|
let call = path.syntax().parent().and_then(ast::CallExpr::cast)?;
|
|
|
|
|
|
|
|
Some(CallInfo {
|
|
|
|
arguments: call.arg_list()?.args().collect(),
|
|
|
|
node: CallExprNode::Call(call),
|
|
|
|
generic_arg_list: segment.generic_arg_list(),
|
|
|
|
})
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
});
|
|
|
|
let replaced = call_infos
|
|
|
|
.map(|call_info| {
|
|
|
|
let replacement =
|
|
|
|
inline(&ctx.sema, def_file, function, &func_body, ¶ms, &call_info);
|
|
|
|
|
|
|
|
builder.replace_ast(
|
|
|
|
match call_info.node {
|
|
|
|
CallExprNode::Call(it) => ast::Expr::CallExpr(it),
|
|
|
|
CallExprNode::MethodCallExpr(it) => ast::Expr::MethodCallExpr(it),
|
|
|
|
},
|
|
|
|
replacement,
|
|
|
|
);
|
|
|
|
})
|
|
|
|
.count();
|
2021-09-26 07:38:58 -05:00
|
|
|
remove_def &= replaced == count;
|
2021-09-25 11:39:43 -05:00
|
|
|
};
|
|
|
|
for (file_id, refs) in usages.into_iter() {
|
2021-09-26 07:38:58 -05:00
|
|
|
inline_refs_for_file(file_id, refs);
|
2021-09-25 11:39:43 -05:00
|
|
|
}
|
|
|
|
if let Some(refs) = current_file_usage {
|
2021-09-26 07:38:58 -05:00
|
|
|
inline_refs_for_file(def_file, refs);
|
2021-09-25 11:39:43 -05:00
|
|
|
} else {
|
|
|
|
builder.edit_file(def_file);
|
|
|
|
}
|
2021-09-26 07:38:58 -05:00
|
|
|
if remove_def {
|
|
|
|
builder.delete(ast_func.syntax().text_range());
|
2021-09-25 11:39:43 -05:00
|
|
|
}
|
|
|
|
},
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2021-07-03 10:13:56 -05:00
|
|
|
// Assist: inline_call
|
2021-07-02 18:33:34 -05:00
|
|
|
//
|
2021-07-05 08:34:01 -05:00
|
|
|
// Inlines a function or method body creating a `let` statement per parameter unless the parameter
|
|
|
|
// can be inlined. The parameter will be inlined either if it the supplied argument is a simple local
|
|
|
|
// or if the parameter is only accessed inside the function body once.
|
2021-01-01 23:47:35 -06:00
|
|
|
//
|
|
|
|
// ```
|
2021-07-05 07:24:25 -05:00
|
|
|
// # //- minicore: option
|
|
|
|
// fn foo(name: Option<&str>) {
|
|
|
|
// let name = name.unwrap$0();
|
2021-01-01 23:47:35 -06:00
|
|
|
// }
|
|
|
|
// ```
|
|
|
|
// ->
|
|
|
|
// ```
|
2021-07-05 07:24:25 -05:00
|
|
|
// fn foo(name: Option<&str>) {
|
|
|
|
// let name = match name {
|
|
|
|
// Some(val) => val,
|
|
|
|
// None => panic!("called `Option::unwrap()` on a `None` value"),
|
|
|
|
// };
|
2021-01-01 23:47:35 -06:00
|
|
|
// }
|
|
|
|
// ```
|
2021-07-03 10:13:56 -05:00
|
|
|
pub(crate) fn inline_call(acc: &mut Assists, ctx: &AssistContext) -> Option<()> {
|
2021-09-25 11:39:43 -05:00
|
|
|
let (label, function, call_info) =
|
2021-07-03 10:13:56 -05:00
|
|
|
if let Some(path_expr) = ctx.find_node_at_offset::<ast::PathExpr>() {
|
2021-09-25 11:39:43 -05:00
|
|
|
// FIXME make applicable only on nameref
|
2021-07-03 10:13:56 -05:00
|
|
|
let call = path_expr.syntax().parent().and_then(ast::CallExpr::cast)?;
|
|
|
|
let path = path_expr.path()?;
|
|
|
|
|
|
|
|
let function = match ctx.sema.resolve_path(&path)? {
|
|
|
|
PathResolution::Def(hir::ModuleDef::Function(f))
|
|
|
|
| PathResolution::AssocItem(hir::AssocItem::Function(f)) => f,
|
|
|
|
_ => return None,
|
|
|
|
};
|
|
|
|
(
|
|
|
|
format!("Inline `{}`", path),
|
|
|
|
function,
|
2021-09-25 11:39:43 -05:00
|
|
|
CallInfo {
|
|
|
|
arguments: call.arg_list()?.args().collect(),
|
|
|
|
node: CallExprNode::Call(call),
|
|
|
|
generic_arg_list: path.segment().and_then(|it| it.generic_arg_list()),
|
|
|
|
},
|
2021-07-03 10:13:56 -05:00
|
|
|
)
|
|
|
|
} else {
|
|
|
|
let name_ref: ast::NameRef = ctx.find_node_at_offset()?;
|
|
|
|
let call = name_ref.syntax().parent().and_then(ast::MethodCallExpr::cast)?;
|
|
|
|
let receiver = call.receiver()?;
|
|
|
|
let function = ctx.sema.resolve_method_call(&call)?;
|
|
|
|
let mut arguments = vec![receiver];
|
|
|
|
arguments.extend(call.arg_list()?.args());
|
2021-08-10 07:39:56 -05:00
|
|
|
(
|
|
|
|
format!("Inline `{}`", name_ref),
|
|
|
|
function,
|
2021-09-25 11:39:43 -05:00
|
|
|
CallInfo {
|
|
|
|
generic_arg_list: call.generic_arg_list(),
|
|
|
|
node: CallExprNode::MethodCallExpr(call),
|
|
|
|
arguments,
|
|
|
|
},
|
2021-08-10 07:39:56 -05:00
|
|
|
)
|
2021-07-03 10:13:56 -05:00
|
|
|
};
|
|
|
|
|
2021-09-26 07:38:58 -05:00
|
|
|
let fn_source = function.source(ctx.db())?;
|
|
|
|
let fn_body = fn_source.value.body()?;
|
|
|
|
let param_list = fn_source.value.param_list()?;
|
2021-09-25 11:39:43 -05:00
|
|
|
|
2021-09-26 07:38:58 -05:00
|
|
|
let FileRange { file_id, range } = fn_source.syntax().original_file_range(ctx.sema.db);
|
|
|
|
if file_id == ctx.frange.file_id && range.contains(ctx.frange.range.start()) {
|
|
|
|
cov_mark::hit!(inline_call_recursive);
|
|
|
|
return None;
|
|
|
|
}
|
2021-09-25 11:39:43 -05:00
|
|
|
let params = get_fn_params(ctx.sema.db, function, ¶m_list)?;
|
|
|
|
|
|
|
|
if call_info.arguments.len() != params.len() {
|
|
|
|
// Can't inline the function because they've passed the wrong number of
|
|
|
|
// arguments to this function
|
|
|
|
cov_mark::hit!(inline_call_incorrect_number_of_arguments);
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
|
|
|
|
let syntax = call_info.node.syntax().clone();
|
|
|
|
acc.add(
|
|
|
|
AssistId("inline_call", AssistKind::RefactorInline),
|
|
|
|
label,
|
|
|
|
syntax.text_range(),
|
|
|
|
|builder| {
|
|
|
|
let replacement = inline(&ctx.sema, file_id, function, &fn_body, ¶ms, &call_info);
|
|
|
|
|
|
|
|
builder.replace_ast(
|
|
|
|
match call_info.node {
|
|
|
|
CallExprNode::Call(it) => ast::Expr::CallExpr(it),
|
|
|
|
CallExprNode::MethodCallExpr(it) => ast::Expr::MethodCallExpr(it),
|
|
|
|
},
|
|
|
|
replacement,
|
|
|
|
);
|
|
|
|
},
|
|
|
|
)
|
2021-07-02 18:33:34 -05:00
|
|
|
}
|
2021-01-01 23:47:35 -06:00
|
|
|
|
2021-09-25 11:39:43 -05:00
|
|
|
enum CallExprNode {
|
|
|
|
Call(ast::CallExpr),
|
|
|
|
MethodCallExpr(ast::MethodCallExpr),
|
|
|
|
}
|
|
|
|
|
|
|
|
impl CallExprNode {
|
|
|
|
fn syntax(&self) -> &SyntaxNode {
|
|
|
|
match self {
|
|
|
|
CallExprNode::Call(it) => it.syntax(),
|
|
|
|
CallExprNode::MethodCallExpr(it) => it.syntax(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
struct CallInfo {
|
|
|
|
node: CallExprNode,
|
|
|
|
arguments: Vec<ast::Expr>,
|
2021-08-10 07:39:56 -05:00
|
|
|
generic_arg_list: Option<ast::GenericArgList>,
|
2021-09-25 11:39:43 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
fn get_fn_params(
|
|
|
|
db: &dyn HirDatabase,
|
|
|
|
function: hir::Function,
|
|
|
|
param_list: &ast::ParamList,
|
|
|
|
) -> Option<Vec<(ast::Pat, Option<ast::Type>, hir::Param)>> {
|
|
|
|
let mut assoc_fn_params = function.assoc_fn_params(db).into_iter();
|
2021-01-01 23:47:35 -06:00
|
|
|
|
2021-07-02 18:33:34 -05:00
|
|
|
let mut params = Vec::new();
|
|
|
|
if let Some(self_param) = param_list.self_param() {
|
|
|
|
// FIXME this should depend on the receiver as well as the self_param
|
2021-07-03 13:05:00 -05:00
|
|
|
params.push((
|
2021-07-02 18:33:34 -05:00
|
|
|
make::ident_pat(
|
|
|
|
self_param.amp_token().is_some(),
|
|
|
|
self_param.mut_token().is_some(),
|
|
|
|
make::name("this"),
|
|
|
|
)
|
|
|
|
.into(),
|
2021-07-10 12:16:32 -05:00
|
|
|
None,
|
2021-07-03 13:05:00 -05:00
|
|
|
assoc_fn_params.next()?,
|
|
|
|
));
|
2021-07-02 18:33:34 -05:00
|
|
|
}
|
|
|
|
for param in param_list.params() {
|
2021-07-10 12:16:32 -05:00
|
|
|
params.push((param.pat()?, param.ty(), assoc_fn_params.next()?));
|
2021-07-02 18:33:34 -05:00
|
|
|
}
|
|
|
|
|
2021-09-25 11:39:43 -05:00
|
|
|
Some(params)
|
|
|
|
}
|
2021-07-03 13:05:00 -05:00
|
|
|
|
2021-09-25 11:39:43 -05:00
|
|
|
fn inline(
|
|
|
|
sema: &Semantics<RootDatabase>,
|
|
|
|
function_def_file_id: FileId,
|
|
|
|
function: hir::Function,
|
|
|
|
fn_body: &ast::BlockExpr,
|
|
|
|
params: &[(ast::Pat, Option<ast::Type>, hir::Param)],
|
|
|
|
CallInfo { node, arguments, generic_arg_list }: &CallInfo,
|
|
|
|
) -> ast::Expr {
|
|
|
|
let body = fn_body.clone_for_update();
|
|
|
|
let usages_for_locals = |local| {
|
|
|
|
Definition::Local(local)
|
|
|
|
.usages(&sema)
|
|
|
|
.all()
|
|
|
|
.references
|
|
|
|
.remove(&function_def_file_id)
|
|
|
|
.unwrap_or_default()
|
|
|
|
.into_iter()
|
|
|
|
};
|
|
|
|
let param_use_nodes: Vec<Vec<_>> = params
|
|
|
|
.iter()
|
|
|
|
.map(|(pat, _, param)| {
|
|
|
|
if !matches!(pat, ast::Pat::IdentPat(pat) if pat.is_simple_ident()) {
|
|
|
|
return Vec::new();
|
2021-07-03 13:05:00 -05:00
|
|
|
}
|
2021-09-25 11:39:43 -05:00
|
|
|
usages_for_locals(param.as_local(sema.db))
|
|
|
|
.map(|FileReference { name, range, .. }| match name {
|
|
|
|
ast::NameLike::NameRef(_) => body
|
|
|
|
.syntax()
|
|
|
|
.covering_element(range)
|
|
|
|
.ancestors()
|
|
|
|
.nth(3)
|
|
|
|
.and_then(ast::PathExpr::cast),
|
|
|
|
_ => None,
|
|
|
|
})
|
|
|
|
.collect::<Option<Vec<_>>>()
|
|
|
|
.unwrap_or_default()
|
|
|
|
})
|
|
|
|
.collect();
|
|
|
|
if function.self_param(sema.db).is_some() {
|
|
|
|
let this = || make::name_ref("this").syntax().clone_for_update();
|
|
|
|
usages_for_locals(params[0].2.as_local(sema.db))
|
|
|
|
.flat_map(|FileReference { name, range, .. }| match name {
|
|
|
|
ast::NameLike::NameRef(_) => Some(body.syntax().covering_element(range)),
|
|
|
|
_ => None,
|
|
|
|
})
|
|
|
|
.for_each(|it| {
|
|
|
|
ted::replace(it, &this());
|
|
|
|
})
|
|
|
|
}
|
|
|
|
// Inline parameter expressions or generate `let` statements depending on whether inlining works or not.
|
|
|
|
for ((pat, param_ty, _), usages, expr) in izip!(params, param_use_nodes, arguments).rev() {
|
|
|
|
let expr_is_name_ref = matches!(&expr,
|
|
|
|
ast::Expr::PathExpr(expr)
|
|
|
|
if expr.path().and_then(|path| path.as_single_name_ref()).is_some()
|
|
|
|
);
|
|
|
|
match &*usages {
|
|
|
|
// inline single use closure arguments
|
|
|
|
[usage]
|
|
|
|
if matches!(expr, ast::Expr::ClosureExpr(_))
|
|
|
|
&& usage.syntax().parent().and_then(ast::Expr::cast).is_some() =>
|
2021-07-10 12:16:32 -05:00
|
|
|
{
|
2021-09-25 11:39:43 -05:00
|
|
|
cov_mark::hit!(inline_call_inline_closure);
|
|
|
|
let expr = make::expr_paren(expr.clone());
|
|
|
|
ted::replace(usage.syntax(), expr.syntax().clone_for_update());
|
|
|
|
}
|
|
|
|
// inline single use literals
|
|
|
|
[usage] if matches!(expr, ast::Expr::Literal(_)) => {
|
|
|
|
cov_mark::hit!(inline_call_inline_literal);
|
|
|
|
ted::replace(usage.syntax(), expr.syntax().clone_for_update());
|
2021-07-02 18:33:34 -05:00
|
|
|
}
|
2021-09-25 11:39:43 -05:00
|
|
|
// inline direct local arguments
|
|
|
|
[_, ..] if expr_is_name_ref => {
|
|
|
|
cov_mark::hit!(inline_call_inline_locals);
|
|
|
|
usages.into_iter().for_each(|usage| {
|
|
|
|
ted::replace(usage.syntax(), &expr.syntax().clone_for_update());
|
|
|
|
});
|
|
|
|
}
|
|
|
|
// cant inline, emit a let statement
|
|
|
|
_ => {
|
|
|
|
let ty =
|
|
|
|
sema.type_of_expr(expr).filter(TypeInfo::has_adjustment).and(param_ty.clone());
|
|
|
|
body.push_front(
|
|
|
|
make::let_stmt(pat.clone(), ty, Some(expr.clone())).clone_for_update().into(),
|
2021-08-10 07:39:56 -05:00
|
|
|
)
|
|
|
|
}
|
2021-09-25 11:39:43 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if let Some(generic_arg_list) = generic_arg_list.clone() {
|
|
|
|
PathTransform::function_call(
|
|
|
|
&sema.scope(node.syntax()),
|
|
|
|
&sema.scope(fn_body.syntax()),
|
|
|
|
function,
|
|
|
|
generic_arg_list,
|
|
|
|
)
|
|
|
|
.apply(body.syntax());
|
|
|
|
}
|
2021-07-03 13:05:00 -05:00
|
|
|
|
2021-09-25 11:39:43 -05:00
|
|
|
let original_indentation = match node {
|
|
|
|
CallExprNode::Call(it) => it.indent_level(),
|
|
|
|
CallExprNode::MethodCallExpr(it) => it.indent_level(),
|
|
|
|
};
|
|
|
|
body.reindent_to(original_indentation);
|
2021-07-03 13:05:00 -05:00
|
|
|
|
2021-09-25 11:39:43 -05:00
|
|
|
match body.tail_expr() {
|
|
|
|
Some(expr) if body.statements().next().is_none() => expr,
|
|
|
|
_ => ast::Expr::BlockExpr(body),
|
|
|
|
}
|
2021-01-01 23:47:35 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use crate::tests::{check_assist, check_assist_not_applicable};
|
|
|
|
|
|
|
|
use super::*;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn no_args_or_return_value_gets_inlined_without_block() {
|
|
|
|
check_assist(
|
2021-07-03 10:13:56 -05:00
|
|
|
inline_call,
|
2021-01-01 23:47:35 -06:00
|
|
|
r#"
|
|
|
|
fn foo() { println!("Hello, World!"); }
|
|
|
|
fn main() {
|
2021-01-06 14:15:48 -06:00
|
|
|
fo$0o();
|
2021-01-01 23:47:35 -06:00
|
|
|
}
|
|
|
|
"#,
|
|
|
|
r#"
|
|
|
|
fn foo() { println!("Hello, World!"); }
|
|
|
|
fn main() {
|
2021-07-03 13:05:00 -05:00
|
|
|
{ println!("Hello, World!"); };
|
2021-01-01 23:47:35 -06:00
|
|
|
}
|
|
|
|
"#,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2021-07-05 06:37:40 -05:00
|
|
|
#[test]
|
|
|
|
fn not_applicable_when_incorrect_number_of_parameters_are_provided() {
|
|
|
|
cov_mark::check!(inline_call_incorrect_number_of_arguments);
|
|
|
|
check_assist_not_applicable(
|
|
|
|
inline_call,
|
|
|
|
r#"
|
|
|
|
fn add(a: u32, b: u32) -> u32 { a + b }
|
|
|
|
fn main() { let x = add$0(42); }
|
|
|
|
"#,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2021-01-01 23:47:35 -06:00
|
|
|
#[test]
|
|
|
|
fn args_with_side_effects() {
|
|
|
|
check_assist(
|
2021-07-03 10:13:56 -05:00
|
|
|
inline_call,
|
2021-01-01 23:47:35 -06:00
|
|
|
r#"
|
2021-07-05 06:37:40 -05:00
|
|
|
fn foo(name: String) {
|
|
|
|
println!("Hello, {}!", name);
|
|
|
|
}
|
2021-01-01 23:47:35 -06:00
|
|
|
fn main() {
|
2021-01-06 14:15:48 -06:00
|
|
|
foo$0(String::from("Michael"));
|
2021-01-01 23:47:35 -06:00
|
|
|
}
|
|
|
|
"#,
|
|
|
|
r#"
|
2021-07-05 06:37:40 -05:00
|
|
|
fn foo(name: String) {
|
|
|
|
println!("Hello, {}!", name);
|
|
|
|
}
|
2021-01-01 23:47:35 -06:00
|
|
|
fn main() {
|
2021-07-05 06:37:40 -05:00
|
|
|
{
|
|
|
|
let name = String::from("Michael");
|
|
|
|
println!("Hello, {}!", name);
|
|
|
|
};
|
2021-01-01 23:47:35 -06:00
|
|
|
}
|
|
|
|
"#,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn function_with_multiple_statements() {
|
|
|
|
check_assist(
|
2021-07-03 10:13:56 -05:00
|
|
|
inline_call,
|
2021-01-01 23:47:35 -06:00
|
|
|
r#"
|
|
|
|
fn foo(a: u32, b: u32) -> u32 {
|
|
|
|
let x = a + b;
|
|
|
|
let y = x - b;
|
|
|
|
x * y
|
|
|
|
}
|
|
|
|
|
|
|
|
fn main() {
|
2021-01-06 14:15:48 -06:00
|
|
|
let x = foo$0(1, 2);
|
2021-01-01 23:47:35 -06:00
|
|
|
}
|
|
|
|
"#,
|
|
|
|
r#"
|
|
|
|
fn foo(a: u32, b: u32) -> u32 {
|
|
|
|
let x = a + b;
|
|
|
|
let y = x - b;
|
|
|
|
x * y
|
|
|
|
}
|
|
|
|
|
|
|
|
fn main() {
|
2021-07-05 06:37:40 -05:00
|
|
|
let x = {
|
|
|
|
let b = 2;
|
2021-07-03 13:05:00 -05:00
|
|
|
let x = 1 + b;
|
2021-01-01 23:47:35 -06:00
|
|
|
let y = x - b;
|
|
|
|
x * y
|
|
|
|
};
|
|
|
|
}
|
2021-07-02 18:33:34 -05:00
|
|
|
"#,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn function_with_self_param() {
|
|
|
|
check_assist(
|
2021-07-03 10:13:56 -05:00
|
|
|
inline_call,
|
2021-07-02 18:33:34 -05:00
|
|
|
r#"
|
|
|
|
struct Foo(u32);
|
|
|
|
|
|
|
|
impl Foo {
|
|
|
|
fn add(self, a: u32) -> Self {
|
|
|
|
Foo(self.0 + a)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn main() {
|
|
|
|
let x = Foo::add$0(Foo(3), 2);
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
r#"
|
|
|
|
struct Foo(u32);
|
|
|
|
|
|
|
|
impl Foo {
|
|
|
|
fn add(self, a: u32) -> Self {
|
|
|
|
Foo(self.0 + a)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn main() {
|
2021-07-05 09:33:45 -05:00
|
|
|
let x = {
|
|
|
|
let this = Foo(3);
|
|
|
|
Foo(this.0 + 2)
|
|
|
|
};
|
2021-07-02 18:33:34 -05:00
|
|
|
}
|
|
|
|
"#,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn method_by_val() {
|
|
|
|
check_assist(
|
2021-07-03 10:13:56 -05:00
|
|
|
inline_call,
|
2021-07-02 18:33:34 -05:00
|
|
|
r#"
|
|
|
|
struct Foo(u32);
|
|
|
|
|
|
|
|
impl Foo {
|
|
|
|
fn add(self, a: u32) -> Self {
|
|
|
|
Foo(self.0 + a)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn main() {
|
|
|
|
let x = Foo(3).add$0(2);
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
r#"
|
|
|
|
struct Foo(u32);
|
|
|
|
|
|
|
|
impl Foo {
|
|
|
|
fn add(self, a: u32) -> Self {
|
|
|
|
Foo(self.0 + a)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn main() {
|
2021-07-05 09:33:45 -05:00
|
|
|
let x = {
|
|
|
|
let this = Foo(3);
|
|
|
|
Foo(this.0 + 2)
|
|
|
|
};
|
2021-07-02 18:33:34 -05:00
|
|
|
}
|
|
|
|
"#,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn method_by_ref() {
|
|
|
|
check_assist(
|
2021-07-03 10:13:56 -05:00
|
|
|
inline_call,
|
2021-07-02 18:33:34 -05:00
|
|
|
r#"
|
|
|
|
struct Foo(u32);
|
|
|
|
|
|
|
|
impl Foo {
|
|
|
|
fn add(&self, a: u32) -> Self {
|
|
|
|
Foo(self.0 + a)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn main() {
|
|
|
|
let x = Foo(3).add$0(2);
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
r#"
|
|
|
|
struct Foo(u32);
|
|
|
|
|
|
|
|
impl Foo {
|
|
|
|
fn add(&self, a: u32) -> Self {
|
|
|
|
Foo(self.0 + a)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn main() {
|
2021-07-05 06:37:40 -05:00
|
|
|
let x = {
|
|
|
|
let ref this = Foo(3);
|
2021-07-03 13:05:00 -05:00
|
|
|
Foo(this.0 + 2)
|
2021-07-02 18:33:34 -05:00
|
|
|
};
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn method_by_ref_mut() {
|
|
|
|
check_assist(
|
2021-07-03 10:13:56 -05:00
|
|
|
inline_call,
|
2021-07-02 18:33:34 -05:00
|
|
|
r#"
|
|
|
|
struct Foo(u32);
|
|
|
|
|
|
|
|
impl Foo {
|
|
|
|
fn clear(&mut self) {
|
|
|
|
self.0 = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn main() {
|
|
|
|
let mut foo = Foo(3);
|
|
|
|
foo.clear$0();
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
r#"
|
|
|
|
struct Foo(u32);
|
|
|
|
|
|
|
|
impl Foo {
|
|
|
|
fn clear(&mut self) {
|
|
|
|
self.0 = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn main() {
|
|
|
|
let mut foo = Foo(3);
|
2021-07-05 06:37:40 -05:00
|
|
|
{
|
|
|
|
let ref mut this = foo;
|
2021-07-02 18:33:34 -05:00
|
|
|
this.0 = 0;
|
|
|
|
};
|
|
|
|
}
|
2021-07-03 14:42:59 -05:00
|
|
|
"#,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn function_multi_use_expr_in_param() {
|
|
|
|
check_assist(
|
|
|
|
inline_call,
|
|
|
|
r#"
|
|
|
|
fn square(x: u32) -> u32 {
|
|
|
|
x * x
|
|
|
|
}
|
|
|
|
fn main() {
|
|
|
|
let x = 51;
|
|
|
|
let y = square$0(10 + x);
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
r#"
|
|
|
|
fn square(x: u32) -> u32 {
|
|
|
|
x * x
|
|
|
|
}
|
|
|
|
fn main() {
|
|
|
|
let x = 51;
|
2021-07-05 06:37:40 -05:00
|
|
|
let y = {
|
|
|
|
let x = 10 + x;
|
2021-07-03 14:42:59 -05:00
|
|
|
x * x
|
|
|
|
};
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2021-07-05 09:33:45 -05:00
|
|
|
fn function_use_local_in_param() {
|
|
|
|
cov_mark::check!(inline_call_inline_locals);
|
2021-07-03 14:42:59 -05:00
|
|
|
check_assist(
|
|
|
|
inline_call,
|
|
|
|
r#"
|
|
|
|
fn square(x: u32) -> u32 {
|
|
|
|
x * x
|
|
|
|
}
|
|
|
|
fn main() {
|
|
|
|
let local = 51;
|
|
|
|
let y = square$0(local);
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
r#"
|
|
|
|
fn square(x: u32) -> u32 {
|
|
|
|
x * x
|
|
|
|
}
|
|
|
|
fn main() {
|
|
|
|
let local = 51;
|
|
|
|
let y = local * local;
|
|
|
|
}
|
2021-07-05 06:37:40 -05:00
|
|
|
"#,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn method_in_impl() {
|
|
|
|
check_assist(
|
|
|
|
inline_call,
|
|
|
|
r#"
|
|
|
|
struct Foo;
|
|
|
|
impl Foo {
|
|
|
|
fn foo(&self) {
|
|
|
|
self;
|
|
|
|
self;
|
|
|
|
}
|
|
|
|
fn bar(&self) {
|
|
|
|
self.foo$0();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
r#"
|
|
|
|
struct Foo;
|
|
|
|
impl Foo {
|
|
|
|
fn foo(&self) {
|
|
|
|
self;
|
|
|
|
self;
|
|
|
|
}
|
|
|
|
fn bar(&self) {
|
|
|
|
{
|
|
|
|
let ref this = self;
|
|
|
|
this;
|
|
|
|
this;
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
2021-07-05 08:34:01 -05:00
|
|
|
"#,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn wraps_closure_in_paren() {
|
2021-07-05 09:33:45 -05:00
|
|
|
cov_mark::check!(inline_call_inline_closure);
|
2021-07-05 08:34:01 -05:00
|
|
|
check_assist(
|
|
|
|
inline_call,
|
|
|
|
r#"
|
|
|
|
fn foo(x: fn()) {
|
|
|
|
x();
|
|
|
|
}
|
|
|
|
|
|
|
|
fn main() {
|
|
|
|
foo$0(|| {})
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
r#"
|
|
|
|
fn foo(x: fn()) {
|
|
|
|
x();
|
|
|
|
}
|
|
|
|
|
|
|
|
fn main() {
|
|
|
|
{
|
|
|
|
(|| {})();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
);
|
|
|
|
check_assist(
|
|
|
|
inline_call,
|
|
|
|
r#"
|
|
|
|
fn foo(x: fn()) {
|
|
|
|
x();
|
|
|
|
}
|
|
|
|
|
|
|
|
fn main() {
|
|
|
|
foo$0(main)
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
r#"
|
|
|
|
fn foo(x: fn()) {
|
|
|
|
x();
|
|
|
|
}
|
|
|
|
|
|
|
|
fn main() {
|
|
|
|
{
|
|
|
|
main();
|
|
|
|
}
|
|
|
|
}
|
2021-07-05 09:33:45 -05:00
|
|
|
"#,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn inline_single_literal_expr() {
|
|
|
|
cov_mark::check!(inline_call_inline_literal);
|
|
|
|
check_assist(
|
|
|
|
inline_call,
|
|
|
|
r#"
|
|
|
|
fn foo(x: u32) -> u32{
|
|
|
|
x
|
|
|
|
}
|
|
|
|
|
|
|
|
fn main() {
|
|
|
|
foo$0(222);
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
r#"
|
|
|
|
fn foo(x: u32) -> u32{
|
|
|
|
x
|
|
|
|
}
|
|
|
|
|
|
|
|
fn main() {
|
|
|
|
222;
|
|
|
|
}
|
2021-07-10 12:16:32 -05:00
|
|
|
"#,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn inline_emits_type_for_coercion() {
|
|
|
|
check_assist(
|
|
|
|
inline_call,
|
|
|
|
r#"
|
|
|
|
fn foo(x: *const u32) -> u32 {
|
|
|
|
x as u32
|
|
|
|
}
|
|
|
|
|
|
|
|
fn main() {
|
|
|
|
foo$0(&222);
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
r#"
|
|
|
|
fn foo(x: *const u32) -> u32 {
|
|
|
|
x as u32
|
|
|
|
}
|
|
|
|
|
|
|
|
fn main() {
|
|
|
|
{
|
|
|
|
let x: *const u32 = &222;
|
|
|
|
x as u32
|
|
|
|
};
|
|
|
|
}
|
2021-08-10 07:39:56 -05:00
|
|
|
"#,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
// FIXME: const generics aren't being substituted, this is blocked on better support for them
|
|
|
|
#[test]
|
|
|
|
fn inline_substitutes_generics() {
|
|
|
|
check_assist(
|
|
|
|
inline_call,
|
|
|
|
r#"
|
|
|
|
fn foo<T, const N: usize>() {
|
|
|
|
bar::<T, N>()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn bar<U, const M: usize>() {}
|
|
|
|
|
|
|
|
fn main() {
|
|
|
|
foo::<usize, {0}>$0();
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
r#"
|
|
|
|
fn foo<T, const N: usize>() {
|
|
|
|
bar::<T, N>()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn bar<U, const M: usize>() {}
|
|
|
|
|
|
|
|
fn main() {
|
|
|
|
bar::<usize, N>();
|
|
|
|
}
|
2021-09-25 11:39:43 -05:00
|
|
|
"#,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn inline_callers() {
|
|
|
|
check_assist(
|
|
|
|
inline_into_callers,
|
|
|
|
r#"
|
|
|
|
fn do_the_math$0(b: u32) -> u32 {
|
|
|
|
let foo = 10;
|
|
|
|
foo * b + foo
|
|
|
|
}
|
|
|
|
fn foo() {
|
|
|
|
do_the_math(0);
|
|
|
|
let bar = 10;
|
|
|
|
do_the_math(bar);
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
r#"
|
|
|
|
|
|
|
|
fn foo() {
|
|
|
|
{
|
|
|
|
let foo = 10;
|
|
|
|
foo * 0 + foo
|
|
|
|
};
|
|
|
|
let bar = 10;
|
|
|
|
{
|
|
|
|
let foo = 10;
|
|
|
|
foo * bar + foo
|
|
|
|
};
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn inline_callers_across_files() {
|
|
|
|
check_assist(
|
|
|
|
inline_into_callers,
|
|
|
|
r#"
|
|
|
|
//- /lib.rs
|
|
|
|
mod foo;
|
|
|
|
fn do_the_math$0(b: u32) -> u32 {
|
|
|
|
let foo = 10;
|
|
|
|
foo * b + foo
|
|
|
|
}
|
|
|
|
//- /foo.rs
|
|
|
|
use super::do_the_math;
|
|
|
|
fn foo() {
|
|
|
|
do_the_math(0);
|
|
|
|
let bar = 10;
|
|
|
|
do_the_math(bar);
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
r#"
|
|
|
|
use super::do_the_math;
|
|
|
|
fn foo() {
|
|
|
|
{
|
|
|
|
let foo = 10;
|
|
|
|
foo * 0 + foo
|
|
|
|
};
|
|
|
|
let bar = 10;
|
|
|
|
{
|
|
|
|
let foo = 10;
|
|
|
|
foo * bar + foo
|
|
|
|
};
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn inline_callers_across_files_with_def_file() {
|
|
|
|
check_assist(
|
|
|
|
inline_into_callers,
|
|
|
|
r#"
|
|
|
|
//- /lib.rs
|
|
|
|
mod foo;
|
|
|
|
fn do_the_math$0(b: u32) -> u32 {
|
|
|
|
let foo = 10;
|
|
|
|
foo * b + foo
|
|
|
|
}
|
|
|
|
fn bar(a: u32, b: u32) -> u32 {
|
|
|
|
do_the_math(0);
|
|
|
|
}
|
|
|
|
//- /foo.rs
|
|
|
|
use super::do_the_math;
|
|
|
|
fn foo() {
|
|
|
|
do_the_math(0);
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
r#"
|
|
|
|
//- /lib.rs
|
|
|
|
mod foo;
|
|
|
|
fn do_the_math(b: u32) -> u32 {
|
|
|
|
let foo = 10;
|
|
|
|
foo * b + foo
|
|
|
|
}
|
|
|
|
fn bar(a: u32, b: u32) -> u32 {
|
|
|
|
{
|
|
|
|
let foo = 10;
|
|
|
|
foo * 0 + foo
|
|
|
|
};
|
|
|
|
}
|
|
|
|
//- /foo.rs
|
|
|
|
use super::do_the_math;
|
|
|
|
fn foo() {
|
|
|
|
{
|
|
|
|
let foo = 10;
|
|
|
|
foo * 0 + foo
|
|
|
|
};
|
|
|
|
}
|
2021-09-26 07:38:58 -05:00
|
|
|
"#,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn inline_callers_recursive() {
|
|
|
|
cov_mark::check!(inline_into_callers_recursive);
|
|
|
|
check_assist_not_applicable(
|
|
|
|
inline_into_callers,
|
|
|
|
r#"
|
|
|
|
fn foo$0() {
|
|
|
|
foo();
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn inline_call_recursive() {
|
|
|
|
cov_mark::check!(inline_call_recursive);
|
|
|
|
check_assist_not_applicable(
|
|
|
|
inline_call,
|
|
|
|
r#"
|
|
|
|
fn foo() {
|
|
|
|
foo$0();
|
|
|
|
}
|
2021-01-01 23:47:35 -06:00
|
|
|
"#,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|