5 use hir::{HirDisplay, InFile, Local, ModuleDef, Semantics, TypeInfo};
7 defs::{Definition, NameRefClass},
9 insert_use::{insert_use, ImportScope},
11 node_ext::{preorder_expr, walk_expr, walk_pat, walk_patterns_in_expr},
14 search::{FileReference, ReferenceCategory, SearchScope},
15 FxIndexSet, RootDatabase,
17 use itertools::Itertools;
22 edit::{AstNodeEdit, IndentLevel},
25 match_ast, ted, SyntaxElement,
26 SyntaxKind::{self, COMMENT},
27 SyntaxNode, SyntaxToken, TextRange, TextSize, TokenAtOffset, WalkEvent, T,
31 assist_context::{AssistContext, Assists, TreeMutator},
35 // Assist: extract_function
37 // Extracts selected statements into new function.
55 // fn $0fun_name(n: i32) {
60 pub(crate) fn extract_function(acc: &mut Assists, ctx: &AssistContext) -> Option<()> {
61 let range = ctx.selection_trimmed();
66 let node = ctx.covering_element();
67 if node.kind() == COMMENT {
68 cov_mark::hit!(extract_function_in_comment_is_not_applicable);
72 let node = match node {
73 syntax::NodeOrToken::Node(n) => n,
74 syntax::NodeOrToken::Token(t) => t.parent()?,
77 let body = extraction_target(&node, range)?;
78 let container_info = body.analyze_container(&ctx.sema)?;
80 let (locals_used, self_param) = body.analyze(&ctx.sema);
82 let anchor = if self_param.is_some() { Anchor::Method } else { Anchor::Freestanding };
83 let insert_after = node_to_insert_after(&body, anchor)?;
84 let module = ctx.sema.scope(&insert_after).module()?;
86 let ret_ty = body.return_ty(ctx)?;
87 let control_flow = body.external_control_flow(ctx, &container_info)?;
88 let ret_values = body.ret_values(ctx, node.parent().as_ref().unwrap_or(&node));
90 let target_range = body.text_range();
92 let scope = ImportScope::find_insert_use_container(&node, &ctx.sema)?;
95 AssistId("extract_function", crate::AssistKind::RefactorExtract),
96 "Extract into function",
99 let outliving_locals: Vec<_> = ret_values.collect();
100 if stdx::never!(!outliving_locals.is_empty() && !ret_ty.is_unit()) {
101 // We should not have variables that outlive body if we have expression block
106 body.extracted_function_params(ctx, &container_info, locals_used.iter().copied());
109 name: make::name_ref("fun_name"),
116 mods: container_info,
119 let new_indent = IndentLevel::from_node(&insert_after);
120 let old_indent = fun.body.indent_level();
122 builder.replace(target_range, make_call(ctx, &fun, old_indent));
124 let fn_def = format_function(ctx, module, &fun, old_indent, new_indent);
125 let insert_offset = insert_after.text_range().end();
127 if fn_def.contains("ControlFlow") {
128 let scope = match scope {
129 ImportScope::File(it) => ImportScope::File(builder.make_mut(it)),
130 ImportScope::Module(it) => ImportScope::Module(builder.make_mut(it)),
131 ImportScope::Block(it) => ImportScope::Block(builder.make_mut(it)),
134 let control_flow_enum =
135 FamousDefs(&ctx.sema, Some(module.krate())).core_ops_ControlFlow();
137 if let Some(control_flow_enum) = control_flow_enum {
138 let mod_path = module.find_use_path_prefixed(
140 ModuleDef::from(control_flow_enum),
141 ctx.config.insert_use.prefix_kind,
144 if let Some(mod_path) = mod_path {
145 insert_use(&scope, mod_path_to_ast(&mod_path), &ctx.config.insert_use);
150 match ctx.config.snippet_cap {
151 Some(cap) => builder.insert_snippet(cap, insert_offset, fn_def),
152 None => builder.insert(insert_offset, fn_def),
158 /// Try to guess what user wants to extract
160 /// We have basically have two cases:
161 /// * We want whole node, like `loop {}`, `2 + 2`, `{ let n = 1; }` exprs.
162 /// Then we can use `ast::Expr`
163 /// * We want a few statements for a block. E.g.
165 /// fn foo() -> i32 {
175 fn extraction_target(node: &SyntaxNode, selection_range: TextRange) -> Option<FunctionBody> {
176 if let Some(stmt) = ast::Stmt::cast(node.clone()) {
178 ast::Stmt::Item(_) => None,
179 ast::Stmt::ExprStmt(_) | ast::Stmt::LetStmt(_) => Some(FunctionBody::from_range(
180 node.parent().and_then(ast::StmtList::cast)?,
186 // Covering element returned the parent block of one or multiple statements that have been selected
187 if let Some(stmt_list) = ast::StmtList::cast(node.clone()) {
188 if let Some(block_expr) = stmt_list.syntax().parent().and_then(ast::BlockExpr::cast) {
189 if block_expr.syntax().text_range() == selection_range {
190 return FunctionBody::from_expr(block_expr.into());
194 // Extract the full statements.
195 return Some(FunctionBody::from_range(stmt_list, selection_range));
198 let expr = ast::Expr::cast(node.clone())?;
199 // A node got selected fully
200 if node.text_range() == selection_range {
201 return FunctionBody::from_expr(expr);
204 node.ancestors().find_map(ast::Expr::cast).and_then(FunctionBody::from_expr)
210 self_param: Option<ast::SelfParam>,
212 control_flow: ControlFlow,
215 outliving_locals: Vec<OutlivedLocal>,
228 #[derive(Debug, Clone, Copy, PartialEq, Eq)]
236 #[derive(Debug, Eq, PartialEq)]
240 Tuple(Vec<hir::Type>),
243 /// Where to put extracted function definition
246 /// Extract free function and put right after current top-level function
248 /// Extract method and put right after current function in the impl-block
252 // FIXME: ControlFlow and ContainerInfo both track some function modifiers, feels like these two should
253 // probably be merged somehow.
256 kind: Option<FlowKind>,
261 /// The thing whose expression we are extracting from. Can be a function, const, static, const arg, ...
262 #[derive(Clone, Debug)]
263 struct ContainerInfo {
266 parent_loop: Option<SyntaxNode>,
267 /// The function's return type, const's type etc.
268 ret_type: Option<hir::Type>,
271 /// Control flow that is exported from extracted function
283 #[derive(Debug, Clone)]
285 /// Return with value (`return $expr;`)
286 Return(Option<ast::Expr>),
290 /// Break with value (`break $expr;`)
291 Break(Option<ast::Expr>),
296 #[derive(Debug, Clone)]
299 Result { ty: hir::Type },
309 fn is_unit(&self) -> bool {
311 RetType::Expr(ty) => ty.is_unit(),
312 RetType::Stmt => true,
317 /// Semantically same as `ast::Expr`, but preserves identity when using only part of the Block
318 /// This is the future function body, the part that is being extracted.
322 Span { parent: ast::StmtList, text_range: TextRange },
326 struct OutlivedLocal {
328 mut_usage_outside_body: bool,
331 /// Container of local variable usages
333 /// Semanticall same as `UsageSearchResult`, but provides more convenient interface
334 struct LocalUsages(ide_db::search::UsageSearchResult);
337 fn find_local_usages(ctx: &AssistContext, var: Local) -> Self {
339 Definition::Local(var)
341 .in_scope(SearchScope::single_file(ctx.file_id()))
346 fn iter(&self) -> impl Iterator<Item = &FileReference> + '_ {
347 self.0.iter().flat_map(|(_, rs)| rs)
352 fn return_type(&self, ctx: &AssistContext) -> FunType {
354 RetType::Expr(ty) if ty.is_unit() => FunType::Unit,
355 RetType::Expr(ty) => FunType::Single(ty.clone()),
356 RetType::Stmt => match self.outliving_locals.as_slice() {
358 [var] => FunType::Single(var.local.ty(ctx.db())),
360 let types = vars.iter().map(|v| v.local.ty(ctx.db())).collect();
361 FunType::Tuple(types)
369 fn is_ref(&self) -> bool {
370 matches!(self, ParamKind::SharedRef | ParamKind::MutRef)
375 fn kind(&self) -> ParamKind {
376 match (self.move_local, self.requires_mut, self.is_copy) {
377 (false, true, _) => ParamKind::MutRef,
378 (false, false, false) => ParamKind::SharedRef,
379 (true, true, _) => ParamKind::MutValue,
380 (_, false, _) => ParamKind::Value,
384 fn to_arg(&self, ctx: &AssistContext) -> ast::Expr {
385 let var = path_expr_from_local(ctx, self.var);
387 ParamKind::Value | ParamKind::MutValue => var,
388 ParamKind::SharedRef => make::expr_ref(var, false),
389 ParamKind::MutRef => make::expr_ref(var, true),
393 fn to_param(&self, ctx: &AssistContext, module: hir::Module) -> ast::Param {
394 let var = self.var.name(ctx.db()).unwrap().to_string();
395 let var_name = make::name(&var);
396 let pat = match self.kind() {
397 ParamKind::MutValue => make::ident_pat(false, true, var_name),
398 ParamKind::Value | ParamKind::SharedRef | ParamKind::MutRef => {
399 make::ext::simple_ident_pat(var_name)
403 let ty = make_ty(&self.ty, ctx, module);
404 let ty = match self.kind() {
405 ParamKind::Value | ParamKind::MutValue => ty,
406 ParamKind::SharedRef => make::ty_ref(ty, false),
407 ParamKind::MutRef => make::ty_ref(ty, true),
410 make::param(pat.into(), ty)
415 fn of_ty(ty: hir::Type, ctx: &AssistContext) -> Option<TryKind> {
417 // We favour Result for `expr?`
418 return Some(TryKind::Result { ty });
420 let adt = ty.as_adt()?;
421 let name = adt.name(ctx.db());
422 // FIXME: use lang items to determine if it is std type or user defined
423 // E.g. if user happens to define type named `Option`, we would have false positive
424 match name.to_string().as_str() {
425 "Option" => Some(TryKind::Option),
426 "Result" => Some(TryKind::Result { ty }),
433 fn make_result_handler(&self, expr: Option<ast::Expr>) -> ast::Expr {
435 FlowKind::Return(_) => make::expr_return(expr),
436 FlowKind::Break(_) => make::expr_break(expr),
437 FlowKind::Try { .. } => {
438 stdx::never!("cannot have result handler with try");
439 expr.unwrap_or_else(|| make::expr_return(None))
441 FlowKind::Continue => {
442 stdx::always!(expr.is_none(), "continue with value is not possible");
443 make::expr_continue()
448 fn expr_ty(&self, ctx: &AssistContext) -> Option<hir::Type> {
450 FlowKind::Return(Some(expr)) | FlowKind::Break(Some(expr)) => {
451 ctx.sema.type_of_expr(expr).map(TypeInfo::adjusted)
453 FlowKind::Try { .. } => {
454 stdx::never!("try does not have defined expr_ty");
463 fn parent(&self) -> Option<SyntaxNode> {
465 FunctionBody::Expr(expr) => expr.syntax().parent(),
466 FunctionBody::Span { parent, .. } => Some(parent.syntax().clone()),
470 fn from_expr(expr: ast::Expr) -> Option<Self> {
472 ast::Expr::BreakExpr(it) => it.expr().map(Self::Expr),
473 ast::Expr::ReturnExpr(it) => it.expr().map(Self::Expr),
474 ast::Expr::BlockExpr(it) if !it.is_standalone() => None,
475 expr => Some(Self::Expr(expr)),
479 fn from_range(parent: ast::StmtList, selected: TextRange) -> FunctionBody {
480 let full_body = parent.syntax().children_with_tokens();
482 let mut text_range = full_body
483 .map(|stmt| stmt.text_range())
484 .filter(|&stmt| selected.intersect(stmt).filter(|it| !it.is_empty()).is_some())
485 .reduce(|acc, stmt| acc.cover(stmt));
487 if let Some(tail_range) = parent
489 .map(|it| it.syntax().text_range())
490 .filter(|&it| selected.intersect(it).is_some())
492 text_range = Some(match text_range {
493 Some(text_range) => text_range.cover(tail_range),
497 Self::Span { parent, text_range: text_range.unwrap_or(selected) }
500 fn indent_level(&self) -> IndentLevel {
502 FunctionBody::Expr(expr) => IndentLevel::from_node(expr.syntax()),
503 FunctionBody::Span { parent, .. } => IndentLevel::from_node(parent.syntax()) + 1,
507 fn tail_expr(&self) -> Option<ast::Expr> {
509 FunctionBody::Expr(expr) => Some(expr.clone()),
510 FunctionBody::Span { parent, text_range } => {
511 let tail_expr = parent.tail_expr()?;
512 text_range.contains_range(tail_expr.syntax().text_range()).then(|| tail_expr)
517 fn walk_expr(&self, cb: &mut dyn FnMut(ast::Expr)) {
519 FunctionBody::Expr(expr) => walk_expr(expr, cb),
520 FunctionBody::Span { parent, text_range } => {
523 .filter(|stmt| text_range.contains_range(stmt.syntax().text_range()))
524 .filter_map(|stmt| match stmt {
525 ast::Stmt::ExprStmt(expr_stmt) => expr_stmt.expr(),
526 ast::Stmt::Item(_) => None,
527 ast::Stmt::LetStmt(stmt) => stmt.initializer(),
529 .for_each(|expr| walk_expr(&expr, cb));
530 if let Some(expr) = parent
532 .filter(|it| text_range.contains_range(it.syntax().text_range()))
534 walk_expr(&expr, cb);
540 fn preorder_expr(&self, cb: &mut dyn FnMut(WalkEvent<ast::Expr>) -> bool) {
542 FunctionBody::Expr(expr) => preorder_expr(expr, cb),
543 FunctionBody::Span { parent, text_range } => {
546 .filter(|stmt| text_range.contains_range(stmt.syntax().text_range()))
547 .filter_map(|stmt| match stmt {
548 ast::Stmt::ExprStmt(expr_stmt) => expr_stmt.expr(),
549 ast::Stmt::Item(_) => None,
550 ast::Stmt::LetStmt(stmt) => stmt.initializer(),
552 .for_each(|expr| preorder_expr(&expr, cb));
553 if let Some(expr) = parent
555 .filter(|it| text_range.contains_range(it.syntax().text_range()))
557 preorder_expr(&expr, cb);
563 fn walk_pat(&self, cb: &mut dyn FnMut(ast::Pat)) {
565 FunctionBody::Expr(expr) => walk_patterns_in_expr(expr, cb),
566 FunctionBody::Span { parent, text_range } => {
569 .filter(|stmt| text_range.contains_range(stmt.syntax().text_range()))
570 .for_each(|stmt| match stmt {
571 ast::Stmt::ExprStmt(expr_stmt) => {
572 if let Some(expr) = expr_stmt.expr() {
573 walk_patterns_in_expr(&expr, cb)
576 ast::Stmt::Item(_) => (),
577 ast::Stmt::LetStmt(stmt) => {
578 if let Some(pat) = stmt.pat() {
581 if let Some(expr) = stmt.initializer() {
582 walk_patterns_in_expr(&expr, cb);
586 if let Some(expr) = parent
588 .filter(|it| text_range.contains_range(it.syntax().text_range()))
590 walk_patterns_in_expr(&expr, cb);
596 fn text_range(&self) -> TextRange {
598 FunctionBody::Expr(expr) => expr.syntax().text_range(),
599 &FunctionBody::Span { text_range, .. } => text_range,
603 fn contains_range(&self, range: TextRange) -> bool {
604 self.text_range().contains_range(range)
607 fn precedes_range(&self, range: TextRange) -> bool {
608 self.text_range().end() <= range.start()
611 fn contains_node(&self, node: &SyntaxNode) -> bool {
612 self.contains_range(node.text_range())
617 /// Analyzes a function body, returning the used local variables that are referenced in it as well as
618 /// whether it contains an await expression.
621 sema: &Semantics<RootDatabase>,
622 ) -> (FxIndexSet<Local>, Option<ast::SelfParam>) {
623 let mut self_param = None;
624 let mut res = FxIndexSet::default();
625 let mut cb = |name_ref: Option<_>| {
627 match name_ref.and_then(|name_ref| NameRefClass::classify(sema, &name_ref)) {
629 NameRefClass::Definition(Definition::Local(local_ref))
630 | NameRefClass::FieldShorthand { local_ref, field_ref: _ },
634 let InFile { file_id, value } = local_ref.source(sema.db);
635 // locals defined inside macros are not relevant to us
636 if !file_id.is_macro() {
638 Either::Right(it) => {
639 self_param.replace(it);
642 res.insert(local_ref);
647 self.walk_expr(&mut |expr| match expr {
648 ast::Expr::PathExpr(path_expr) => {
649 cb(path_expr.path().and_then(|it| it.as_single_name_ref()))
651 ast::Expr::MacroCall(call) => {
652 if let Some(tt) = call.token_tree() {
654 .children_with_tokens()
655 .flat_map(SyntaxElement::into_token)
656 .filter(|it| it.kind() == SyntaxKind::IDENT)
657 .flat_map(|t| sema.descend_into_macros(t))
658 .for_each(|t| cb(t.parent().and_then(ast::NameRef::cast)));
666 fn analyze_container(&self, sema: &Semantics<RootDatabase>) -> Option<ContainerInfo> {
667 let mut ancestors = self.parent()?.ancestors();
668 let infer_expr_opt = |expr| sema.type_of_expr(&expr?).map(TypeInfo::adjusted);
669 let mut parent_loop = None;
670 let mut set_parent_loop = |loop_: &dyn ast::HasLoopBody| {
673 .map_or(false, |it| it.syntax().text_range().contains_range(self.text_range()))
675 parent_loop.get_or_insert(loop_.syntax().clone());
678 let (is_const, expr, ty) = loop {
679 let anc = ancestors.next()?;
682 ast::ClosureExpr(closure) => (false, closure.body(), infer_expr_opt(closure.body())),
683 ast::BlockExpr(block_expr) => {
684 let (constness, block) = match block_expr.modifier() {
685 Some(ast::BlockModifier::Const(_)) => (true, block_expr),
686 Some(ast::BlockModifier::Try(_)) => (false, block_expr),
687 Some(ast::BlockModifier::Label(label)) if label.lifetime().is_some() => (false, block_expr),
690 let expr = Some(ast::Expr::BlockExpr(block));
691 (constness, expr.clone(), infer_expr_opt(expr))
694 (fn_.const_token().is_some(), fn_.body().map(ast::Expr::BlockExpr), Some(sema.to_def(&fn_)?.ret_type(sema.db)))
696 ast::Static(statik) => {
697 (true, statik.body(), Some(sema.to_def(&statik)?.ty(sema.db)))
699 ast::ConstArg(ca) => {
700 (true, ca.expr(), infer_expr_opt(ca.expr()))
702 ast::Const(konst) => {
703 (true, konst.body(), Some(sema.to_def(&konst)?.ty(sema.db)))
705 ast::ConstParam(cp) => {
706 (true, cp.default_val(), Some(sema.to_def(&cp)?.ty(sema.db)))
708 ast::ConstBlockPat(cbp) => {
709 let expr = cbp.block_expr().map(ast::Expr::BlockExpr);
710 (true, expr.clone(), infer_expr_opt(expr))
712 ast::Variant(__) => return None,
713 ast::Meta(__) => return None,
714 ast::LoopExpr(it) => {
715 set_parent_loop(&it);
718 ast::ForExpr(it) => {
719 set_parent_loop(&it);
722 ast::WhileExpr(it) => {
723 set_parent_loop(&it);
730 let container_tail = match expr? {
731 ast::Expr::BlockExpr(block) => block.tail_expr(),
735 container_tail.zip(self.tail_expr()).map_or(false, |(container_tail, body_tail)| {
736 container_tail.syntax().text_range().contains_range(body_tail.syntax().text_range())
738 Some(ContainerInfo { is_in_tail, is_const, parent_loop, ret_type: ty })
741 fn return_ty(&self, ctx: &AssistContext) -> Option<RetType> {
742 match self.tail_expr() {
743 Some(expr) => ctx.sema.type_of_expr(&expr).map(TypeInfo::original).map(RetType::Expr),
744 None => Some(RetType::Stmt),
748 /// Local variables defined inside `body` that are accessed outside of it
751 ctx: &'a AssistContext,
753 ) -> impl Iterator<Item = OutlivedLocal> + 'a {
754 let parent = parent.clone();
755 let range = self.text_range();
756 locals_defined_in_body(&ctx.sema, self)
758 .filter_map(move |local| local_outlives_body(ctx, range, local, &parent))
761 /// Analyses the function body for external control flow.
762 fn external_control_flow(
765 container_info: &ContainerInfo,
766 ) -> Option<ControlFlow> {
767 let mut ret_expr = None;
768 let mut try_expr = None;
769 let mut break_expr = None;
770 let mut continue_expr = None;
771 let mut is_async = false;
772 let mut _is_unsafe = false;
774 let mut unsafe_depth = 0;
775 let mut loop_depth = 0;
777 self.preorder_expr(&mut |expr| {
778 let expr = match expr {
779 WalkEvent::Enter(e) => e,
780 WalkEvent::Leave(expr) => {
782 ast::Expr::LoopExpr(_)
783 | ast::Expr::ForExpr(_)
784 | ast::Expr::WhileExpr(_) => loop_depth -= 1,
785 ast::Expr::BlockExpr(block_expr) if block_expr.unsafe_token().is_some() => {
794 ast::Expr::LoopExpr(_) | ast::Expr::ForExpr(_) | ast::Expr::WhileExpr(_) => {
797 ast::Expr::BlockExpr(block_expr) if block_expr.unsafe_token().is_some() => {
800 ast::Expr::ReturnExpr(it) => {
803 ast::Expr::TryExpr(it) => {
806 ast::Expr::BreakExpr(it) if loop_depth == 0 => {
807 break_expr = Some(it);
809 ast::Expr::ContinueExpr(it) if loop_depth == 0 => {
810 continue_expr = Some(it);
812 ast::Expr::AwaitExpr(_) => is_async = true,
813 // FIXME: Do unsafe analysis on expression, sem highlighting knows this so we should be able
814 // to just lift that out of there
815 // expr if unsafe_depth ==0 && expr.is_unsafe => is_unsafe = true,
821 let kind = match (try_expr, ret_expr, break_expr, continue_expr) {
822 (Some(_), _, None, None) => {
823 let ret_ty = container_info.ret_type.clone()?;
824 let kind = TryKind::of_ty(ret_ty, ctx)?;
826 Some(FlowKind::Try { kind })
828 (Some(_), _, _, _) => {
829 cov_mark::hit!(external_control_flow_try_and_bc);
832 (None, Some(r), None, None) => Some(FlowKind::Return(r.expr())),
833 (None, Some(_), _, _) => {
834 cov_mark::hit!(external_control_flow_return_and_bc);
837 (None, None, Some(_), Some(_)) => {
838 cov_mark::hit!(external_control_flow_break_and_continue);
841 (None, None, Some(b), None) => Some(FlowKind::Break(b.expr())),
842 (None, None, None, Some(_)) => Some(FlowKind::Continue),
843 (None, None, None, None) => None,
846 Some(ControlFlow { kind, is_async, is_unsafe: _is_unsafe })
849 /// find variables that should be extracted as params
851 /// Computes additional info that affects param type and mutability
852 fn extracted_function_params(
855 container_info: &ContainerInfo,
856 locals: impl Iterator<Item = Local>,
859 .map(|local| (local, local.source(ctx.db())))
860 .filter(|(_, src)| is_defined_outside_of_body(ctx, self, src))
861 .filter_map(|(local, src)| match src.value {
862 Either::Left(src) => Some((local, src)),
863 Either::Right(_) => {
864 stdx::never!(false, "Local::is_self returned false, but source is SelfParam");
869 let usages = LocalUsages::find_local_usages(ctx, var);
870 let ty = var.ty(ctx.db());
872 let defined_outside_parent_loop = container_info
875 .map_or(true, |it| it.text_range().contains_range(src.syntax().text_range()));
877 let is_copy = ty.is_copy(ctx.db());
878 let has_usages = self.has_usages_after_body(&usages);
880 !ty.is_mutable_reference() && has_exclusive_usages(ctx, &usages, self);
881 // We can move the value into the function call if it's not used after the call,
882 // if the var is not used but defined outside a loop we are extracting from we can't move it either
883 // as the function will reuse it in the next iteration.
884 let move_local = (!has_usages && defined_outside_parent_loop) || ty.is_reference();
885 Param { var, ty, move_local, requires_mut, is_copy }
890 fn has_usages_after_body(&self, usages: &LocalUsages) -> bool {
891 usages.iter().any(|reference| self.precedes_range(reference.range))
895 /// checks if relevant var is used with `&mut` access inside body
896 fn has_exclusive_usages(ctx: &AssistContext, usages: &LocalUsages, body: &FunctionBody) -> bool {
899 .filter(|reference| body.contains_range(reference.range))
900 .any(|reference| reference_is_exclusive(reference, body, ctx))
903 /// checks if this reference requires `&mut` access inside node
904 fn reference_is_exclusive(
905 reference: &FileReference,
906 node: &dyn HasTokenAtOffset,
909 // we directly modify variable with set: `n = 0`, `n += 1`
910 if reference.category == Some(ReferenceCategory::Write) {
914 // we take `&mut` reference to variable: `&mut v`
915 let path = match path_element_of_reference(node, reference) {
917 None => return false,
920 expr_require_exclusive_access(ctx, &path).unwrap_or(false)
923 /// checks if this expr requires `&mut` access, recurses on field access
924 fn expr_require_exclusive_access(ctx: &AssistContext, expr: &ast::Expr) -> Option<bool> {
925 if let ast::Expr::MacroCall(_) = expr {
926 // FIXME: expand macro and check output for mutable usages of the variable?
930 let parent = expr.syntax().parent()?;
932 if let Some(bin_expr) = ast::BinExpr::cast(parent.clone()) {
933 if matches!(bin_expr.op_kind()?, ast::BinaryOp::Assignment { .. }) {
934 return Some(bin_expr.lhs()?.syntax() == expr.syntax());
939 if let Some(ref_expr) = ast::RefExpr::cast(parent.clone()) {
940 return Some(ref_expr.mut_token().is_some());
943 if let Some(method_call) = ast::MethodCallExpr::cast(parent.clone()) {
944 let func = ctx.sema.resolve_method_call(&method_call)?;
945 let self_param = func.self_param(ctx.db())?;
946 let access = self_param.access(ctx.db());
948 return Some(matches!(access, hir::Access::Exclusive));
951 if let Some(field) = ast::FieldExpr::cast(parent) {
952 return expr_require_exclusive_access(ctx, &field.into());
958 trait HasTokenAtOffset {
959 fn token_at_offset(&self, offset: TextSize) -> TokenAtOffset<SyntaxToken>;
962 impl HasTokenAtOffset for SyntaxNode {
963 fn token_at_offset(&self, offset: TextSize) -> TokenAtOffset<SyntaxToken> {
964 SyntaxNode::token_at_offset(self, offset)
968 impl HasTokenAtOffset for FunctionBody {
969 fn token_at_offset(&self, offset: TextSize) -> TokenAtOffset<SyntaxToken> {
971 FunctionBody::Expr(expr) => expr.syntax().token_at_offset(offset),
972 FunctionBody::Span { parent, text_range } => {
973 match parent.syntax().token_at_offset(offset) {
974 TokenAtOffset::None => TokenAtOffset::None,
975 TokenAtOffset::Single(t) => {
976 if text_range.contains_range(t.text_range()) {
977 TokenAtOffset::Single(t)
982 TokenAtOffset::Between(a, b) => {
984 text_range.contains_range(a.text_range()),
985 text_range.contains_range(b.text_range()),
987 (true, true) => TokenAtOffset::Between(a, b),
988 (true, false) => TokenAtOffset::Single(a),
989 (false, true) => TokenAtOffset::Single(b),
990 (false, false) => TokenAtOffset::None,
999 /// find relevant `ast::Expr` for reference
1003 /// `node` must cover `reference`, that is `node.text_range().contains_range(reference.range)`
1004 fn path_element_of_reference(
1005 node: &dyn HasTokenAtOffset,
1006 reference: &FileReference,
1007 ) -> Option<ast::Expr> {
1008 let token = node.token_at_offset(reference.range.start()).right_biased().or_else(|| {
1009 stdx::never!(false, "cannot find token at variable usage: {:?}", reference);
1012 let path = token.ancestors().find_map(ast::Expr::cast).or_else(|| {
1013 stdx::never!(false, "cannot find path parent of variable usage: {:?}", token);
1017 matches!(path, ast::Expr::PathExpr(_) | ast::Expr::MacroCall(_)),
1018 "unexpected expression type for variable usage: {:?}",
1024 /// list local variables defined inside `body`
1025 fn locals_defined_in_body(
1026 sema: &Semantics<RootDatabase>,
1027 body: &FunctionBody,
1028 ) -> FxIndexSet<Local> {
1029 // FIXME: this doesn't work well with macros
1030 // see https://github.com/rust-analyzer/rust-analyzer/pull/7535#discussion_r570048550
1031 let mut res = FxIndexSet::default();
1032 body.walk_pat(&mut |pat| {
1033 if let ast::Pat::IdentPat(pat) = pat {
1034 if let Some(local) = sema.to_def(&pat) {
1042 /// Returns usage details if local variable is used after(outside of) body
1043 fn local_outlives_body(
1044 ctx: &AssistContext,
1045 body_range: TextRange,
1047 parent: &SyntaxNode,
1048 ) -> Option<OutlivedLocal> {
1049 let usages = LocalUsages::find_local_usages(ctx, local);
1050 let mut has_mut_usages = false;
1051 let mut any_outlives = false;
1052 for usage in usages.iter() {
1053 if body_range.end() <= usage.range.start() {
1054 has_mut_usages |= reference_is_exclusive(usage, parent, ctx);
1055 any_outlives |= true;
1057 break; // no need to check more elements we have all the info we wanted
1064 Some(OutlivedLocal { local, mut_usage_outside_body: has_mut_usages })
1067 /// checks if the relevant local was defined before(outside of) body
1068 fn is_defined_outside_of_body(
1069 ctx: &AssistContext,
1070 body: &FunctionBody,
1071 src: &hir::InFile<Either<ast::IdentPat, ast::SelfParam>>,
1073 src.file_id.original_file(ctx.db()) == ctx.file_id()
1074 && !body.contains_node(either_syntax(&src.value))
1077 fn either_syntax(value: &Either<ast::IdentPat, ast::SelfParam>) -> &SyntaxNode {
1079 Either::Left(pat) => pat.syntax(),
1080 Either::Right(it) => it.syntax(),
1084 /// find where to put extracted function definition
1086 /// Function should be put right after returned node
1087 fn node_to_insert_after(body: &FunctionBody, anchor: Anchor) -> Option<SyntaxNode> {
1088 let node = match body {
1089 FunctionBody::Expr(e) => e.syntax(),
1090 FunctionBody::Span { parent, .. } => parent.syntax(),
1092 let mut ancestors = node.ancestors().peekable();
1093 let mut last_ancestor = None;
1094 while let Some(next_ancestor) = ancestors.next() {
1095 match next_ancestor.kind() {
1096 SyntaxKind::SOURCE_FILE => break,
1097 SyntaxKind::ITEM_LIST if !matches!(anchor, Anchor::Freestanding) => continue,
1098 SyntaxKind::ITEM_LIST => {
1099 if ancestors.peek().map(SyntaxNode::kind) == Some(SyntaxKind::MODULE) {
1103 SyntaxKind::ASSOC_ITEM_LIST if !matches!(anchor, Anchor::Method) => {
1106 SyntaxKind::ASSOC_ITEM_LIST => {
1107 if ancestors.peek().map(SyntaxNode::kind) == Some(SyntaxKind::IMPL) {
1113 last_ancestor = Some(next_ancestor);
1118 fn make_call(ctx: &AssistContext, fun: &Function, indent: IndentLevel) -> String {
1119 let ret_ty = fun.return_type(ctx);
1121 let args = make::arg_list(fun.params.iter().map(|param| param.to_arg(ctx)));
1122 let name = fun.name.clone();
1123 let mut call_expr = if fun.self_param.is_some() {
1124 let self_arg = make::expr_path(make::ext::ident_path("self"));
1125 make::expr_method_call(self_arg, name, args)
1127 let func = make::expr_path(make::path_unqualified(make::path_segment(name)));
1128 make::expr_call(func, args)
1131 let handler = FlowHandler::from_ret_ty(fun, &ret_ty);
1133 if fun.control_flow.is_async {
1134 call_expr = make::expr_await(call_expr);
1136 let expr = handler.make_call_expr(call_expr).indent(indent);
1138 let mut_modifier = |var: &OutlivedLocal| if var.mut_usage_outside_body { "mut " } else { "" };
1140 let mut buf = String::new();
1141 match fun.outliving_locals.as_slice() {
1144 format_to!(buf, "let {}{} = ", mut_modifier(var), var.local.name(ctx.db()).unwrap())
1147 buf.push_str("let (");
1148 let bindings = vars.iter().format_with(", ", |local, f| {
1149 f(&format_args!("{}{}", mut_modifier(local), local.local.name(ctx.db()).unwrap()))
1151 format_to!(buf, "{}", bindings);
1152 buf.push_str(") = ");
1156 format_to!(buf, "{}", expr);
1157 let insert_comma = fun
1160 .and_then(ast::MatchArm::cast)
1161 .map_or(false, |it| it.comma_token().is_none());
1164 } else if fun.ret_ty.is_unit() && (!fun.outliving_locals.is_empty() || !expr.is_block_like()) {
1172 Try { kind: TryKind },
1173 If { action: FlowKind },
1174 IfOption { action: FlowKind },
1175 MatchOption { none: FlowKind },
1176 MatchResult { err: FlowKind },
1180 fn from_ret_ty(fun: &Function, ret_ty: &FunType) -> FlowHandler {
1181 match &fun.control_flow.kind {
1182 None => FlowHandler::None,
1183 Some(flow_kind) => {
1184 let action = flow_kind.clone();
1185 if *ret_ty == FunType::Unit {
1187 FlowKind::Return(None) | FlowKind::Break(None) | FlowKind::Continue => {
1188 FlowHandler::If { action }
1190 FlowKind::Return(_) | FlowKind::Break(_) => {
1191 FlowHandler::IfOption { action }
1193 FlowKind::Try { kind } => FlowHandler::Try { kind: kind.clone() },
1197 FlowKind::Return(None) | FlowKind::Break(None) | FlowKind::Continue => {
1198 FlowHandler::MatchOption { none: action }
1200 FlowKind::Return(_) | FlowKind::Break(_) => {
1201 FlowHandler::MatchResult { err: action }
1203 FlowKind::Try { kind } => FlowHandler::Try { kind: kind.clone() },
1210 fn make_call_expr(&self, call_expr: ast::Expr) -> ast::Expr {
1212 FlowHandler::None => call_expr,
1213 FlowHandler::Try { kind: _ } => make::expr_try(call_expr),
1214 FlowHandler::If { action } => {
1215 let action = action.make_result_handler(None);
1216 let stmt = make::expr_stmt(action);
1217 let block = make::block_expr(iter::once(stmt.into()), None);
1218 let controlflow_break_path = make::path_from_text("ControlFlow::Break");
1219 let condition = make::condition(
1222 make::tuple_struct_pat(
1223 controlflow_break_path,
1224 iter::once(make::wildcard_pat().into()),
1229 make::expr_if(condition, block, None)
1231 FlowHandler::IfOption { action } => {
1232 let path = make::ext::ident_path("Some");
1233 let value_pat = make::ext::simple_ident_pat(make::name("value"));
1234 let pattern = make::tuple_struct_pat(path, iter::once(value_pat.into()));
1235 let cond = make::condition(call_expr, Some(pattern.into()));
1236 let value = make::expr_path(make::ext::ident_path("value"));
1237 let action_expr = action.make_result_handler(Some(value));
1238 let action_stmt = make::expr_stmt(action_expr);
1239 let then = make::block_expr(iter::once(action_stmt.into()), None);
1240 make::expr_if(cond, then, None)
1242 FlowHandler::MatchOption { none } => {
1243 let some_name = "value";
1246 let path = make::ext::ident_path("Some");
1247 let value_pat = make::ext::simple_ident_pat(make::name(some_name));
1248 let pat = make::tuple_struct_pat(path, iter::once(value_pat.into()));
1249 let value = make::expr_path(make::ext::ident_path(some_name));
1250 make::match_arm(iter::once(pat.into()), None, value)
1253 let path = make::ext::ident_path("None");
1254 let pat = make::path_pat(path);
1255 make::match_arm(iter::once(pat), None, none.make_result_handler(None))
1257 let arms = make::match_arm_list(vec![some_arm, none_arm]);
1258 make::expr_match(call_expr, arms)
1260 FlowHandler::MatchResult { err } => {
1261 let ok_name = "value";
1262 let err_name = "value";
1265 let path = make::ext::ident_path("Ok");
1266 let value_pat = make::ext::simple_ident_pat(make::name(ok_name));
1267 let pat = make::tuple_struct_pat(path, iter::once(value_pat.into()));
1268 let value = make::expr_path(make::ext::ident_path(ok_name));
1269 make::match_arm(iter::once(pat.into()), None, value)
1272 let path = make::ext::ident_path("Err");
1273 let value_pat = make::ext::simple_ident_pat(make::name(err_name));
1274 let pat = make::tuple_struct_pat(path, iter::once(value_pat.into()));
1275 let value = make::expr_path(make::ext::ident_path(err_name));
1277 iter::once(pat.into()),
1279 err.make_result_handler(Some(value)),
1282 let arms = make::match_arm_list(vec![ok_arm, err_arm]);
1283 make::expr_match(call_expr, arms)
1289 fn path_expr_from_local(ctx: &AssistContext, var: Local) -> ast::Expr {
1290 let name = var.name(ctx.db()).unwrap().to_string();
1291 make::expr_path(make::ext::ident_path(&name))
1295 ctx: &AssistContext,
1296 module: hir::Module,
1298 old_indent: IndentLevel,
1299 new_indent: IndentLevel,
1301 let mut fn_def = String::new();
1302 let params = fun.make_param_list(ctx, module);
1303 let ret_ty = fun.make_ret_ty(ctx, module);
1304 let body = make_body(ctx, old_indent, new_indent, fun);
1305 let const_kw = if fun.mods.is_const { "const " } else { "" };
1306 let async_kw = if fun.control_flow.is_async { "async " } else { "" };
1307 let unsafe_kw = if fun.control_flow.is_unsafe { "unsafe " } else { "" };
1308 match ctx.config.snippet_cap {
1309 Some(_) => format_to!(
1311 "\n\n{}{}{}{}fn $0{}{}",
1321 "\n\n{}{}{}{}fn {}{}",
1330 if let Some(ret_ty) = ret_ty {
1331 format_to!(fn_def, " {}", ret_ty);
1333 format_to!(fn_def, " {}", body);
1339 fn make_param_list(&self, ctx: &AssistContext, module: hir::Module) -> ast::ParamList {
1340 let self_param = self.self_param.clone();
1341 let params = self.params.iter().map(|param| param.to_param(ctx, module));
1342 make::param_list(self_param, params)
1345 fn make_ret_ty(&self, ctx: &AssistContext, module: hir::Module) -> Option<ast::RetType> {
1346 let fun_ty = self.return_type(ctx);
1347 let handler = if self.mods.is_in_tail {
1350 FlowHandler::from_ret_ty(self, &fun_ty)
1352 let ret_ty = match &handler {
1353 FlowHandler::None => {
1354 if matches!(fun_ty, FunType::Unit) {
1357 fun_ty.make_ty(ctx, module)
1359 FlowHandler::Try { kind: TryKind::Option } => {
1360 make::ext::ty_option(fun_ty.make_ty(ctx, module))
1362 FlowHandler::Try { kind: TryKind::Result { ty: parent_ret_ty } } => {
1363 let handler_ty = parent_ret_ty
1366 .map(|ty| make_ty(&ty, ctx, module))
1367 .unwrap_or_else(make::ty_placeholder);
1368 make::ext::ty_result(fun_ty.make_ty(ctx, module), handler_ty)
1370 FlowHandler::If { .. } => make::ty("ControlFlow<()>"),
1371 FlowHandler::IfOption { action } => {
1372 let handler_ty = action
1374 .map(|ty| make_ty(&ty, ctx, module))
1375 .unwrap_or_else(make::ty_placeholder);
1376 make::ext::ty_option(handler_ty)
1378 FlowHandler::MatchOption { .. } => make::ext::ty_option(fun_ty.make_ty(ctx, module)),
1379 FlowHandler::MatchResult { err } => {
1380 let handler_ty = err
1382 .map(|ty| make_ty(&ty, ctx, module))
1383 .unwrap_or_else(make::ty_placeholder);
1384 make::ext::ty_result(fun_ty.make_ty(ctx, module), handler_ty)
1387 Some(make::ret_type(ret_ty))
1392 fn make_ty(&self, ctx: &AssistContext, module: hir::Module) -> ast::Type {
1394 FunType::Unit => make::ty_unit(),
1395 FunType::Single(ty) => make_ty(ty, ctx, module),
1396 FunType::Tuple(types) => match types.as_slice() {
1398 stdx::never!("tuple type with 0 elements");
1402 stdx::never!("tuple type with 1 element");
1403 make_ty(ty, ctx, module)
1406 let types = types.iter().map(|ty| make_ty(ty, ctx, module));
1407 make::ty_tuple(types)
1415 ctx: &AssistContext,
1416 old_indent: IndentLevel,
1417 new_indent: IndentLevel,
1419 ) -> ast::BlockExpr {
1420 let ret_ty = fun.return_type(ctx);
1421 let handler = if fun.mods.is_in_tail {
1424 FlowHandler::from_ret_ty(fun, &ret_ty)
1427 let block = match &fun.body {
1428 FunctionBody::Expr(expr) => {
1429 let expr = rewrite_body_segment(ctx, &fun.params, &handler, expr.syntax());
1430 let expr = ast::Expr::cast(expr).unwrap();
1432 ast::Expr::BlockExpr(block) => {
1433 // If the extracted expression is itself a block, there is no need to wrap it inside another block.
1434 let block = block.dedent(old_indent);
1435 // Recreate the block for formatting consistency with other extracted functions.
1436 make::block_expr(block.statements(), block.tail_expr())
1439 let expr = expr.dedent(old_indent).indent(IndentLevel(1));
1441 make::block_expr(Vec::new(), Some(expr))
1445 FunctionBody::Span { parent, text_range } => {
1446 let mut elements: Vec<_> = parent
1448 .children_with_tokens()
1449 .filter(|it| text_range.contains_range(it.text_range()))
1450 .map(|it| match &it {
1451 syntax::NodeOrToken::Node(n) => syntax::NodeOrToken::Node(
1452 rewrite_body_segment(ctx, &fun.params, &handler, &n),
1458 let mut tail_expr = match &elements.last() {
1459 Some(element) => match element {
1460 syntax::NodeOrToken::Node(node) if ast::Expr::can_cast(node.kind()) => {
1461 ast::Expr::cast(node.clone())
1472 None => match fun.outliving_locals.as_slice() {
1475 tail_expr = Some(path_expr_from_local(ctx, var.local));
1478 let exprs = vars.iter().map(|var| path_expr_from_local(ctx, var.local));
1479 let expr = make::expr_tuple(exprs);
1480 tail_expr = Some(expr);
1485 let body_indent = IndentLevel(1);
1486 let elements: Vec<SyntaxElement> = elements
1488 .map(|node_or_token| match &node_or_token {
1489 syntax::NodeOrToken::Node(node) => match ast::Stmt::cast(node.clone()) {
1491 let indented = stmt.dedent(old_indent).indent(body_indent);
1492 let ast_node = indented.syntax().clone_subtree();
1493 syntax::NodeOrToken::Node(ast_node)
1499 .collect::<Vec<SyntaxElement>>();
1500 let tail_expr = tail_expr.map(|expr| expr.dedent(old_indent).indent(body_indent));
1502 make::hacky_block_expr_with_comments(elements, tail_expr)
1506 let block = match &handler {
1507 FlowHandler::None => block,
1508 FlowHandler::Try { kind } => {
1509 let block = with_default_tail_expr(block, make::expr_unit());
1510 map_tail_expr(block, |tail_expr| {
1511 let constructor = match kind {
1512 TryKind::Option => "Some",
1513 TryKind::Result { .. } => "Ok",
1515 let func = make::expr_path(make::ext::ident_path(constructor));
1516 let args = make::arg_list(iter::once(tail_expr));
1517 make::expr_call(func, args)
1520 FlowHandler::If { .. } => {
1521 let controlflow_continue = make::expr_call(
1522 make::expr_path(make::path_from_text("ControlFlow::Continue")),
1523 make::arg_list(iter::once(make::expr_unit())),
1525 with_tail_expr(block, controlflow_continue.into())
1527 FlowHandler::IfOption { .. } => {
1528 let none = make::expr_path(make::ext::ident_path("None"));
1529 with_tail_expr(block, none)
1531 FlowHandler::MatchOption { .. } => map_tail_expr(block, |tail_expr| {
1532 let some = make::expr_path(make::ext::ident_path("Some"));
1533 let args = make::arg_list(iter::once(tail_expr));
1534 make::expr_call(some, args)
1536 FlowHandler::MatchResult { .. } => map_tail_expr(block, |tail_expr| {
1537 let ok = make::expr_path(make::ext::ident_path("Ok"));
1538 let args = make::arg_list(iter::once(tail_expr));
1539 make::expr_call(ok, args)
1543 block.indent(new_indent)
1546 fn map_tail_expr(block: ast::BlockExpr, f: impl FnOnce(ast::Expr) -> ast::Expr) -> ast::BlockExpr {
1547 let tail_expr = match block.tail_expr() {
1548 Some(tail_expr) => tail_expr,
1549 None => return block,
1551 make::block_expr(block.statements(), Some(f(tail_expr)))
1554 fn with_default_tail_expr(block: ast::BlockExpr, tail_expr: ast::Expr) -> ast::BlockExpr {
1555 match block.tail_expr() {
1557 None => make::block_expr(block.statements(), Some(tail_expr)),
1561 fn with_tail_expr(block: ast::BlockExpr, tail_expr: ast::Expr) -> ast::BlockExpr {
1562 let stmt_tail = block.tail_expr().map(|expr| make::expr_stmt(expr).into());
1563 let stmts = block.statements().chain(stmt_tail);
1564 make::block_expr(stmts, Some(tail_expr))
1567 fn format_type(ty: &hir::Type, ctx: &AssistContext, module: hir::Module) -> String {
1568 ty.display_source_code(ctx.db(), module.into()).ok().unwrap_or_else(|| "_".to_string())
1571 fn make_ty(ty: &hir::Type, ctx: &AssistContext, module: hir::Module) -> ast::Type {
1572 let ty_str = format_type(ty, ctx, module);
1576 fn rewrite_body_segment(
1577 ctx: &AssistContext,
1579 handler: &FlowHandler,
1580 syntax: &SyntaxNode,
1582 let syntax = fix_param_usages(ctx, params, syntax);
1583 update_external_control_flow(handler, &syntax);
1587 /// change all usages to account for added `&`/`&mut` for some params
1588 fn fix_param_usages(ctx: &AssistContext, params: &[Param], syntax: &SyntaxNode) -> SyntaxNode {
1589 let mut usages_for_param: Vec<(&Param, Vec<ast::Expr>)> = Vec::new();
1591 let tm = TreeMutator::new(syntax);
1593 for param in params {
1594 if !param.kind().is_ref() {
1598 let usages = LocalUsages::find_local_usages(ctx, param.var);
1601 .filter(|reference| syntax.text_range().contains_range(reference.range))
1602 .filter_map(|reference| path_element_of_reference(syntax, reference))
1603 .map(|expr| tm.make_mut(&expr));
1605 usages_for_param.push((param, usages.collect()));
1608 let res = tm.make_syntax_mut(syntax);
1610 for (param, usages) in usages_for_param {
1611 for usage in usages {
1612 match usage.syntax().ancestors().skip(1).find_map(ast::Expr::cast) {
1613 Some(ast::Expr::MethodCallExpr(_) | ast::Expr::FieldExpr(_)) => {
1616 Some(ast::Expr::RefExpr(node))
1617 if param.kind() == ParamKind::MutRef && node.mut_token().is_some() =>
1619 ted::replace(node.syntax(), node.expr().unwrap().syntax());
1621 Some(ast::Expr::RefExpr(node))
1622 if param.kind() == ParamKind::SharedRef && node.mut_token().is_none() =>
1624 ted::replace(node.syntax(), node.expr().unwrap().syntax());
1627 let p = &make::expr_prefix(T![*], usage.clone()).clone_for_update();
1628 ted::replace(usage.syntax(), p.syntax())
1637 fn update_external_control_flow(handler: &FlowHandler, syntax: &SyntaxNode) {
1638 let mut nested_loop = None;
1639 let mut nested_scope = None;
1640 for event in syntax.preorder() {
1642 WalkEvent::Enter(e) => match e.kind() {
1643 SyntaxKind::LOOP_EXPR | SyntaxKind::WHILE_EXPR | SyntaxKind::FOR_EXPR => {
1644 if nested_loop.is_none() {
1645 nested_loop = Some(e.clone());
1650 | SyntaxKind::STATIC
1652 | SyntaxKind::MODULE => {
1653 if nested_scope.is_none() {
1654 nested_scope = Some(e.clone());
1659 WalkEvent::Leave(e) => {
1660 if nested_scope.is_none() {
1661 if let Some(expr) = ast::Expr::cast(e.clone()) {
1663 ast::Expr::ReturnExpr(return_expr) if nested_scope.is_none() => {
1664 let expr = return_expr.expr();
1665 if let Some(replacement) = make_rewritten_flow(handler, expr) {
1666 ted::replace(return_expr.syntax(), replacement.syntax())
1669 ast::Expr::BreakExpr(break_expr) if nested_loop.is_none() => {
1670 let expr = break_expr.expr();
1671 if let Some(replacement) = make_rewritten_flow(handler, expr) {
1672 ted::replace(break_expr.syntax(), replacement.syntax())
1675 ast::Expr::ContinueExpr(continue_expr) if nested_loop.is_none() => {
1676 if let Some(replacement) = make_rewritten_flow(handler, None) {
1677 ted::replace(continue_expr.syntax(), replacement.syntax())
1687 if nested_loop.as_ref() == Some(&e) {
1690 if nested_scope.as_ref() == Some(&e) {
1691 nested_scope = None;
1698 fn make_rewritten_flow(handler: &FlowHandler, arg_expr: Option<ast::Expr>) -> Option<ast::Expr> {
1699 let value = match handler {
1700 FlowHandler::None | FlowHandler::Try { .. } => return None,
1701 FlowHandler::If { .. } => make::expr_call(
1702 make::expr_path(make::path_from_text("ControlFlow::Break")),
1703 make::arg_list(iter::once(make::expr_unit())),
1705 FlowHandler::IfOption { .. } => {
1706 let expr = arg_expr.unwrap_or_else(|| make::expr_tuple(Vec::new()));
1707 let args = make::arg_list(iter::once(expr));
1708 make::expr_call(make::expr_path(make::ext::ident_path("Some")), args)
1710 FlowHandler::MatchOption { .. } => make::expr_path(make::ext::ident_path("None")),
1711 FlowHandler::MatchResult { .. } => {
1712 let expr = arg_expr.unwrap_or_else(|| make::expr_tuple(Vec::new()));
1713 let args = make::arg_list(iter::once(expr));
1714 make::expr_call(make::expr_path(make::ext::ident_path("Err")), args)
1717 Some(make::expr_return(Some(value)).clone_for_update())
1722 use crate::tests::{check_assist, check_assist_not_applicable};
1727 fn no_args_from_binary_expr() {
1740 fn $0fun_name() -> i32 {
1748 fn no_args_from_binary_expr_in_module() {
1764 fn $0fun_name() -> i32 {
1773 fn no_args_from_binary_expr_indented() {
1786 fn $0fun_name() -> i32 {
1794 fn no_args_from_stmt_with_last_expr() {
1810 fn $0fun_name() -> i32 {
1819 fn no_args_from_stmt_unit() {
1867 fn no_args_if_else() {
1872 $0if true { 1 } else { 2 }$0
1880 fn $0fun_name() -> i32 {
1881 if true { 1 } else { 2 }
1888 fn no_args_if_let_else() {
1893 $0if let true = false { 1 } else { 2 }$0
1901 fn $0fun_name() -> i32 {
1902 if let true = false { 1 } else { 2 }
1909 fn no_args_match() {
1925 fn $0fun_name() -> i32 {
1936 fn no_args_while() {
1962 $0for v in &[0, 1] { }$0
1971 for v in &[0, 1] { }
1978 fn no_args_from_loop_unit() {
1993 fn $0fun_name() -> ! {
2003 fn no_args_from_loop_with_return() {
2019 fn $0fun_name() -> i32 {
2030 fn no_args_from_match() {
2035 let v: i32 = $0match Some(1) {
2043 let v: i32 = fun_name();
2046 fn $0fun_name() -> i32 {
2057 fn extract_partial_block_single_line() {
2063 let mut v = $0n * n;$0
2070 let mut v = fun_name(n);
2074 fn $0fun_name(n: i32) -> i32 {
2083 fn extract_partial_block() {
2090 let mut v = m $0* n;
2100 let (mut v, mut w) = fun_name(m, n);
2105 fn $0fun_name(m: i32, n: i32) -> (i32, i32) {
2115 fn argument_form_expr() {
2130 fn $0fun_name(n: u32) -> u32 {
2138 fn argument_used_twice_form_expr() {
2153 fn $0fun_name(n: u32) -> u32 {
2161 fn two_arguments_form_expr() {
2178 fn $0fun_name(n: u32, m: u32) -> u32 {
2186 fn argument_and_locals() {
2202 fn $0fun_name(n: u32) -> u32 {
2211 fn in_comment_is_not_applicable() {
2212 cov_mark::check!(extract_function_in_comment_is_not_applicable);
2213 check_assist_not_applicable(extract_function, r"fn main() { 1 + /* $0comment$0 */ 1; }");
2217 fn part_of_expr_stmt() {
2230 fn $0fun_name() -> i32 {
2238 fn function_expr() {
2259 fn extract_from_nested() {
2265 let tuple = match x {
2266 true => ($02 + 2$0, true)
2274 let tuple = match x {
2275 true => (fun_name(), true)
2280 fn $0fun_name() -> i32 {
2288 fn param_from_closure() {
2293 let lambda = |x: u32| $0x * 2$0;
2298 let lambda = |x: u32| fun_name(x);
2301 fn $0fun_name(x: u32) -> u32 {
2309 fn extract_return_stmt() {
2322 fn $0fun_name() -> u32 {
2330 fn does_not_add_extra_whitespace() {
2347 fn $0fun_name() -> u32 {
2372 fn $0fun_name() -> i32 {
2385 let v = $00f32 as u32$0;
2393 fn $0fun_name() -> u32 {
2401 fn return_not_applicable() {
2402 check_assist_not_applicable(extract_function, r"fn foo() { $0return$0; } ");
2406 fn method_to_freestanding() {
2413 fn foo(&self) -> i32 {
2422 fn foo(&self) -> i32 {
2427 fn $0fun_name() -> i32 {
2435 fn method_with_reference() {
2439 struct S { f: i32 };
2442 fn foo(&self) -> i32 {
2448 struct S { f: i32 };
2451 fn foo(&self) -> i32 {
2455 fn $0fun_name(&self) -> i32 {
2464 fn method_with_mut() {
2468 struct S { f: i32 };
2477 struct S { f: i32 };
2484 fn $0fun_name(&mut self) {
2493 fn variable_defined_inside_and_used_after_no_ret() {
2506 let k = fun_name(n);
2510 fn $0fun_name(n: i32) -> i32 {
2519 fn variable_defined_inside_and_used_after_mutably_no_ret() {
2525 $0let mut k = n * n;$0
2532 let mut k = fun_name(n);
2536 fn $0fun_name(n: i32) -> i32 {
2545 fn two_variables_defined_inside_and_used_after_no_ret() {
2559 let (k, m) = fun_name(n);
2563 fn $0fun_name(n: i32) -> (i32, i32) {
2573 fn multi_variables_defined_inside_and_used_after_mutably_no_ret() {
2579 $0let mut k = n * n;
2590 let (mut k, mut m, o) = fun_name(n);
2595 fn $0fun_name(n: i32) -> (i32, i32, i32) {
2607 fn nontrivial_patterns_define_variables() {
2611 struct Counter(i32);
2613 $0let Counter(n) = Counter(0);$0
2618 struct Counter(i32);
2624 fn $0fun_name() -> i32 {
2625 let Counter(n) = Counter(0);
2633 fn struct_with_two_fields_pattern_define_variables() {
2637 struct Counter { n: i32, m: i32 };
2639 $0let Counter { n, m: k } = Counter { n: 1, m: 2 };$0
2644 struct Counter { n: i32, m: i32 };
2646 let (n, k) = fun_name();
2650 fn $0fun_name() -> (i32, i32) {
2651 let Counter { n, m: k } = Counter { n: 1, m: 2 };
2659 fn mut_var_from_outer_scope() {
2676 fn $0fun_name(n: &mut i32) {
2684 fn mut_field_from_outer_scope() {
2690 let mut c = C { n: 0 };
2698 let mut c = C { n: 0 };
2703 fn $0fun_name(c: &mut C) {
2711 fn mut_nested_field_from_outer_scope() {
2718 let mut c = C { p: P { n: 0 } };
2719 let mut v = C { p: P { n: 0 } };
2720 let u = C { p: P { n: 0 } };
2722 let r = &mut v.p.n;$0
2723 let m = c.p.n + v.p.n + u.p.n;
2730 let mut c = C { p: P { n: 0 } };
2731 let mut v = C { p: P { n: 0 } };
2732 let u = C { p: P { n: 0 } };
2733 fun_name(&mut c, &u, &mut v);
2734 let m = c.p.n + v.p.n + u.p.n;
2737 fn $0fun_name(c: &mut C, u: &C, v: &mut C) {
2746 fn mut_param_many_usages_stmt() {
2752 fn succ(&self) -> Self;
2753 fn inc(&mut self) -> Self { let v = self.succ(); *self = v; v }
2756 fn succ(&self) -> Self { *self + 1 }
2775 fn succ(&self) -> Self;
2776 fn inc(&mut self) -> Self { let v = self.succ(); *self = v; v }
2779 fn succ(&self) -> Self { *self + 1 }
2787 fn $0fun_name(n: &mut i32) {
2803 fn mut_param_many_usages_expr() {
2809 fn succ(&self) -> Self;
2810 fn inc(&mut self) -> Self { let v = self.succ(); *self = v; v }
2813 fn succ(&self) -> Self { *self + 1 }
2834 fn succ(&self) -> Self;
2835 fn inc(&mut self) -> Self { let v = self.succ(); *self = v; v }
2838 fn succ(&self) -> Self { *self + 1 }
2846 fn $0fun_name(n: &mut i32) {
2862 fn mut_param_by_value() {
2877 fn $0fun_name(mut n: i32) {
2885 fn mut_param_because_of_mut_ref() {
2903 fn $0fun_name(n: &mut i32) {
2912 fn mut_param_by_value_because_of_mut_ref() {
2928 fn $0fun_name(mut n: i32) {
2937 fn mut_method_call() {
2945 fn inc(&mut self) { *self += 1 }
2957 fn inc(&mut self) { *self += 1 }
2964 fn $0fun_name(mut n: i32) {
2972 fn shared_method_call() {
2980 fn succ(&self) { *self + 1 }
2992 fn succ(&self) { *self + 1 }
2999 fn $0fun_name(n: i32) {
3007 fn mut_method_call_with_other_receiver() {
3012 fn inc(&mut self, n: i32);
3015 fn inc(&mut self, n: i32) { *self += n }
3025 fn inc(&mut self, n: i32);
3028 fn inc(&mut self, n: i32) { *self += n }
3035 fn $0fun_name(n: i32) {
3044 fn non_copy_without_usages_after() {
3048 struct Counter(i32);
3055 struct Counter(i32);
3061 fn $0fun_name(c: Counter) {
3069 fn non_copy_used_after() {
3073 struct Counter(i32);
3081 struct Counter(i32);
3088 fn $0fun_name(c: &Counter) {
3096 fn copy_used_after() {
3114 fn $0fun_name(n: i32) {
3122 fn copy_custom_used_after() {
3126 //- minicore: copy, derive
3127 #[derive(Clone, Copy)]
3128 struct Counter(i32);
3136 #[derive(Clone, Copy)]
3137 struct Counter(i32);
3144 fn $0fun_name(c: Counter) {
3152 fn indented_stmts() {
3183 fn indented_stmts_inside_mod() {
3222 //- minicore: option
3237 let k = match fun_name(n) {
3238 Some(value) => value,
3245 fn $0fun_name(n: i32) -> Option<i32> {
3256 fn return_to_parent() {
3260 //- minicore: copy, result
3272 let k = match fun_name(n) {
3274 Err(value) => return value,
3279 fn $0fun_name(n: i32) -> Result<i32, i64> {
3290 fn break_and_continue() {
3291 cov_mark::check!(external_control_flow_break_and_continue);
3292 check_assist_not_applicable(
3311 fn return_and_break() {
3312 cov_mark::check!(external_control_flow_return_and_bc);
3313 check_assist_not_applicable(
3332 fn break_loop_with_if() {
3348 use core::ops::ControlFlow;
3353 if let ControlFlow::Break(_) = fun_name(&mut n) {
3360 fn $0fun_name(n: &mut i32) -> ControlFlow<()> {
3362 return ControlFlow::Break(());
3364 ControlFlow::Continue(())
3371 fn break_loop_nested() {
3388 use core::ops::ControlFlow;
3393 if let ControlFlow::Break(_) = fun_name(n) {
3400 fn $0fun_name(n: i32) -> ControlFlow<()> {
3403 return ControlFlow::Break(());
3405 ControlFlow::Continue(())
3412 fn return_from_nested_loop() {
3432 let m = match fun_name() {
3433 Some(value) => value,
3440 fn $0fun_name() -> Option<i32> {
3453 fn break_from_nested_loop() {
3478 fn $0fun_name() -> i32 {
3491 fn break_from_nested_and_outer_loops() {
3514 let m = match fun_name() {
3515 Some(value) => value,
3522 fn $0fun_name() -> Option<i32> {
3538 fn return_from_nested_fn() {
3563 fn $0fun_name() -> i32 {
3576 fn break_with_value() {
3596 if let Some(value) = fun_name() {
3603 fn $0fun_name() -> Option<i32> {
3616 fn break_with_value_and_return() {
3636 let m = match fun_name() {
3638 Err(value) => break value,
3644 fn $0fun_name() -> Result<i32, i64> {
3661 //- minicore: option
3662 fn bar() -> Option<i32> { None }
3663 fn foo() -> Option<()> {
3672 fn bar() -> Option<i32> { None }
3673 fn foo() -> Option<()> {
3675 let m = fun_name()?;
3680 fn $0fun_name() -> Option<i32> {
3690 fn try_option_unit() {
3694 //- minicore: option
3695 fn foo() -> Option<()> {
3704 fn foo() -> Option<()> {
3711 fn $0fun_name() -> Option<()> {
3725 //- minicore: result
3726 fn foo() -> Result<(), i64> {
3735 fn foo() -> Result<(), i64> {
3737 let m = fun_name()?;
3742 fn $0fun_name() -> Result<i32, i64> {
3752 fn try_option_with_return() {
3756 //- minicore: option
3757 fn foo() -> Option<()> {
3769 fn foo() -> Option<()> {
3771 let m = fun_name()?;
3776 fn $0fun_name() -> Option<i32> {
3789 fn try_result_with_return() {
3793 //- minicore: result
3794 fn foo() -> Result<(), i64> {
3806 fn foo() -> Result<(), i64> {
3808 let m = fun_name()?;
3813 fn $0fun_name() -> Result<i32, i64> {
3826 fn try_and_break() {
3827 cov_mark::check!(external_control_flow_try_and_bc);
3828 check_assist_not_applicable(
3831 //- minicore: option
3832 fn foo() -> Option<()> {
3848 fn try_and_return_ok() {
3852 //- minicore: result
3853 fn foo() -> Result<(), i64> {
3865 fn foo() -> Result<(), i64> {
3867 let m = fun_name()?;
3872 fn $0fun_name() -> Result<i32, i64> {
3885 fn param_usage_in_macro() {
3890 ($val:expr) => { $val };
3895 $0let k = n * m!(n);$0
3901 ($val:expr) => { $val };
3906 let k = fun_name(n);
3910 fn $0fun_name(n: i32) -> i32 {
3919 fn extract_with_await() {
3924 $0some_function().await;$0
3927 async fn some_function() {
3936 async fn $0fun_name() {
3937 some_function().await;
3940 async fn some_function() {
3948 fn extract_with_await_and_result_not_producing_match_expr() {
3952 async fn foo() -> Result<(), ()> {
3958 async fn foo() -> Result<(), ()> {
3962 async fn $0fun_name() -> _ {
3971 fn extract_with_await_and_result_producing_match_expr() {
3975 async fn foo() -> i32 {
3978 let k = async { 1 }.await;
3988 async fn foo() -> i32 {
3991 let m = match fun_name().await {
3993 Err(value) => break value,
3999 async fn $0fun_name() -> Result<i32, i32> {
4000 let k = async { 1 }.await;
4012 fn extract_with_await_in_args() {
4017 $0function_call("a", some_function().await);$0
4020 async fn some_function() {
4029 async fn $0fun_name() {
4030 function_call("a", some_function().await);
4033 async fn some_function() {
4041 fn extract_does_not_extract_standalone_blocks() {
4042 check_assist_not_applicable(
4051 fn extract_adds_comma_for_match_arm() {
4070 fn $0fun_name() -> i32 {
4093 fn $0fun_name() -> i32 {
4101 fn extract_does_not_tear_comments_apart() {
4128 fn extract_does_not_wrap_res_in_res() {
4132 //- minicore: result
4133 fn foo() -> Result<(), i64> {
4134 $0Result::<i32, i64>::Ok(0)?;
4139 fn foo() -> Result<(), i64> {
4143 fn $0fun_name() -> Result<(), i64> {
4144 Result::<i32, i64>::Ok(0)?;
4152 fn extract_knows_const() {
4165 const fn $0fun_name() {
4182 const fn $0fun_name() {
4190 fn extract_does_not_move_outer_loop_vars() {
4209 fn $0fun_name(x: &mut i32) {
4232 fn $0fun_name(mut x: i32) {
4259 fn $0fun_name(x: &mut i32) {
4266 // regression test for #9822
4268 fn extract_mut_ref_param_has_no_mut_binding_in_loop() {
4274 fn foo(&mut self) {}
4288 fn foo(&mut self) {}
4299 fn $0fun_name(y: &mut Foo) {
4307 fn extract_with_macro_arg() {
4312 ($val:expr) => { $val };
4321 ($val:expr) => { $val };
4328 fn $0fun_name(bar: &str) {
4336 fn unresolveable_types_default_to_placeholder() {
4341 let a = __unresolved;
4347 let a = __unresolved;
4348 let _ = fun_name(a);
4351 fn $0fun_name(a: _) -> _ {
4359 fn reference_mutable_param_with_further_usages() {
4367 pub fn testfn(arg: &mut Foo) {
4369 // Simulating access after the extracted portion
4378 pub fn testfn(arg: &mut Foo) {
4380 // Simulating access after the extracted portion
4384 fn $0fun_name(arg: &mut Foo) {
4392 fn reference_mutable_param_without_further_usages() {
4400 pub fn testfn(arg: &mut Foo) {
4409 pub fn testfn(arg: &mut Foo) {
4413 fn $0fun_name(arg: &mut Foo) {
4421 fn extract_function_copies_comment_at_start() {
4446 fn extract_function_copies_comment_in_between() {
4475 fn extract_function_copies_comment_at_end() {
4500 fn extract_function_copies_comment_indented() {
4529 fn extract_function_does_not_preserve_whitespace() {
4555 fn extract_function_long_form_comment() {