3 use hir::{db::HirDatabase, PathResolution, Semantics, TypeInfo};
5 base_db::{FileId, FileRange},
7 helpers::{insert_use::remove_path_if_in_use_stmt, node_ext::expr_as_name_ref},
8 path_transform::PathTransform,
9 search::{FileReference, SearchScope},
12 use itertools::{izip, Itertools};
14 ast::{self, edit_in_place::Indent, HasArgList, PathExpr},
19 assist_context::{AssistContext, Assists},
23 // Assist: inline_into_callers
25 // Inline a function or method body into all of its callers where possible, creating a `let` statement per parameter
26 // unless the parameter can be inlined. The parameter will be inlined either if it the supplied argument is a simple local
27 // or if the parameter is only accessed inside the function body once.
28 // If all calls can be inlined the function will be removed.
31 // fn print(_: &str) {}
32 // fn foo$0(word: &str) {
33 // if !word.is_empty() {
44 // fn print(_: &str) {}
48 // let word = "안녕하세요";
49 // if !word.is_empty() {
55 // if !word.is_empty() {
61 pub(crate) fn inline_into_callers(acc: &mut Assists, ctx: &AssistContext) -> Option<()> {
62 let def_file = ctx.file_id();
63 let name = ctx.find_node_at_offset::<ast::Name>()?;
64 let ast_func = name.syntax().parent().and_then(ast::Fn::cast)?;
65 let func_body = ast_func.body()?;
66 let param_list = ast_func.param_list()?;
68 let function = ctx.sema.to_def(&ast_func)?;
70 let params = get_fn_params(ctx.sema.db, function, ¶m_list)?;
72 let usages = Definition::Function(function).usages(&ctx.sema);
73 if !usages.at_least_one() {
77 let is_recursive_fn = usages
79 .in_scope(SearchScope::file_range(FileRange {
81 range: func_body.syntax().text_range(),
85 cov_mark::hit!(inline_into_callers_recursive);
90 AssistId("inline_into_callers", AssistKind::RefactorInline),
91 "Inline into all callers",
92 name.syntax().text_range(),
94 let mut usages = usages.all();
95 let current_file_usage = usages.references.remove(&def_file);
97 let mut remove_def = true;
98 let mut inline_refs_for_file = |file_id, refs: Vec<FileReference>| {
99 builder.edit_file(file_id);
100 let count = refs.len();
101 // The collects are required as we are otherwise iterating while mutating 🙅♀️🙅♂️
102 let (name_refs, name_refs_use): (Vec<_>, Vec<_>) = refs
104 .filter_map(|file_ref| match file_ref.name {
105 ast::NameLike::NameRef(name_ref) => Some(name_ref),
108 .partition_map(|name_ref| {
109 match name_ref.syntax().ancestors().find_map(ast::UseTree::cast) {
110 Some(use_tree) => Either::Right(builder.make_mut(use_tree)),
111 None => Either::Left(name_ref),
114 let call_infos: Vec<_> = name_refs
116 .filter_map(CallInfo::from_name_ref)
118 let mut_node = builder.make_syntax_mut(call_info.node.syntax().clone());
119 (call_info, mut_node)
122 let replaced = call_infos
124 .map(|(call_info, mut_node)| {
126 inline(&ctx.sema, def_file, function, &func_body, ¶ms, &call_info);
127 ted::replace(mut_node, replacement.syntax());
130 if replaced + name_refs_use.len() == count {
131 // we replaced all usages in this file, so we can remove the imports
132 name_refs_use.into_iter().for_each(|use_tree| {
133 if let Some(path) = use_tree.path() {
134 remove_path_if_in_use_stmt(&path);
141 for (file_id, refs) in usages.into_iter() {
142 inline_refs_for_file(file_id, refs);
144 match current_file_usage {
145 Some(refs) => inline_refs_for_file(def_file, refs),
146 None => builder.edit_file(def_file),
149 builder.delete(ast_func.syntax().text_range());
155 // Assist: inline_call
157 // Inlines a function or method body creating a `let` statement per parameter unless the parameter
158 // can be inlined. The parameter will be inlined either if it the supplied argument is a simple local
159 // or if the parameter is only accessed inside the function body once.
162 // # //- minicore: option
163 // fn foo(name: Option<&str>) {
164 // let name = name.unwrap$0();
169 // fn foo(name: Option<&str>) {
170 // let name = match name {
172 // None => panic!("called `Option::unwrap()` on a `None` value"),
176 pub(crate) fn inline_call(acc: &mut Assists, ctx: &AssistContext) -> Option<()> {
177 let name_ref: ast::NameRef = ctx.find_node_at_offset()?;
178 let call_info = CallInfo::from_name_ref(name_ref.clone())?;
179 let (function, label) = match &call_info.node {
180 ast::CallableExpr::Call(call) => {
181 let path = match call.expr()? {
182 ast::Expr::PathExpr(path) => path.path(),
185 let function = match ctx.sema.resolve_path(&path)? {
186 PathResolution::Def(hir::ModuleDef::Function(f)) => f,
187 PathResolution::AssocItem(hir::AssocItem::Function(f)) => f,
190 (function, format!("Inline `{}`", path))
192 ast::CallableExpr::MethodCall(call) => {
193 (ctx.sema.resolve_method_call(call)?, format!("Inline `{}`", name_ref))
197 let fn_source = ctx.sema.source(function)?;
198 let fn_body = fn_source.value.body()?;
199 let param_list = fn_source.value.param_list()?;
201 let FileRange { file_id, range } = fn_source.syntax().original_file_range(ctx.sema.db);
202 if file_id == ctx.file_id() && range.contains(ctx.offset()) {
203 cov_mark::hit!(inline_call_recursive);
206 let params = get_fn_params(ctx.sema.db, function, ¶m_list)?;
208 if call_info.arguments.len() != params.len() {
209 // Can't inline the function because they've passed the wrong number of
210 // arguments to this function
211 cov_mark::hit!(inline_call_incorrect_number_of_arguments);
215 let syntax = call_info.node.syntax().clone();
217 AssistId("inline_call", AssistKind::RefactorInline),
221 let replacement = inline(&ctx.sema, file_id, function, &fn_body, ¶ms, &call_info);
224 match call_info.node {
225 ast::CallableExpr::Call(it) => ast::Expr::CallExpr(it),
226 ast::CallableExpr::MethodCall(it) => ast::Expr::MethodCallExpr(it),
235 node: ast::CallableExpr,
236 arguments: Vec<ast::Expr>,
237 generic_arg_list: Option<ast::GenericArgList>,
241 fn from_name_ref(name_ref: ast::NameRef) -> Option<CallInfo> {
242 let parent = name_ref.syntax().parent()?;
243 if let Some(call) = ast::MethodCallExpr::cast(parent.clone()) {
244 let receiver = call.receiver()?;
245 let mut arguments = vec![receiver];
246 arguments.extend(call.arg_list()?.args());
248 generic_arg_list: call.generic_arg_list(),
249 node: ast::CallableExpr::MethodCall(call),
252 } else if let Some(segment) = ast::PathSegment::cast(parent) {
253 let path = segment.syntax().parent().and_then(ast::Path::cast)?;
254 let path = path.syntax().parent().and_then(ast::PathExpr::cast)?;
255 let call = path.syntax().parent().and_then(ast::CallExpr::cast)?;
258 arguments: call.arg_list()?.args().collect(),
259 node: ast::CallableExpr::Call(call),
260 generic_arg_list: segment.generic_arg_list(),
269 db: &dyn HirDatabase,
270 function: hir::Function,
271 param_list: &ast::ParamList,
272 ) -> Option<Vec<(ast::Pat, Option<ast::Type>, hir::Param)>> {
273 let mut assoc_fn_params = function.assoc_fn_params(db).into_iter();
275 let mut params = Vec::new();
276 if let Some(self_param) = param_list.self_param() {
277 // FIXME this should depend on the receiver as well as the self_param
280 self_param.amp_token().is_some(),
281 self_param.mut_token().is_some(),
286 assoc_fn_params.next()?,
289 for param in param_list.params() {
290 params.push((param.pat()?, param.ty(), assoc_fn_params.next()?));
297 sema: &Semantics<RootDatabase>,
298 function_def_file_id: FileId,
299 function: hir::Function,
300 fn_body: &ast::BlockExpr,
301 params: &[(ast::Pat, Option<ast::Type>, hir::Param)],
302 CallInfo { node, arguments, generic_arg_list }: &CallInfo,
304 let body = fn_body.clone_for_update();
305 let usages_for_locals = |local| {
306 Definition::Local(local)
310 .remove(&function_def_file_id)
314 let param_use_nodes: Vec<Vec<_>> = params
316 .map(|(pat, _, param)| {
317 if !matches!(pat, ast::Pat::IdentPat(pat) if pat.is_simple_ident()) {
320 usages_for_locals(param.as_local(sema.db))
321 .map(|FileReference { name, range, .. }| match name {
322 ast::NameLike::NameRef(_) => body
324 .covering_element(range)
327 .and_then(ast::PathExpr::cast),
330 .collect::<Option<Vec<_>>>()
334 if function.self_param(sema.db).is_some() {
335 let this = || make::name_ref("this").syntax().clone_for_update();
336 usages_for_locals(params[0].2.as_local(sema.db))
337 .flat_map(|FileReference { name, range, .. }| match name {
338 ast::NameLike::NameRef(_) => Some(body.syntax().covering_element(range)),
342 ted::replace(it, &this());
345 // Inline parameter expressions or generate `let` statements depending on whether inlining works or not.
346 for ((pat, param_ty, _), usages, expr) in izip!(params, param_use_nodes, arguments).rev() {
347 let inline_direct = |usage, replacement: &ast::Expr| {
348 if let Some(field) = path_expr_as_record_field(usage) {
349 cov_mark::hit!(inline_call_inline_direct_field);
350 field.replace_expr(replacement.clone_for_update());
352 ted::replace(usage.syntax(), &replacement.syntax().clone_for_update());
355 // izip confuses RA due to our lack of hygiene info currently losing us type info causing incorrect errors
356 let usages: &[ast::PathExpr] = &*usages;
357 let expr: &ast::Expr = expr;
359 // inline single use closure arguments
361 if matches!(expr, ast::Expr::ClosureExpr(_))
362 && usage.syntax().parent().and_then(ast::Expr::cast).is_some() =>
364 cov_mark::hit!(inline_call_inline_closure);
365 let expr = make::expr_paren(expr.clone());
366 inline_direct(usage, &expr);
368 // inline single use literals
369 [usage] if matches!(expr, ast::Expr::Literal(_)) => {
370 cov_mark::hit!(inline_call_inline_literal);
371 inline_direct(usage, &expr);
373 // inline direct local arguments
374 [_, ..] if expr_as_name_ref(&expr).is_some() => {
375 cov_mark::hit!(inline_call_inline_locals);
376 usages.into_iter().for_each(|usage| inline_direct(usage, &expr));
378 // can't inline, emit a let statement
381 sema.type_of_expr(expr).filter(TypeInfo::has_adjustment).and(param_ty.clone());
382 if let Some(stmt_list) = body.stmt_list() {
383 stmt_list.push_front(
384 make::let_stmt(pat.clone(), ty, Some(expr.clone()))
392 if let Some(generic_arg_list) = generic_arg_list.clone() {
393 PathTransform::function_call(
394 &sema.scope(node.syntax()),
395 &sema.scope(fn_body.syntax()),
399 .apply(body.syntax());
402 let original_indentation = match node {
403 ast::CallableExpr::Call(it) => it.indent_level(),
404 ast::CallableExpr::MethodCall(it) => it.indent_level(),
406 body.reindent_to(original_indentation);
408 match body.tail_expr() {
409 Some(expr) if body.statements().next().is_none() => expr,
413 .and_then(ast::BinExpr::cast)
414 .and_then(|bin_expr| bin_expr.lhs())
416 Some(lhs) if lhs.syntax() == node.syntax() => {
417 make::expr_paren(ast::Expr::BlockExpr(body)).clone_for_update()
419 _ => ast::Expr::BlockExpr(body),
424 fn path_expr_as_record_field(usage: &PathExpr) -> Option<ast::RecordExprField> {
425 let path = usage.path()?;
426 let name_ref = path.as_single_name_ref()?;
427 ast::RecordExprField::for_name_ref(&name_ref)
432 use crate::tests::{check_assist, check_assist_not_applicable};
437 fn no_args_or_return_value_gets_inlined_without_block() {
441 fn foo() { println!("Hello, World!"); }
447 fn foo() { println!("Hello, World!"); }
449 { println!("Hello, World!"); };
456 fn not_applicable_when_incorrect_number_of_parameters_are_provided() {
457 cov_mark::check!(inline_call_incorrect_number_of_arguments);
458 check_assist_not_applicable(
461 fn add(a: u32, b: u32) -> u32 { a + b }
462 fn main() { let x = add$0(42); }
468 fn args_with_side_effects() {
472 fn foo(name: String) {
473 println!("Hello, {}!", name);
476 foo$0(String::from("Michael"));
480 fn foo(name: String) {
481 println!("Hello, {}!", name);
485 let name = String::from("Michael");
486 println!("Hello, {}!", name);
494 fn function_with_multiple_statements() {
498 fn foo(a: u32, b: u32) -> u32 {
509 fn foo(a: u32, b: u32) -> u32 {
528 fn function_with_self_param() {
535 fn add(self, a: u32) -> Self {
541 let x = Foo::add$0(Foo(3), 2);
548 fn add(self, a: u32) -> Self {
571 fn add(self, a: u32) -> Self {
577 let x = Foo(3).add$0(2);
584 fn add(self, a: u32) -> Self {
607 fn add(&self, a: u32) -> Self {
613 let x = Foo(3).add$0(2);
620 fn add(&self, a: u32) -> Self {
627 let ref this = Foo(3);
636 fn method_by_ref_mut() {
643 fn clear(&mut self) {
649 let mut foo = Foo(3);
657 fn clear(&mut self) {
663 let mut foo = Foo(3);
665 let ref mut this = foo;
674 fn function_multi_use_expr_in_param() {
678 fn square(x: u32) -> u32 {
683 let y = square$0(10 + x);
687 fn square(x: u32) -> u32 {
702 fn function_use_local_in_param() {
703 cov_mark::check!(inline_call_inline_locals);
707 fn square(x: u32) -> u32 {
712 let y = square$0(local);
716 fn square(x: u32) -> u32 {
721 let y = local * local;
728 fn method_in_impl() {
763 fn wraps_closure_in_paren() {
764 cov_mark::check!(inline_call_inline_closure);
814 fn inline_single_literal_expr() {
815 cov_mark::check!(inline_call_inline_literal);
819 fn foo(x: u32) -> u32{
828 fn foo(x: u32) -> u32{
840 fn inline_emits_type_for_coercion() {
844 fn foo(x: *const u32) -> u32 {
853 fn foo(x: *const u32) -> u32 {
859 let x: *const u32 = &222;
867 // FIXME: const generics aren't being substituted, this is blocked on better support for them
869 fn inline_substitutes_generics() {
873 fn foo<T, const N: usize>() {
877 fn bar<U, const M: usize>() {}
880 foo$0::<usize, {0}>();
884 fn foo<T, const N: usize>() {
888 fn bar<U, const M: usize>() {}
898 fn inline_callers() {
902 fn do_the_math$0(b: u32) -> u32 {
930 fn inline_callers_across_files() {
936 fn do_the_math$0(b: u32) -> u32 {
941 use super::do_the_math;
969 fn inline_callers_across_files_with_def_file() {
975 fn do_the_math$0(b: u32) -> u32 {
979 fn bar(a: u32, b: u32) -> u32 {
983 use super::do_the_math;
992 fn bar(a: u32, b: u32) -> u32 {
1010 fn inline_callers_recursive() {
1011 cov_mark::check!(inline_into_callers_recursive);
1012 check_assist_not_applicable(
1013 inline_into_callers,
1023 fn inline_call_recursive() {
1024 cov_mark::check!(inline_call_recursive);
1025 check_assist_not_applicable(
1036 fn inline_call_field_shorthand() {
1037 cov_mark::check!(inline_call_inline_direct_field);
1047 fn foo(field: u32, field1: u32, val2: u32, val3: u32) -> Foo {
1058 foo$0(bar, 0, baz, 0);
1068 fn foo(field: u32, field1: u32, val2: u32, val3: u32) -> Foo {
1091 fn inline_callers_wrapped_in_parentheses() {
1093 inline_into_callers,
1119 fn inline_call_wrapped_in_parentheses() {