1 use std::collections::BTreeSet;
5 use hir::{db::HirDatabase, PathResolution, Semantics, TypeInfo};
7 base_db::{FileId, FileRange},
9 imports::insert_use::remove_path_if_in_use_stmt,
10 path_transform::PathTransform,
11 search::{FileReference, SearchScope},
12 source_change::SourceChangeBuilder,
13 syntax_helpers::{insert_whitespace_into_node::insert_ws_into, node_ext::expr_as_name_ref},
16 use itertools::{izip, Itertools};
18 ast::{self, edit_in_place::Indent, HasArgList, PathExpr},
19 ted, AstNode, NodeOrToken, SyntaxKind,
23 assist_context::{AssistContext, Assists},
27 // Assist: inline_into_callers
29 // Inline a function or method body into all of its callers where possible, creating a `let` statement per parameter
30 // unless the parameter can be inlined. The parameter will be inlined either if it the supplied argument is a simple local
31 // or if the parameter is only accessed inside the function body once.
32 // If all calls can be inlined the function will be removed.
35 // fn print(_: &str) {}
36 // fn foo$0(word: &str) {
37 // if !word.is_empty() {
48 // fn print(_: &str) {}
52 // let word = "안녕하세요";
53 // if !word.is_empty() {
59 // if !word.is_empty() {
65 pub(crate) fn inline_into_callers(acc: &mut Assists, ctx: &AssistContext<'_>) -> Option<()> {
66 let def_file = ctx.file_id();
67 let name = ctx.find_node_at_offset::<ast::Name>()?;
68 let ast_func = name.syntax().parent().and_then(ast::Fn::cast)?;
69 let func_body = ast_func.body()?;
70 let param_list = ast_func.param_list()?;
72 let function = ctx.sema.to_def(&ast_func)?;
74 let params = get_fn_params(ctx.sema.db, function, ¶m_list)?;
76 let usages = Definition::Function(function).usages(&ctx.sema);
77 if !usages.at_least_one() {
81 let is_recursive_fn = usages
83 .in_scope(SearchScope::file_range(FileRange {
85 range: func_body.syntax().text_range(),
89 cov_mark::hit!(inline_into_callers_recursive);
94 AssistId("inline_into_callers", AssistKind::RefactorInline),
95 "Inline into all callers",
96 name.syntax().text_range(),
98 let mut usages = usages.all();
99 let current_file_usage = usages.references.remove(&def_file);
101 let mut remove_def = true;
102 let mut inline_refs_for_file = |file_id, refs: Vec<FileReference>| {
103 builder.edit_file(file_id);
104 let count = refs.len();
105 // The collects are required as we are otherwise iterating while mutating 🙅♀️🙅♂️
106 let (name_refs, name_refs_use) = split_refs_and_uses(builder, refs, Some);
107 let call_infos: Vec<_> = name_refs
109 .filter_map(CallInfo::from_name_ref)
111 let mut_node = builder.make_syntax_mut(call_info.node.syntax().clone());
112 (call_info, mut_node)
115 let replaced = call_infos
117 .map(|(call_info, mut_node)| {
119 inline(&ctx.sema, def_file, function, &func_body, ¶ms, &call_info);
120 ted::replace(mut_node, replacement.syntax());
123 if replaced + name_refs_use.len() == count {
124 // we replaced all usages in this file, so we can remove the imports
125 name_refs_use.iter().for_each(remove_path_if_in_use_stmt);
130 for (file_id, refs) in usages.into_iter() {
131 inline_refs_for_file(file_id, refs);
133 match current_file_usage {
134 Some(refs) => inline_refs_for_file(def_file, refs),
135 None => builder.edit_file(def_file),
138 builder.delete(ast_func.syntax().text_range());
144 pub(super) fn split_refs_and_uses<T: ast::AstNode>(
145 builder: &mut SourceChangeBuilder,
146 iter: impl IntoIterator<Item = FileReference>,
147 mut map_ref: impl FnMut(ast::NameRef) -> Option<T>,
148 ) -> (Vec<T>, Vec<ast::Path>) {
150 .filter_map(|file_ref| match file_ref.name {
151 ast::NameLike::NameRef(name_ref) => Some(name_ref),
154 .filter_map(|name_ref| match name_ref.syntax().ancestors().find_map(ast::UseTree::cast) {
155 Some(use_tree) => builder.make_mut(use_tree).path().map(Either::Right),
156 None => map_ref(name_ref).map(Either::Left),
158 .partition_map(|either| either)
161 // Assist: inline_call
163 // Inlines a function or method body creating a `let` statement per parameter unless the parameter
164 // can be inlined. The parameter will be inlined either if it the supplied argument is a simple local
165 // or if the parameter is only accessed inside the function body once.
168 // # //- minicore: option
169 // fn foo(name: Option<&str>) {
170 // let name = name.unwrap$0();
175 // fn foo(name: Option<&str>) {
176 // let name = match name {
178 // None => panic!("called `Option::unwrap()` on a `None` value"),
182 pub(crate) fn inline_call(acc: &mut Assists, ctx: &AssistContext<'_>) -> Option<()> {
183 let name_ref: ast::NameRef = ctx.find_node_at_offset()?;
184 let call_info = CallInfo::from_name_ref(name_ref.clone())?;
185 let (function, label) = match &call_info.node {
186 ast::CallableExpr::Call(call) => {
187 let path = match call.expr()? {
188 ast::Expr::PathExpr(path) => path.path(),
191 let function = match ctx.sema.resolve_path(&path)? {
192 PathResolution::Def(hir::ModuleDef::Function(f)) => f,
195 (function, format!("Inline `{path}`"))
197 ast::CallableExpr::MethodCall(call) => {
198 (ctx.sema.resolve_method_call(call)?, format!("Inline `{name_ref}`"))
202 let fn_source = ctx.sema.source(function)?;
203 let fn_body = fn_source.value.body()?;
204 let param_list = fn_source.value.param_list()?;
206 let FileRange { file_id, range } = fn_source.syntax().original_file_range(ctx.sema.db);
207 if file_id == ctx.file_id() && range.contains(ctx.offset()) {
208 cov_mark::hit!(inline_call_recursive);
211 let params = get_fn_params(ctx.sema.db, function, ¶m_list)?;
213 if call_info.arguments.len() != params.len() {
214 // Can't inline the function because they've passed the wrong number of
215 // arguments to this function
216 cov_mark::hit!(inline_call_incorrect_number_of_arguments);
220 let syntax = call_info.node.syntax().clone();
222 AssistId("inline_call", AssistKind::RefactorInline),
226 let replacement = inline(&ctx.sema, file_id, function, &fn_body, ¶ms, &call_info);
229 match call_info.node {
230 ast::CallableExpr::Call(it) => ast::Expr::CallExpr(it),
231 ast::CallableExpr::MethodCall(it) => ast::Expr::MethodCallExpr(it),
240 node: ast::CallableExpr,
241 arguments: Vec<ast::Expr>,
242 generic_arg_list: Option<ast::GenericArgList>,
246 fn from_name_ref(name_ref: ast::NameRef) -> Option<CallInfo> {
247 let parent = name_ref.syntax().parent()?;
248 if let Some(call) = ast::MethodCallExpr::cast(parent.clone()) {
249 let receiver = call.receiver()?;
250 let mut arguments = vec![receiver];
251 arguments.extend(call.arg_list()?.args());
253 generic_arg_list: call.generic_arg_list(),
254 node: ast::CallableExpr::MethodCall(call),
257 } else if let Some(segment) = ast::PathSegment::cast(parent) {
258 let path = segment.syntax().parent().and_then(ast::Path::cast)?;
259 let path = path.syntax().parent().and_then(ast::PathExpr::cast)?;
260 let call = path.syntax().parent().and_then(ast::CallExpr::cast)?;
263 arguments: call.arg_list()?.args().collect(),
264 node: ast::CallableExpr::Call(call),
265 generic_arg_list: segment.generic_arg_list(),
274 db: &dyn HirDatabase,
275 function: hir::Function,
276 param_list: &ast::ParamList,
277 ) -> Option<Vec<(ast::Pat, Option<ast::Type>, hir::Param)>> {
278 let mut assoc_fn_params = function.assoc_fn_params(db).into_iter();
280 let mut params = Vec::new();
281 if let Some(self_param) = param_list.self_param() {
282 // FIXME this should depend on the receiver as well as the self_param
285 self_param.amp_token().is_some(),
286 self_param.mut_token().is_some(),
291 assoc_fn_params.next()?,
294 for param in param_list.params() {
295 params.push((param.pat()?, param.ty(), assoc_fn_params.next()?));
302 sema: &Semantics<'_, RootDatabase>,
303 function_def_file_id: FileId,
304 function: hir::Function,
305 fn_body: &ast::BlockExpr,
306 params: &[(ast::Pat, Option<ast::Type>, hir::Param)],
307 CallInfo { node, arguments, generic_arg_list }: &CallInfo,
309 let body = if sema.hir_file_for(fn_body.syntax()).is_macro() {
310 cov_mark::hit!(inline_call_defined_in_macro);
311 if let Some(body) = ast::BlockExpr::cast(insert_ws_into(fn_body.syntax().clone())) {
314 fn_body.clone_for_update()
317 fn_body.clone_for_update()
319 if let Some(imp) = body.syntax().ancestors().find_map(ast::Impl::cast) {
320 if !node.syntax().ancestors().any(|anc| &anc == imp.syntax()) {
321 if let Some(t) = imp.self_ty() {
323 .descendants_with_tokens()
324 .filter_map(NodeOrToken::into_token)
325 .filter(|tok| tok.kind() == SyntaxKind::SELF_TYPE_KW)
326 .for_each(|tok| ted::replace(tok, t.syntax()));
330 let usages_for_locals = |local| {
331 Definition::Local(local)
335 .remove(&function_def_file_id)
339 let param_use_nodes: Vec<Vec<_>> = params
341 .map(|(pat, _, param)| {
342 if !matches!(pat, ast::Pat::IdentPat(pat) if pat.is_simple_ident()) {
345 // FIXME: we need to fetch all locals declared in the parameter here
346 // not only the local if it is a simple binding
347 match param.as_local(sema.db) {
348 Some(l) => usages_for_locals(l)
349 .map(|FileReference { name, range, .. }| match name {
350 ast::NameLike::NameRef(_) => body
352 .covering_element(range)
355 .and_then(ast::PathExpr::cast),
358 .collect::<Option<Vec<_>>>()
359 .unwrap_or_default(),
365 if function.self_param(sema.db).is_some() {
366 let this = || make::name_ref("this").syntax().clone_for_update();
367 if let Some(self_local) = params[0].2.as_local(sema.db) {
368 usages_for_locals(self_local)
369 .flat_map(|FileReference { name, range, .. }| match name {
370 ast::NameLike::NameRef(_) => Some(body.syntax().covering_element(range)),
374 ted::replace(it, &this());
379 let mut func_let_vars: BTreeSet<String> = BTreeSet::new();
381 // grab all of the local variable declarations in the function
382 for stmt in fn_body.statements() {
383 if let Some(let_stmt) = ast::LetStmt::cast(stmt.syntax().to_owned()) {
384 for has_token in let_stmt.syntax().children_with_tokens() {
385 if let Some(node) = has_token.as_node() {
386 if let Some(ident_pat) = ast::IdentPat::cast(node.to_owned()) {
387 func_let_vars.insert(ident_pat.syntax().text().to_string());
394 // Inline parameter expressions or generate `let` statements depending on whether inlining works or not.
395 for ((pat, param_ty, _), usages, expr) in izip!(params, param_use_nodes, arguments).rev() {
396 // izip confuses RA due to our lack of hygiene info currently losing us type info causing incorrect errors
397 let usages: &[ast::PathExpr] = &*usages;
398 let expr: &ast::Expr = expr;
400 let insert_let_stmt = || {
401 let ty = sema.type_of_expr(expr).filter(TypeInfo::has_adjustment).and(param_ty.clone());
402 if let Some(stmt_list) = body.stmt_list() {
403 stmt_list.push_front(
404 make::let_stmt(pat.clone(), ty, Some(expr.clone())).clone_for_update().into(),
409 // check if there is a local var in the function that conflicts with parameter
410 // if it does then emit a let statement and continue
411 if func_let_vars.contains(&expr.syntax().text().to_string()) {
416 let inline_direct = |usage, replacement: &ast::Expr| {
417 if let Some(field) = path_expr_as_record_field(usage) {
418 cov_mark::hit!(inline_call_inline_direct_field);
419 field.replace_expr(replacement.clone_for_update());
421 ted::replace(usage.syntax(), &replacement.syntax().clone_for_update());
426 // inline single use closure arguments
428 if matches!(expr, ast::Expr::ClosureExpr(_))
429 && usage.syntax().parent().and_then(ast::Expr::cast).is_some() =>
431 cov_mark::hit!(inline_call_inline_closure);
432 let expr = make::expr_paren(expr.clone());
433 inline_direct(usage, &expr);
435 // inline single use literals
436 [usage] if matches!(expr, ast::Expr::Literal(_)) => {
437 cov_mark::hit!(inline_call_inline_literal);
438 inline_direct(usage, expr);
440 // inline direct local arguments
441 [_, ..] if expr_as_name_ref(expr).is_some() => {
442 cov_mark::hit!(inline_call_inline_locals);
443 usages.iter().for_each(|usage| inline_direct(usage, expr));
445 // can't inline, emit a let statement
452 if let Some(generic_arg_list) = generic_arg_list.clone() {
453 if let Some((target, source)) = &sema.scope(node.syntax()).zip(sema.scope(fn_body.syntax()))
455 PathTransform::function_call(target, source, function, generic_arg_list)
456 .apply(body.syntax());
460 let original_indentation = match node {
461 ast::CallableExpr::Call(it) => it.indent_level(),
462 ast::CallableExpr::MethodCall(it) => it.indent_level(),
464 body.reindent_to(original_indentation);
466 match body.tail_expr() {
467 Some(expr) if body.statements().next().is_none() => expr,
471 .and_then(ast::BinExpr::cast)
472 .and_then(|bin_expr| bin_expr.lhs())
474 Some(lhs) if lhs.syntax() == node.syntax() => {
475 make::expr_paren(ast::Expr::BlockExpr(body)).clone_for_update()
477 _ => ast::Expr::BlockExpr(body),
482 fn path_expr_as_record_field(usage: &PathExpr) -> Option<ast::RecordExprField> {
483 let path = usage.path()?;
484 let name_ref = path.as_single_name_ref()?;
485 ast::RecordExprField::for_name_ref(&name_ref)
490 use crate::tests::{check_assist, check_assist_not_applicable};
495 fn no_args_or_return_value_gets_inlined_without_block() {
499 fn foo() { println!("Hello, World!"); }
505 fn foo() { println!("Hello, World!"); }
507 { println!("Hello, World!"); };
514 fn not_applicable_when_incorrect_number_of_parameters_are_provided() {
515 cov_mark::check!(inline_call_incorrect_number_of_arguments);
516 check_assist_not_applicable(
519 fn add(a: u32, b: u32) -> u32 { a + b }
520 fn main() { let x = add$0(42); }
526 fn args_with_side_effects() {
530 fn foo(name: String) {
531 println!("Hello, {}!", name);
534 foo$0(String::from("Michael"));
538 fn foo(name: String) {
539 println!("Hello, {}!", name);
543 let name = String::from("Michael");
544 println!("Hello, {}!", name);
552 fn function_with_multiple_statements() {
556 fn foo(a: u32, b: u32) -> u32 {
567 fn foo(a: u32, b: u32) -> u32 {
586 fn function_with_self_param() {
593 fn add(self, a: u32) -> Self {
599 let x = Foo::add$0(Foo(3), 2);
606 fn add(self, a: u32) -> Self {
629 fn add(self, a: u32) -> Self {
635 let x = Foo(3).add$0(2);
642 fn add(self, a: u32) -> Self {
665 fn add(&self, a: u32) -> Self {
671 let x = Foo(3).add$0(2);
678 fn add(&self, a: u32) -> Self {
685 let ref this = Foo(3);
694 fn method_by_ref_mut() {
701 fn clear(&mut self) {
707 let mut foo = Foo(3);
715 fn clear(&mut self) {
721 let mut foo = Foo(3);
723 let ref mut this = foo;
732 fn function_multi_use_expr_in_param() {
736 fn square(x: u32) -> u32 {
741 let y = square$0(10 + x);
745 fn square(x: u32) -> u32 {
760 fn function_use_local_in_param() {
761 cov_mark::check!(inline_call_inline_locals);
765 fn square(x: u32) -> u32 {
770 let y = square$0(local);
774 fn square(x: u32) -> u32 {
779 let y = local * local;
786 fn method_in_impl() {
821 fn wraps_closure_in_paren() {
822 cov_mark::check!(inline_call_inline_closure);
872 fn inline_single_literal_expr() {
873 cov_mark::check!(inline_call_inline_literal);
877 fn foo(x: u32) -> u32{
886 fn foo(x: u32) -> u32{
898 fn inline_emits_type_for_coercion() {
902 fn foo(x: *const u32) -> u32 {
911 fn foo(x: *const u32) -> u32 {
917 let x: *const u32 = &222;
925 // FIXME: const generics aren't being substituted, this is blocked on better support for them
927 fn inline_substitutes_generics() {
931 fn foo<T, const N: usize>() {
935 fn bar<U, const M: usize>() {}
938 foo$0::<usize, {0}>();
942 fn foo<T, const N: usize>() {
946 fn bar<U, const M: usize>() {}
956 fn inline_callers() {
960 fn do_the_math$0(b: u32) -> u32 {
988 fn inline_callers_across_files() {
994 fn do_the_math$0(b: u32) -> u32 {
999 use super::do_the_math;
1027 fn inline_callers_across_files_with_def_file() {
1029 inline_into_callers,
1033 fn do_the_math$0(b: u32) -> u32 {
1037 fn bar(a: u32, b: u32) -> u32 {
1041 use super::do_the_math;
1050 fn bar(a: u32, b: u32) -> u32 {
1068 fn inline_callers_recursive() {
1069 cov_mark::check!(inline_into_callers_recursive);
1070 check_assist_not_applicable(
1071 inline_into_callers,
1081 fn inline_call_recursive() {
1082 cov_mark::check!(inline_call_recursive);
1083 check_assist_not_applicable(
1094 fn inline_call_field_shorthand() {
1095 cov_mark::check!(inline_call_inline_direct_field);
1105 fn foo(field: u32, field1: u32, val2: u32, val3: u32) -> Foo {
1116 foo$0(bar, 0, baz, 0);
1126 fn foo(field: u32, field1: u32, val2: u32, val3: u32) -> Foo {
1149 fn inline_callers_wrapped_in_parentheses() {
1151 inline_into_callers,
1177 fn inline_call_wrapped_in_parentheses() {
1205 fn inline_call_defined_in_macro() {
1206 cov_mark::check!(inline_call_defined_in_macro);
1210 macro_rules! define_foo {
1211 () => { fn foo() -> u32 {
1222 macro_rules! define_foo {
1223 () => { fn foo() -> u32 {
1240 fn inline_call_with_self_type() {
1246 fn f() -> Self { Self(114514) }
1255 fn f() -> Self { Self(114514) }
1265 fn inline_call_with_self_type_but_within_same_impl() {
1271 fn f() -> Self { Self(1919810) }
1280 fn f() -> Self { Self(1919810) }
1290 fn local_variable_shadowing_callers_argument() {
1294 fn foo(bar: u32, baz: u32) -> u32 {
1301 let res = foo$0(a, b);
1305 fn foo(bar: u32, baz: u32) -> u32 {