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 println!("initial node: {:?}", node);
74 let node = match node {
75 syntax::NodeOrToken::Node(n) => n,
76 syntax::NodeOrToken::Token(t) => t.parent()?,
79 println!("next node: {:?}", node);
80 let body = extraction_target(&node, range)?;
81 println!("body: {:?}", body);
82 let container_info = body.analyze_container(&ctx.sema)?;
84 let (locals_used, self_param) = body.analyze(&ctx.sema);
86 let anchor = if self_param.is_some() { Anchor::Method } else { Anchor::Freestanding };
87 let insert_after = node_to_insert_after(&body, anchor)?;
88 let module = ctx.sema.scope(&insert_after).module()?;
90 let ret_ty = body.return_ty(ctx)?;
91 let control_flow = body.external_control_flow(ctx, &container_info)?;
92 let ret_values = body.ret_values(ctx, node.parent().as_ref().unwrap_or(&node));
94 let target_range = body.text_range();
96 let scope = ImportScope::find_insert_use_container(&node, &ctx.sema)?;
99 AssistId("extract_function", crate::AssistKind::RefactorExtract),
100 "Extract into function",
103 let outliving_locals: Vec<_> = ret_values.collect();
104 if stdx::never!(!outliving_locals.is_empty() && !ret_ty.is_unit()) {
105 // We should not have variables that outlive body if we have expression block
110 body.extracted_function_params(ctx, &container_info, locals_used.iter().copied());
113 name: make::name_ref("fun_name"),
120 mods: container_info,
123 let new_indent = IndentLevel::from_node(&insert_after);
124 let old_indent = fun.body.indent_level();
126 builder.replace(target_range, make_call(ctx, &fun, old_indent));
128 let fn_def = format_function(ctx, module, &fun, old_indent, new_indent);
129 let insert_offset = insert_after.text_range().end();
131 if fn_def.contains("ControlFlow") {
132 let scope = match scope {
133 ImportScope::File(it) => ImportScope::File(builder.make_mut(it)),
134 ImportScope::Module(it) => ImportScope::Module(builder.make_mut(it)),
135 ImportScope::Block(it) => ImportScope::Block(builder.make_mut(it)),
138 let control_flow_enum =
139 FamousDefs(&ctx.sema, Some(module.krate())).core_ops_ControlFlow();
141 if let Some(control_flow_enum) = control_flow_enum {
142 let mod_path = module.find_use_path_prefixed(
144 ModuleDef::from(control_flow_enum),
145 ctx.config.insert_use.prefix_kind,
148 if let Some(mod_path) = mod_path {
149 insert_use(&scope, mod_path_to_ast(&mod_path), &ctx.config.insert_use);
154 match ctx.config.snippet_cap {
155 Some(cap) => builder.insert_snippet(cap, insert_offset, fn_def),
156 None => builder.insert(insert_offset, fn_def),
162 /// Try to guess what user wants to extract
164 /// We have basically have two cases:
165 /// * We want whole node, like `loop {}`, `2 + 2`, `{ let n = 1; }` exprs.
166 /// Then we can use `ast::Expr`
167 /// * We want a few statements for a block. E.g.
169 /// fn foo() -> i32 {
179 fn extraction_target(node: &SyntaxNode, selection_range: TextRange) -> Option<FunctionBody> {
180 if let Some(stmt) = ast::Stmt::cast(node.clone()) {
182 ast::Stmt::Item(_) => None,
183 ast::Stmt::ExprStmt(_) | ast::Stmt::LetStmt(_) => Some(FunctionBody::from_range(
184 node.parent().and_then(ast::StmtList::cast)?,
190 println!("node: {:?}", node);
192 // Covering element returned the parent block of one or multiple statements that have been selected
193 if let Some(stmt_list) = ast::StmtList::cast(node.clone()) {
194 if let Some(block_expr) = stmt_list.syntax().parent().and_then(ast::BlockExpr::cast) {
195 if block_expr.syntax().text_range() == selection_range {
196 return FunctionBody::from_expr(block_expr.into());
200 // Extract the full statements.
201 println!("stmt_list: {:?}", stmt_list);
202 println!("selection_range: {:?}", selection_range);
203 return Some(FunctionBody::from_range(stmt_list, selection_range));
206 let expr = ast::Expr::cast(node.clone())?;
207 // A node got selected fully
208 if node.text_range() == selection_range {
209 return FunctionBody::from_expr(expr);
212 node.ancestors().find_map(ast::Expr::cast).and_then(FunctionBody::from_expr)
218 self_param: Option<ast::SelfParam>,
220 control_flow: ControlFlow,
223 outliving_locals: Vec<OutlivedLocal>,
236 #[derive(Debug, Clone, Copy, PartialEq, Eq)]
244 #[derive(Debug, Eq, PartialEq)]
248 Tuple(Vec<hir::Type>),
251 /// Where to put extracted function definition
254 /// Extract free function and put right after current top-level function
256 /// Extract method and put right after current function in the impl-block
260 // FIXME: ControlFlow and ContainerInfo both track some function modifiers, feels like these two should
261 // probably be merged somehow.
264 kind: Option<FlowKind>,
269 /// The thing whose expression we are extracting from. Can be a function, const, static, const arg, ...
270 #[derive(Clone, Debug)]
271 struct ContainerInfo {
274 parent_loop: Option<SyntaxNode>,
275 /// The function's return type, const's type etc.
276 ret_type: Option<hir::Type>,
279 /// Control flow that is exported from extracted function
291 #[derive(Debug, Clone)]
293 /// Return with value (`return $expr;`)
294 Return(Option<ast::Expr>),
298 /// Break with value (`break $expr;`)
299 Break(Option<ast::Expr>),
304 #[derive(Debug, Clone)]
307 Result { ty: hir::Type },
317 fn is_unit(&self) -> bool {
319 RetType::Expr(ty) => ty.is_unit(),
320 RetType::Stmt => true,
325 /// Semantically same as `ast::Expr`, but preserves identity when using only part of the Block
326 /// This is the future function body, the part that is being extracted.
330 Span { parent: ast::StmtList, text_range: TextRange },
334 struct OutlivedLocal {
336 mut_usage_outside_body: bool,
339 /// Container of local variable usages
341 /// Semanticall same as `UsageSearchResult`, but provides more convenient interface
342 struct LocalUsages(ide_db::search::UsageSearchResult);
345 fn find_local_usages(ctx: &AssistContext, var: Local) -> Self {
347 Definition::Local(var)
349 .in_scope(SearchScope::single_file(ctx.file_id()))
354 fn iter(&self) -> impl Iterator<Item = &FileReference> + '_ {
355 self.0.iter().flat_map(|(_, rs)| rs)
360 fn return_type(&self, ctx: &AssistContext) -> FunType {
362 RetType::Expr(ty) if ty.is_unit() => FunType::Unit,
363 RetType::Expr(ty) => FunType::Single(ty.clone()),
364 RetType::Stmt => match self.outliving_locals.as_slice() {
366 [var] => FunType::Single(var.local.ty(ctx.db())),
368 let types = vars.iter().map(|v| v.local.ty(ctx.db())).collect();
369 FunType::Tuple(types)
377 fn is_ref(&self) -> bool {
378 matches!(self, ParamKind::SharedRef | ParamKind::MutRef)
383 fn kind(&self) -> ParamKind {
384 match (self.move_local, self.requires_mut, self.is_copy) {
385 (false, true, _) => ParamKind::MutRef,
386 (false, false, false) => ParamKind::SharedRef,
387 (true, true, _) => ParamKind::MutValue,
388 (_, false, _) => ParamKind::Value,
392 fn to_arg(&self, ctx: &AssistContext) -> ast::Expr {
393 let var = path_expr_from_local(ctx, self.var);
395 ParamKind::Value | ParamKind::MutValue => var,
396 ParamKind::SharedRef => make::expr_ref(var, false),
397 ParamKind::MutRef => make::expr_ref(var, true),
401 fn to_param(&self, ctx: &AssistContext, module: hir::Module) -> ast::Param {
402 let var = self.var.name(ctx.db()).unwrap().to_string();
403 let var_name = make::name(&var);
404 let pat = match self.kind() {
405 ParamKind::MutValue => make::ident_pat(false, true, var_name),
406 ParamKind::Value | ParamKind::SharedRef | ParamKind::MutRef => {
407 make::ext::simple_ident_pat(var_name)
411 let ty = make_ty(&self.ty, ctx, module);
412 let ty = match self.kind() {
413 ParamKind::Value | ParamKind::MutValue => ty,
414 ParamKind::SharedRef => make::ty_ref(ty, false),
415 ParamKind::MutRef => make::ty_ref(ty, true),
418 make::param(pat.into(), ty)
423 fn of_ty(ty: hir::Type, ctx: &AssistContext) -> Option<TryKind> {
425 // We favour Result for `expr?`
426 return Some(TryKind::Result { ty });
428 let adt = ty.as_adt()?;
429 let name = adt.name(ctx.db());
430 // FIXME: use lang items to determine if it is std type or user defined
431 // E.g. if user happens to define type named `Option`, we would have false positive
432 match name.to_string().as_str() {
433 "Option" => Some(TryKind::Option),
434 "Result" => Some(TryKind::Result { ty }),
441 fn make_result_handler(&self, expr: Option<ast::Expr>) -> ast::Expr {
443 FlowKind::Return(_) => make::expr_return(expr),
444 FlowKind::Break(_) => make::expr_break(expr),
445 FlowKind::Try { .. } => {
446 stdx::never!("cannot have result handler with try");
447 expr.unwrap_or_else(|| make::expr_return(None))
449 FlowKind::Continue => {
450 stdx::always!(expr.is_none(), "continue with value is not possible");
451 make::expr_continue()
456 fn expr_ty(&self, ctx: &AssistContext) -> Option<hir::Type> {
458 FlowKind::Return(Some(expr)) | FlowKind::Break(Some(expr)) => {
459 ctx.sema.type_of_expr(expr).map(TypeInfo::adjusted)
461 FlowKind::Try { .. } => {
462 stdx::never!("try does not have defined expr_ty");
471 fn parent(&self) -> Option<SyntaxNode> {
473 FunctionBody::Expr(expr) => expr.syntax().parent(),
474 FunctionBody::Span { parent, .. } => Some(parent.syntax().clone()),
478 fn from_expr(expr: ast::Expr) -> Option<Self> {
480 ast::Expr::BreakExpr(it) => it.expr().map(Self::Expr),
481 ast::Expr::ReturnExpr(it) => it.expr().map(Self::Expr),
482 ast::Expr::BlockExpr(it) if !it.is_standalone() => None,
483 expr => Some(Self::Expr(expr)),
487 fn from_range(parent: ast::StmtList, selected: TextRange) -> FunctionBody {
488 let full_body = parent.syntax().children_with_tokens();
489 for st in parent.syntax().children_with_tokens() {
490 println!("Statement: {:?}", &st);
493 let mut text_range = full_body
494 .map(|stmt| stmt.text_range())
495 .filter(|&stmt| selected.intersect(stmt).filter(|it| !it.is_empty()).is_some())
496 .reduce(|acc, stmt| acc.cover(stmt));
498 println!("from_range text_range first: {:?}", text_range);
499 if let Some(tail_range) = parent
501 .map(|it| it.syntax().text_range())
502 .filter(|&it| selected.intersect(it).is_some())
504 text_range = Some(match text_range {
505 Some(text_range) => text_range.cover(tail_range),
509 println!("from_range text_range second: {:?}", text_range);
511 Self::Span { parent, text_range: text_range.unwrap_or(selected) }
514 fn indent_level(&self) -> IndentLevel {
516 FunctionBody::Expr(expr) => IndentLevel::from_node(expr.syntax()),
517 FunctionBody::Span { parent, .. } => IndentLevel::from_node(parent.syntax()) + 1,
521 fn tail_expr(&self) -> Option<ast::Expr> {
523 FunctionBody::Expr(expr) => Some(expr.clone()),
524 FunctionBody::Span { parent, text_range } => {
525 let tail_expr = parent.tail_expr()?;
526 text_range.contains_range(tail_expr.syntax().text_range()).then(|| tail_expr)
531 fn walk_expr(&self, cb: &mut dyn FnMut(ast::Expr)) {
533 FunctionBody::Expr(expr) => walk_expr(expr, cb),
534 FunctionBody::Span { parent, text_range } => {
537 .filter(|stmt| text_range.contains_range(stmt.syntax().text_range()))
538 .filter_map(|stmt| match stmt {
539 ast::Stmt::ExprStmt(expr_stmt) => expr_stmt.expr(),
540 ast::Stmt::Item(_) => None,
541 ast::Stmt::LetStmt(stmt) => stmt.initializer(),
543 .for_each(|expr| walk_expr(&expr, cb));
544 if let Some(expr) = parent
546 .filter(|it| text_range.contains_range(it.syntax().text_range()))
548 walk_expr(&expr, cb);
554 fn preorder_expr(&self, cb: &mut dyn FnMut(WalkEvent<ast::Expr>) -> bool) {
556 FunctionBody::Expr(expr) => preorder_expr(expr, cb),
557 FunctionBody::Span { parent, text_range } => {
560 .filter(|stmt| text_range.contains_range(stmt.syntax().text_range()))
561 .filter_map(|stmt| match stmt {
562 ast::Stmt::ExprStmt(expr_stmt) => expr_stmt.expr(),
563 ast::Stmt::Item(_) => None,
564 ast::Stmt::LetStmt(stmt) => stmt.initializer(),
566 .for_each(|expr| preorder_expr(&expr, cb));
567 if let Some(expr) = parent
569 .filter(|it| text_range.contains_range(it.syntax().text_range()))
571 preorder_expr(&expr, cb);
577 fn walk_pat(&self, cb: &mut dyn FnMut(ast::Pat)) {
579 FunctionBody::Expr(expr) => walk_patterns_in_expr(expr, cb),
580 FunctionBody::Span { parent, text_range } => {
583 .filter(|stmt| text_range.contains_range(stmt.syntax().text_range()))
584 .for_each(|stmt| match stmt {
585 ast::Stmt::ExprStmt(expr_stmt) => {
586 if let Some(expr) = expr_stmt.expr() {
587 walk_patterns_in_expr(&expr, cb)
590 ast::Stmt::Item(_) => (),
591 ast::Stmt::LetStmt(stmt) => {
592 if let Some(pat) = stmt.pat() {
595 if let Some(expr) = stmt.initializer() {
596 walk_patterns_in_expr(&expr, cb);
600 if let Some(expr) = parent
602 .filter(|it| text_range.contains_range(it.syntax().text_range()))
604 walk_patterns_in_expr(&expr, cb);
610 fn text_range(&self) -> TextRange {
612 FunctionBody::Expr(expr) => expr.syntax().text_range(),
613 &FunctionBody::Span { text_range, .. } => text_range,
617 fn contains_range(&self, range: TextRange) -> bool {
618 self.text_range().contains_range(range)
621 fn precedes_range(&self, range: TextRange) -> bool {
622 self.text_range().end() <= range.start()
625 fn contains_node(&self, node: &SyntaxNode) -> bool {
626 self.contains_range(node.text_range())
631 /// Analyzes a function body, returning the used local variables that are referenced in it as well as
632 /// whether it contains an await expression.
635 sema: &Semantics<RootDatabase>,
636 ) -> (FxIndexSet<Local>, Option<ast::SelfParam>) {
637 let mut self_param = None;
638 let mut res = FxIndexSet::default();
639 let mut cb = |name_ref: Option<_>| {
641 match name_ref.and_then(|name_ref| NameRefClass::classify(sema, &name_ref)) {
643 NameRefClass::Definition(Definition::Local(local_ref))
644 | NameRefClass::FieldShorthand { local_ref, field_ref: _ },
648 let InFile { file_id, value } = local_ref.source(sema.db);
649 // locals defined inside macros are not relevant to us
650 if !file_id.is_macro() {
652 Either::Right(it) => {
653 self_param.replace(it);
656 res.insert(local_ref);
661 self.walk_expr(&mut |expr| match expr {
662 ast::Expr::PathExpr(path_expr) => {
663 cb(path_expr.path().and_then(|it| it.as_single_name_ref()))
665 ast::Expr::MacroCall(call) => {
666 if let Some(tt) = call.token_tree() {
668 .children_with_tokens()
669 .flat_map(SyntaxElement::into_token)
670 .filter(|it| it.kind() == SyntaxKind::IDENT)
671 .flat_map(|t| sema.descend_into_macros(t))
672 .for_each(|t| cb(t.parent().and_then(ast::NameRef::cast)));
680 fn analyze_container(&self, sema: &Semantics<RootDatabase>) -> Option<ContainerInfo> {
681 let mut ancestors = self.parent()?.ancestors();
682 let infer_expr_opt = |expr| sema.type_of_expr(&expr?).map(TypeInfo::adjusted);
683 let mut parent_loop = None;
684 let mut set_parent_loop = |loop_: &dyn ast::HasLoopBody| {
687 .map_or(false, |it| it.syntax().text_range().contains_range(self.text_range()))
689 parent_loop.get_or_insert(loop_.syntax().clone());
692 let (is_const, expr, ty) = loop {
693 let anc = ancestors.next()?;
696 ast::ClosureExpr(closure) => (false, closure.body(), infer_expr_opt(closure.body())),
697 ast::BlockExpr(block_expr) => {
698 let (constness, block) = match block_expr.modifier() {
699 Some(ast::BlockModifier::Const(_)) => (true, block_expr),
700 Some(ast::BlockModifier::Try(_)) => (false, block_expr),
701 Some(ast::BlockModifier::Label(label)) if label.lifetime().is_some() => (false, block_expr),
704 let expr = Some(ast::Expr::BlockExpr(block));
705 (constness, expr.clone(), infer_expr_opt(expr))
708 (fn_.const_token().is_some(), fn_.body().map(ast::Expr::BlockExpr), Some(sema.to_def(&fn_)?.ret_type(sema.db)))
710 ast::Static(statik) => {
711 (true, statik.body(), Some(sema.to_def(&statik)?.ty(sema.db)))
713 ast::ConstArg(ca) => {
714 (true, ca.expr(), infer_expr_opt(ca.expr()))
716 ast::Const(konst) => {
717 (true, konst.body(), Some(sema.to_def(&konst)?.ty(sema.db)))
719 ast::ConstParam(cp) => {
720 (true, cp.default_val(), Some(sema.to_def(&cp)?.ty(sema.db)))
722 ast::ConstBlockPat(cbp) => {
723 let expr = cbp.block_expr().map(ast::Expr::BlockExpr);
724 (true, expr.clone(), infer_expr_opt(expr))
726 ast::Variant(__) => return None,
727 ast::Meta(__) => return None,
728 ast::LoopExpr(it) => {
729 set_parent_loop(&it);
732 ast::ForExpr(it) => {
733 set_parent_loop(&it);
736 ast::WhileExpr(it) => {
737 set_parent_loop(&it);
744 let container_tail = match expr? {
745 ast::Expr::BlockExpr(block) => block.tail_expr(),
749 container_tail.zip(self.tail_expr()).map_or(false, |(container_tail, body_tail)| {
750 container_tail.syntax().text_range().contains_range(body_tail.syntax().text_range())
752 Some(ContainerInfo { is_in_tail, is_const, parent_loop, ret_type: ty })
755 fn return_ty(&self, ctx: &AssistContext) -> Option<RetType> {
756 match self.tail_expr() {
757 Some(expr) => ctx.sema.type_of_expr(&expr).map(TypeInfo::original).map(RetType::Expr),
758 None => Some(RetType::Stmt),
762 /// Local variables defined inside `body` that are accessed outside of it
765 ctx: &'a AssistContext,
767 ) -> impl Iterator<Item = OutlivedLocal> + 'a {
768 let parent = parent.clone();
769 let range = self.text_range();
770 locals_defined_in_body(&ctx.sema, self)
772 .filter_map(move |local| local_outlives_body(ctx, range, local, &parent))
775 /// Analyses the function body for external control flow.
776 fn external_control_flow(
779 container_info: &ContainerInfo,
780 ) -> Option<ControlFlow> {
781 let mut ret_expr = None;
782 let mut try_expr = None;
783 let mut break_expr = None;
784 let mut continue_expr = None;
785 let mut is_async = false;
786 let mut _is_unsafe = false;
788 let mut unsafe_depth = 0;
789 let mut loop_depth = 0;
791 self.preorder_expr(&mut |expr| {
792 let expr = match expr {
793 WalkEvent::Enter(e) => e,
794 WalkEvent::Leave(expr) => {
796 ast::Expr::LoopExpr(_)
797 | ast::Expr::ForExpr(_)
798 | ast::Expr::WhileExpr(_) => loop_depth -= 1,
799 ast::Expr::BlockExpr(block_expr) if block_expr.unsafe_token().is_some() => {
808 ast::Expr::LoopExpr(_) | ast::Expr::ForExpr(_) | ast::Expr::WhileExpr(_) => {
811 ast::Expr::BlockExpr(block_expr) if block_expr.unsafe_token().is_some() => {
814 ast::Expr::ReturnExpr(it) => {
817 ast::Expr::TryExpr(it) => {
820 ast::Expr::BreakExpr(it) if loop_depth == 0 => {
821 break_expr = Some(it);
823 ast::Expr::ContinueExpr(it) if loop_depth == 0 => {
824 continue_expr = Some(it);
826 ast::Expr::AwaitExpr(_) => is_async = true,
827 // FIXME: Do unsafe analysis on expression, sem highlighting knows this so we should be able
828 // to just lift that out of there
829 // expr if unsafe_depth ==0 && expr.is_unsafe => is_unsafe = true,
835 let kind = match (try_expr, ret_expr, break_expr, continue_expr) {
836 (Some(_), _, None, None) => {
837 let ret_ty = container_info.ret_type.clone()?;
838 let kind = TryKind::of_ty(ret_ty, ctx)?;
840 Some(FlowKind::Try { kind })
842 (Some(_), _, _, _) => {
843 cov_mark::hit!(external_control_flow_try_and_bc);
846 (None, Some(r), None, None) => Some(FlowKind::Return(r.expr())),
847 (None, Some(_), _, _) => {
848 cov_mark::hit!(external_control_flow_return_and_bc);
851 (None, None, Some(_), Some(_)) => {
852 cov_mark::hit!(external_control_flow_break_and_continue);
855 (None, None, Some(b), None) => Some(FlowKind::Break(b.expr())),
856 (None, None, None, Some(_)) => Some(FlowKind::Continue),
857 (None, None, None, None) => None,
860 Some(ControlFlow { kind, is_async, is_unsafe: _is_unsafe })
863 /// find variables that should be extracted as params
865 /// Computes additional info that affects param type and mutability
866 fn extracted_function_params(
869 container_info: &ContainerInfo,
870 locals: impl Iterator<Item = Local>,
873 .map(|local| (local, local.source(ctx.db())))
874 .filter(|(_, src)| is_defined_outside_of_body(ctx, self, src))
875 .filter_map(|(local, src)| match src.value {
876 Either::Left(src) => Some((local, src)),
877 Either::Right(_) => {
878 stdx::never!(false, "Local::is_self returned false, but source is SelfParam");
883 let usages = LocalUsages::find_local_usages(ctx, var);
884 let ty = var.ty(ctx.db());
886 let defined_outside_parent_loop = container_info
889 .map_or(true, |it| it.text_range().contains_range(src.syntax().text_range()));
891 let is_copy = ty.is_copy(ctx.db());
892 let has_usages = self.has_usages_after_body(&usages);
894 !ty.is_mutable_reference() && has_exclusive_usages(ctx, &usages, self);
895 // We can move the value into the function call if it's not used after the call,
896 // if the var is not used but defined outside a loop we are extracting from we can't move it either
897 // as the function will reuse it in the next iteration.
898 let move_local = (!has_usages && defined_outside_parent_loop) || ty.is_reference();
899 Param { var, ty, move_local, requires_mut, is_copy }
904 fn has_usages_after_body(&self, usages: &LocalUsages) -> bool {
905 usages.iter().any(|reference| self.precedes_range(reference.range))
909 /// checks if relevant var is used with `&mut` access inside body
910 fn has_exclusive_usages(ctx: &AssistContext, usages: &LocalUsages, body: &FunctionBody) -> bool {
913 .filter(|reference| body.contains_range(reference.range))
914 .any(|reference| reference_is_exclusive(reference, body, ctx))
917 /// checks if this reference requires `&mut` access inside node
918 fn reference_is_exclusive(
919 reference: &FileReference,
920 node: &dyn HasTokenAtOffset,
923 // we directly modify variable with set: `n = 0`, `n += 1`
924 if reference.category == Some(ReferenceCategory::Write) {
928 // we take `&mut` reference to variable: `&mut v`
929 let path = match path_element_of_reference(node, reference) {
931 None => return false,
934 expr_require_exclusive_access(ctx, &path).unwrap_or(false)
937 /// checks if this expr requires `&mut` access, recurses on field access
938 fn expr_require_exclusive_access(ctx: &AssistContext, expr: &ast::Expr) -> Option<bool> {
939 if let ast::Expr::MacroCall(_) = expr {
940 // FIXME: expand macro and check output for mutable usages of the variable?
944 let parent = expr.syntax().parent()?;
946 if let Some(bin_expr) = ast::BinExpr::cast(parent.clone()) {
947 if matches!(bin_expr.op_kind()?, ast::BinaryOp::Assignment { .. }) {
948 return Some(bin_expr.lhs()?.syntax() == expr.syntax());
953 if let Some(ref_expr) = ast::RefExpr::cast(parent.clone()) {
954 return Some(ref_expr.mut_token().is_some());
957 if let Some(method_call) = ast::MethodCallExpr::cast(parent.clone()) {
958 let func = ctx.sema.resolve_method_call(&method_call)?;
959 let self_param = func.self_param(ctx.db())?;
960 let access = self_param.access(ctx.db());
962 return Some(matches!(access, hir::Access::Exclusive));
965 if let Some(field) = ast::FieldExpr::cast(parent) {
966 return expr_require_exclusive_access(ctx, &field.into());
972 trait HasTokenAtOffset {
973 fn token_at_offset(&self, offset: TextSize) -> TokenAtOffset<SyntaxToken>;
976 impl HasTokenAtOffset for SyntaxNode {
977 fn token_at_offset(&self, offset: TextSize) -> TokenAtOffset<SyntaxToken> {
978 SyntaxNode::token_at_offset(self, offset)
982 impl HasTokenAtOffset for FunctionBody {
983 fn token_at_offset(&self, offset: TextSize) -> TokenAtOffset<SyntaxToken> {
985 FunctionBody::Expr(expr) => expr.syntax().token_at_offset(offset),
986 FunctionBody::Span { parent, text_range } => {
987 match parent.syntax().token_at_offset(offset) {
988 TokenAtOffset::None => TokenAtOffset::None,
989 TokenAtOffset::Single(t) => {
990 if text_range.contains_range(t.text_range()) {
991 TokenAtOffset::Single(t)
996 TokenAtOffset::Between(a, b) => {
998 text_range.contains_range(a.text_range()),
999 text_range.contains_range(b.text_range()),
1001 (true, true) => TokenAtOffset::Between(a, b),
1002 (true, false) => TokenAtOffset::Single(a),
1003 (false, true) => TokenAtOffset::Single(b),
1004 (false, false) => TokenAtOffset::None,
1013 /// find relevant `ast::Expr` for reference
1017 /// `node` must cover `reference`, that is `node.text_range().contains_range(reference.range)`
1018 fn path_element_of_reference(
1019 node: &dyn HasTokenAtOffset,
1020 reference: &FileReference,
1021 ) -> Option<ast::Expr> {
1022 let token = node.token_at_offset(reference.range.start()).right_biased().or_else(|| {
1023 stdx::never!(false, "cannot find token at variable usage: {:?}", reference);
1026 let path = token.ancestors().find_map(ast::Expr::cast).or_else(|| {
1027 stdx::never!(false, "cannot find path parent of variable usage: {:?}", token);
1031 matches!(path, ast::Expr::PathExpr(_) | ast::Expr::MacroCall(_)),
1032 "unexpected expression type for variable usage: {:?}",
1038 /// list local variables defined inside `body`
1039 fn locals_defined_in_body(
1040 sema: &Semantics<RootDatabase>,
1041 body: &FunctionBody,
1042 ) -> FxIndexSet<Local> {
1043 // FIXME: this doesn't work well with macros
1044 // see https://github.com/rust-analyzer/rust-analyzer/pull/7535#discussion_r570048550
1045 let mut res = FxIndexSet::default();
1046 body.walk_pat(&mut |pat| {
1047 if let ast::Pat::IdentPat(pat) = pat {
1048 if let Some(local) = sema.to_def(&pat) {
1056 /// Returns usage details if local variable is used after(outside of) body
1057 fn local_outlives_body(
1058 ctx: &AssistContext,
1059 body_range: TextRange,
1061 parent: &SyntaxNode,
1062 ) -> Option<OutlivedLocal> {
1063 let usages = LocalUsages::find_local_usages(ctx, local);
1064 let mut has_mut_usages = false;
1065 let mut any_outlives = false;
1066 for usage in usages.iter() {
1067 if body_range.end() <= usage.range.start() {
1068 has_mut_usages |= reference_is_exclusive(usage, parent, ctx);
1069 any_outlives |= true;
1071 break; // no need to check more elements we have all the info we wanted
1078 Some(OutlivedLocal { local, mut_usage_outside_body: has_mut_usages })
1081 /// checks if the relevant local was defined before(outside of) body
1082 fn is_defined_outside_of_body(
1083 ctx: &AssistContext,
1084 body: &FunctionBody,
1085 src: &hir::InFile<Either<ast::IdentPat, ast::SelfParam>>,
1087 src.file_id.original_file(ctx.db()) == ctx.file_id()
1088 && !body.contains_node(either_syntax(&src.value))
1091 fn either_syntax(value: &Either<ast::IdentPat, ast::SelfParam>) -> &SyntaxNode {
1093 Either::Left(pat) => pat.syntax(),
1094 Either::Right(it) => it.syntax(),
1098 /// find where to put extracted function definition
1100 /// Function should be put right after returned node
1101 fn node_to_insert_after(body: &FunctionBody, anchor: Anchor) -> Option<SyntaxNode> {
1102 let node = match body {
1103 FunctionBody::Expr(e) => e.syntax(),
1104 FunctionBody::Span { parent, .. } => parent.syntax(),
1106 let mut ancestors = node.ancestors().peekable();
1107 let mut last_ancestor = None;
1108 while let Some(next_ancestor) = ancestors.next() {
1109 match next_ancestor.kind() {
1110 SyntaxKind::SOURCE_FILE => break,
1111 SyntaxKind::ITEM_LIST if !matches!(anchor, Anchor::Freestanding) => continue,
1112 SyntaxKind::ITEM_LIST => {
1113 if ancestors.peek().map(SyntaxNode::kind) == Some(SyntaxKind::MODULE) {
1117 SyntaxKind::ASSOC_ITEM_LIST if !matches!(anchor, Anchor::Method) => {
1120 SyntaxKind::ASSOC_ITEM_LIST => {
1121 if ancestors.peek().map(SyntaxNode::kind) == Some(SyntaxKind::IMPL) {
1127 last_ancestor = Some(next_ancestor);
1132 fn make_call(ctx: &AssistContext, fun: &Function, indent: IndentLevel) -> String {
1133 let ret_ty = fun.return_type(ctx);
1135 let args = make::arg_list(fun.params.iter().map(|param| param.to_arg(ctx)));
1136 let name = fun.name.clone();
1137 let mut call_expr = if fun.self_param.is_some() {
1138 let self_arg = make::expr_path(make::ext::ident_path("self"));
1139 make::expr_method_call(self_arg, name, args)
1141 let func = make::expr_path(make::path_unqualified(make::path_segment(name)));
1142 make::expr_call(func, args)
1145 let handler = FlowHandler::from_ret_ty(fun, &ret_ty);
1147 if fun.control_flow.is_async {
1148 call_expr = make::expr_await(call_expr);
1150 let expr = handler.make_call_expr(call_expr).indent(indent);
1152 let mut_modifier = |var: &OutlivedLocal| if var.mut_usage_outside_body { "mut " } else { "" };
1154 let mut buf = String::new();
1155 match fun.outliving_locals.as_slice() {
1158 format_to!(buf, "let {}{} = ", mut_modifier(var), var.local.name(ctx.db()).unwrap())
1161 buf.push_str("let (");
1162 let bindings = vars.iter().format_with(", ", |local, f| {
1163 f(&format_args!("{}{}", mut_modifier(local), local.local.name(ctx.db()).unwrap()))
1165 format_to!(buf, "{}", bindings);
1166 buf.push_str(") = ");
1170 format_to!(buf, "{}", expr);
1171 let insert_comma = fun
1174 .and_then(ast::MatchArm::cast)
1175 .map_or(false, |it| it.comma_token().is_none());
1178 } else if fun.ret_ty.is_unit() && (!fun.outliving_locals.is_empty() || !expr.is_block_like()) {
1186 Try { kind: TryKind },
1187 If { action: FlowKind },
1188 IfOption { action: FlowKind },
1189 MatchOption { none: FlowKind },
1190 MatchResult { err: FlowKind },
1194 fn from_ret_ty(fun: &Function, ret_ty: &FunType) -> FlowHandler {
1195 match &fun.control_flow.kind {
1196 None => FlowHandler::None,
1197 Some(flow_kind) => {
1198 let action = flow_kind.clone();
1199 if *ret_ty == FunType::Unit {
1201 FlowKind::Return(None) | FlowKind::Break(None) | FlowKind::Continue => {
1202 FlowHandler::If { action }
1204 FlowKind::Return(_) | FlowKind::Break(_) => {
1205 FlowHandler::IfOption { action }
1207 FlowKind::Try { kind } => FlowHandler::Try { kind: kind.clone() },
1211 FlowKind::Return(None) | FlowKind::Break(None) | FlowKind::Continue => {
1212 FlowHandler::MatchOption { none: action }
1214 FlowKind::Return(_) | FlowKind::Break(_) => {
1215 FlowHandler::MatchResult { err: action }
1217 FlowKind::Try { kind } => FlowHandler::Try { kind: kind.clone() },
1224 fn make_call_expr(&self, call_expr: ast::Expr) -> ast::Expr {
1226 FlowHandler::None => call_expr,
1227 FlowHandler::Try { kind: _ } => make::expr_try(call_expr),
1228 FlowHandler::If { action } => {
1229 let action = action.make_result_handler(None);
1230 let stmt = make::expr_stmt(action);
1231 let block = make::block_expr(iter::once(stmt.into()), None);
1232 let controlflow_break_path = make::path_from_text("ControlFlow::Break");
1233 let condition = make::condition(
1236 make::tuple_struct_pat(
1237 controlflow_break_path,
1238 iter::once(make::wildcard_pat().into()),
1243 make::expr_if(condition, block, None)
1245 FlowHandler::IfOption { action } => {
1246 let path = make::ext::ident_path("Some");
1247 let value_pat = make::ext::simple_ident_pat(make::name("value"));
1248 let pattern = make::tuple_struct_pat(path, iter::once(value_pat.into()));
1249 let cond = make::condition(call_expr, Some(pattern.into()));
1250 let value = make::expr_path(make::ext::ident_path("value"));
1251 let action_expr = action.make_result_handler(Some(value));
1252 let action_stmt = make::expr_stmt(action_expr);
1253 let then = make::block_expr(iter::once(action_stmt.into()), None);
1254 make::expr_if(cond, then, None)
1256 FlowHandler::MatchOption { none } => {
1257 let some_name = "value";
1260 let path = make::ext::ident_path("Some");
1261 let value_pat = make::ext::simple_ident_pat(make::name(some_name));
1262 let pat = make::tuple_struct_pat(path, iter::once(value_pat.into()));
1263 let value = make::expr_path(make::ext::ident_path(some_name));
1264 make::match_arm(iter::once(pat.into()), None, value)
1267 let path = make::ext::ident_path("None");
1268 let pat = make::path_pat(path);
1269 make::match_arm(iter::once(pat), None, none.make_result_handler(None))
1271 let arms = make::match_arm_list(vec![some_arm, none_arm]);
1272 make::expr_match(call_expr, arms)
1274 FlowHandler::MatchResult { err } => {
1275 let ok_name = "value";
1276 let err_name = "value";
1279 let path = make::ext::ident_path("Ok");
1280 let value_pat = make::ext::simple_ident_pat(make::name(ok_name));
1281 let pat = make::tuple_struct_pat(path, iter::once(value_pat.into()));
1282 let value = make::expr_path(make::ext::ident_path(ok_name));
1283 make::match_arm(iter::once(pat.into()), None, value)
1286 let path = make::ext::ident_path("Err");
1287 let value_pat = make::ext::simple_ident_pat(make::name(err_name));
1288 let pat = make::tuple_struct_pat(path, iter::once(value_pat.into()));
1289 let value = make::expr_path(make::ext::ident_path(err_name));
1291 iter::once(pat.into()),
1293 err.make_result_handler(Some(value)),
1296 let arms = make::match_arm_list(vec![ok_arm, err_arm]);
1297 make::expr_match(call_expr, arms)
1303 fn path_expr_from_local(ctx: &AssistContext, var: Local) -> ast::Expr {
1304 let name = var.name(ctx.db()).unwrap().to_string();
1305 make::expr_path(make::ext::ident_path(&name))
1309 ctx: &AssistContext,
1310 module: hir::Module,
1312 old_indent: IndentLevel,
1313 new_indent: IndentLevel,
1315 let mut fn_def = String::new();
1316 let params = fun.make_param_list(ctx, module);
1317 let ret_ty = fun.make_ret_ty(ctx, module);
1318 let body = make_body(ctx, old_indent, new_indent, fun);
1319 let const_kw = if fun.mods.is_const { "const " } else { "" };
1320 let async_kw = if fun.control_flow.is_async { "async " } else { "" };
1321 let unsafe_kw = if fun.control_flow.is_unsafe { "unsafe " } else { "" };
1322 match ctx.config.snippet_cap {
1323 Some(_) => format_to!(
1325 "\n\n{}{}{}{}fn $0{}{}",
1335 "\n\n{}{}{}{}fn {}{}",
1344 if let Some(ret_ty) = ret_ty {
1345 format_to!(fn_def, " {}", ret_ty);
1347 format_to!(fn_def, " {}", body);
1353 fn make_param_list(&self, ctx: &AssistContext, module: hir::Module) -> ast::ParamList {
1354 let self_param = self.self_param.clone();
1355 let params = self.params.iter().map(|param| param.to_param(ctx, module));
1356 make::param_list(self_param, params)
1359 fn make_ret_ty(&self, ctx: &AssistContext, module: hir::Module) -> Option<ast::RetType> {
1360 let fun_ty = self.return_type(ctx);
1361 let handler = if self.mods.is_in_tail {
1364 FlowHandler::from_ret_ty(self, &fun_ty)
1366 let ret_ty = match &handler {
1367 FlowHandler::None => {
1368 if matches!(fun_ty, FunType::Unit) {
1371 fun_ty.make_ty(ctx, module)
1373 FlowHandler::Try { kind: TryKind::Option } => {
1374 make::ext::ty_option(fun_ty.make_ty(ctx, module))
1376 FlowHandler::Try { kind: TryKind::Result { ty: parent_ret_ty } } => {
1377 let handler_ty = parent_ret_ty
1380 .map(|ty| make_ty(&ty, ctx, module))
1381 .unwrap_or_else(make::ty_placeholder);
1382 make::ext::ty_result(fun_ty.make_ty(ctx, module), handler_ty)
1384 FlowHandler::If { .. } => make::ty("ControlFlow<()>"),
1385 FlowHandler::IfOption { action } => {
1386 let handler_ty = action
1388 .map(|ty| make_ty(&ty, ctx, module))
1389 .unwrap_or_else(make::ty_placeholder);
1390 make::ext::ty_option(handler_ty)
1392 FlowHandler::MatchOption { .. } => make::ext::ty_option(fun_ty.make_ty(ctx, module)),
1393 FlowHandler::MatchResult { err } => {
1394 let handler_ty = err
1396 .map(|ty| make_ty(&ty, ctx, module))
1397 .unwrap_or_else(make::ty_placeholder);
1398 make::ext::ty_result(fun_ty.make_ty(ctx, module), handler_ty)
1401 Some(make::ret_type(ret_ty))
1406 fn make_ty(&self, ctx: &AssistContext, module: hir::Module) -> ast::Type {
1408 FunType::Unit => make::ty_unit(),
1409 FunType::Single(ty) => make_ty(ty, ctx, module),
1410 FunType::Tuple(types) => match types.as_slice() {
1412 stdx::never!("tuple type with 0 elements");
1416 stdx::never!("tuple type with 1 element");
1417 make_ty(ty, ctx, module)
1420 let types = types.iter().map(|ty| make_ty(ty, ctx, module));
1421 make::ty_tuple(types)
1429 ctx: &AssistContext,
1430 old_indent: IndentLevel,
1431 new_indent: IndentLevel,
1433 ) -> ast::BlockExpr {
1434 let ret_ty = fun.return_type(ctx);
1435 let handler = if fun.mods.is_in_tail {
1438 FlowHandler::from_ret_ty(fun, &ret_ty)
1441 println!("making body: {:?}", fun.body);
1442 let block = match &fun.body {
1443 FunctionBody::Expr(expr) => {
1444 let expr = rewrite_body_segment(ctx, &fun.params, &handler, expr.syntax());
1445 let expr = ast::Expr::cast(expr).unwrap();
1447 ast::Expr::BlockExpr(block) => {
1448 // If the extracted expression is itself a block, there is no need to wrap it inside another block.
1449 let block = block.dedent(old_indent);
1450 // Recreate the block for formatting consistency with other extracted functions.
1451 make::block_expr(block.statements(), block.tail_expr())
1454 let expr = expr.dedent(old_indent).indent(IndentLevel(1));
1456 make::block_expr(Vec::new(), Some(expr))
1460 FunctionBody::Span { parent, text_range } => {
1461 let mut elements: Vec<_> = parent
1463 .children_with_tokens()
1464 .filter(|it| text_range.contains_range(it.text_range()))
1465 .map(|it| match it {
1466 syntax::NodeOrToken::Node(n) => {
1467 println!("Found node: {:?}", n);
1468 let node_rewritten = rewrite_body_segment(ctx, &fun.params, &handler, &n);
1470 syntax::NodeOrToken::Node(node_rewritten)
1472 syntax::NodeOrToken::Token(t) => {
1473 println!("Found token: {:?}", t);
1474 syntax::NodeOrToken::Token(t)
1479 let mut tail_expr = match &elements.last() {
1480 Some(element) => match element {
1481 syntax::NodeOrToken::Node(node) if ast::Expr::can_cast(node.kind()) => {
1482 ast::Expr::cast(node.clone())
1493 None => match fun.outliving_locals.as_slice() {
1496 tail_expr = Some(path_expr_from_local(ctx, var.local));
1499 let exprs = vars.iter().map(|var| path_expr_from_local(ctx, var.local));
1500 let expr = make::expr_tuple(exprs);
1501 tail_expr = Some(expr);
1506 let body_indent = IndentLevel(1);
1507 let elements: Vec<SyntaxElement> = elements
1509 .map(|node_or_token| match &node_or_token {
1510 syntax::NodeOrToken::Node(node) => match ast::Stmt::cast(node.clone()) {
1512 let indented = stmt.dedent(old_indent).indent(body_indent);
1513 let ast_node = indented.syntax().clone_subtree();
1514 syntax::NodeOrToken::Node(ast_node)
1516 None => node_or_token,
1520 .collect::<Vec<SyntaxElement>>();
1521 let tail_expr = tail_expr.map(|expr| expr.dedent(old_indent).indent(body_indent));
1523 for element in &elements {
1524 println!("element: {:?}", element);
1527 make::hacky_block_expr_with_comments(elements, tail_expr)
1531 let block = match &handler {
1532 FlowHandler::None => block,
1533 FlowHandler::Try { kind } => {
1534 let block = with_default_tail_expr(block, make::expr_unit());
1535 map_tail_expr(block, |tail_expr| {
1536 let constructor = match kind {
1537 TryKind::Option => "Some",
1538 TryKind::Result { .. } => "Ok",
1540 let func = make::expr_path(make::ext::ident_path(constructor));
1541 let args = make::arg_list(iter::once(tail_expr));
1542 make::expr_call(func, args)
1545 FlowHandler::If { .. } => {
1546 let controlflow_continue = make::expr_call(
1547 make::expr_path(make::path_from_text("ControlFlow::Continue")),
1548 make::arg_list(iter::once(make::expr_unit())),
1550 with_tail_expr(block, controlflow_continue.into())
1552 FlowHandler::IfOption { .. } => {
1553 let none = make::expr_path(make::ext::ident_path("None"));
1554 with_tail_expr(block, none)
1556 FlowHandler::MatchOption { .. } => map_tail_expr(block, |tail_expr| {
1557 let some = make::expr_path(make::ext::ident_path("Some"));
1558 let args = make::arg_list(iter::once(tail_expr));
1559 make::expr_call(some, args)
1561 FlowHandler::MatchResult { .. } => map_tail_expr(block, |tail_expr| {
1562 let ok = make::expr_path(make::ext::ident_path("Ok"));
1563 let args = make::arg_list(iter::once(tail_expr));
1564 make::expr_call(ok, args)
1568 block.indent(new_indent)
1571 fn map_tail_expr(block: ast::BlockExpr, f: impl FnOnce(ast::Expr) -> ast::Expr) -> ast::BlockExpr {
1572 let tail_expr = match block.tail_expr() {
1573 Some(tail_expr) => tail_expr,
1574 None => return block,
1576 make::block_expr(block.statements(), Some(f(tail_expr)))
1579 fn with_default_tail_expr(block: ast::BlockExpr, tail_expr: ast::Expr) -> ast::BlockExpr {
1580 match block.tail_expr() {
1582 None => make::block_expr(block.statements(), Some(tail_expr)),
1586 fn with_tail_expr(block: ast::BlockExpr, tail_expr: ast::Expr) -> ast::BlockExpr {
1587 let stmt_tail = block.tail_expr().map(|expr| make::expr_stmt(expr).into());
1588 let stmts = block.statements().chain(stmt_tail);
1589 make::block_expr(stmts, Some(tail_expr))
1592 fn format_type(ty: &hir::Type, ctx: &AssistContext, module: hir::Module) -> String {
1593 ty.display_source_code(ctx.db(), module.into()).ok().unwrap_or_else(|| "_".to_string())
1596 fn make_ty(ty: &hir::Type, ctx: &AssistContext, module: hir::Module) -> ast::Type {
1597 let ty_str = format_type(ty, ctx, module);
1601 fn rewrite_body_segment(
1602 ctx: &AssistContext,
1604 handler: &FlowHandler,
1605 syntax: &SyntaxNode,
1607 let syntax = fix_param_usages(ctx, params, syntax);
1608 update_external_control_flow(handler, &syntax);
1612 /// change all usages to account for added `&`/`&mut` for some params
1613 fn fix_param_usages(ctx: &AssistContext, params: &[Param], syntax: &SyntaxNode) -> SyntaxNode {
1614 let mut usages_for_param: Vec<(&Param, Vec<ast::Expr>)> = Vec::new();
1616 let tm = TreeMutator::new(syntax);
1618 for param in params {
1619 if !param.kind().is_ref() {
1623 let usages = LocalUsages::find_local_usages(ctx, param.var);
1626 .filter(|reference| syntax.text_range().contains_range(reference.range))
1627 .filter_map(|reference| path_element_of_reference(syntax, reference))
1628 .map(|expr| tm.make_mut(&expr));
1630 usages_for_param.push((param, usages.collect()));
1633 let res = tm.make_syntax_mut(syntax);
1635 for (param, usages) in usages_for_param {
1636 for usage in usages {
1637 match usage.syntax().ancestors().skip(1).find_map(ast::Expr::cast) {
1638 Some(ast::Expr::MethodCallExpr(_) | ast::Expr::FieldExpr(_)) => {
1641 Some(ast::Expr::RefExpr(node))
1642 if param.kind() == ParamKind::MutRef && node.mut_token().is_some() =>
1644 ted::replace(node.syntax(), node.expr().unwrap().syntax());
1646 Some(ast::Expr::RefExpr(node))
1647 if param.kind() == ParamKind::SharedRef && node.mut_token().is_none() =>
1649 ted::replace(node.syntax(), node.expr().unwrap().syntax());
1652 let p = &make::expr_prefix(T![*], usage.clone()).clone_for_update();
1653 ted::replace(usage.syntax(), p.syntax())
1662 fn update_external_control_flow(handler: &FlowHandler, syntax: &SyntaxNode) {
1663 let mut nested_loop = None;
1664 let mut nested_scope = None;
1665 for event in syntax.preorder() {
1667 WalkEvent::Enter(e) => match e.kind() {
1668 SyntaxKind::LOOP_EXPR | SyntaxKind::WHILE_EXPR | SyntaxKind::FOR_EXPR => {
1669 if nested_loop.is_none() {
1670 nested_loop = Some(e.clone());
1675 | SyntaxKind::STATIC
1677 | SyntaxKind::MODULE => {
1678 if nested_scope.is_none() {
1679 nested_scope = Some(e.clone());
1684 WalkEvent::Leave(e) => {
1685 if nested_scope.is_none() {
1686 if let Some(expr) = ast::Expr::cast(e.clone()) {
1688 ast::Expr::ReturnExpr(return_expr) if nested_scope.is_none() => {
1689 let expr = return_expr.expr();
1690 if let Some(replacement) = make_rewritten_flow(handler, expr) {
1691 ted::replace(return_expr.syntax(), replacement.syntax())
1694 ast::Expr::BreakExpr(break_expr) if nested_loop.is_none() => {
1695 let expr = break_expr.expr();
1696 if let Some(replacement) = make_rewritten_flow(handler, expr) {
1697 ted::replace(break_expr.syntax(), replacement.syntax())
1700 ast::Expr::ContinueExpr(continue_expr) if nested_loop.is_none() => {
1701 if let Some(replacement) = make_rewritten_flow(handler, None) {
1702 ted::replace(continue_expr.syntax(), replacement.syntax())
1712 if nested_loop.as_ref() == Some(&e) {
1715 if nested_scope.as_ref() == Some(&e) {
1716 nested_scope = None;
1723 fn make_rewritten_flow(handler: &FlowHandler, arg_expr: Option<ast::Expr>) -> Option<ast::Expr> {
1724 let value = match handler {
1725 FlowHandler::None | FlowHandler::Try { .. } => return None,
1726 FlowHandler::If { .. } => make::expr_call(
1727 make::expr_path(make::path_from_text("ControlFlow::Break")),
1728 make::arg_list(iter::once(make::expr_unit())),
1730 FlowHandler::IfOption { .. } => {
1731 let expr = arg_expr.unwrap_or_else(|| make::expr_tuple(Vec::new()));
1732 let args = make::arg_list(iter::once(expr));
1733 make::expr_call(make::expr_path(make::ext::ident_path("Some")), args)
1735 FlowHandler::MatchOption { .. } => make::expr_path(make::ext::ident_path("None")),
1736 FlowHandler::MatchResult { .. } => {
1737 let expr = arg_expr.unwrap_or_else(|| make::expr_tuple(Vec::new()));
1738 let args = make::arg_list(iter::once(expr));
1739 make::expr_call(make::expr_path(make::ext::ident_path("Err")), args)
1742 Some(make::expr_return(Some(value)).clone_for_update())
1747 use crate::tests::{check_assist, check_assist_not_applicable};
1752 fn no_args_from_binary_expr() {
1765 fn $0fun_name() -> i32 {
1773 fn no_args_from_binary_expr_in_module() {
1789 fn $0fun_name() -> i32 {
1798 fn no_args_from_binary_expr_indented() {
1811 fn $0fun_name() -> i32 {
1819 fn no_args_from_stmt_with_last_expr() {
1835 fn $0fun_name() -> i32 {
1844 fn no_args_from_stmt_unit() {
1892 fn no_args_if_else() {
1897 $0if true { 1 } else { 2 }$0
1905 fn $0fun_name() -> i32 {
1906 if true { 1 } else { 2 }
1913 fn no_args_if_let_else() {
1918 $0if let true = false { 1 } else { 2 }$0
1926 fn $0fun_name() -> i32 {
1927 if let true = false { 1 } else { 2 }
1934 fn no_args_match() {
1950 fn $0fun_name() -> i32 {
1961 fn no_args_while() {
1987 $0for v in &[0, 1] { }$0
1996 for v in &[0, 1] { }
2003 fn no_args_from_loop_unit() {
2018 fn $0fun_name() -> ! {
2028 fn no_args_from_loop_with_return() {
2044 fn $0fun_name() -> i32 {
2055 fn no_args_from_match() {
2060 let v: i32 = $0match Some(1) {
2068 let v: i32 = fun_name();
2071 fn $0fun_name() -> i32 {
2082 fn extract_partial_block_single_line() {
2088 let mut v = $0n * n;$0
2095 let mut v = fun_name(n);
2099 fn $0fun_name(n: i32) -> i32 {
2108 fn extract_partial_block() {
2115 let mut v = m $0* n;
2125 let (mut v, mut w) = fun_name(m, n);
2130 fn $0fun_name(m: i32, n: i32) -> (i32, i32) {
2140 fn argument_form_expr() {
2155 fn $0fun_name(n: u32) -> u32 {
2163 fn argument_used_twice_form_expr() {
2178 fn $0fun_name(n: u32) -> u32 {
2186 fn two_arguments_form_expr() {
2203 fn $0fun_name(n: u32, m: u32) -> u32 {
2211 fn argument_and_locals() {
2227 fn $0fun_name(n: u32) -> u32 {
2236 fn in_comment_is_not_applicable() {
2237 cov_mark::check!(extract_function_in_comment_is_not_applicable);
2238 check_assist_not_applicable(extract_function, r"fn main() { 1 + /* $0comment$0 */ 1; }");
2242 fn part_of_expr_stmt() {
2255 fn $0fun_name() -> i32 {
2263 fn function_expr() {
2284 fn extract_from_nested() {
2290 let tuple = match x {
2291 true => ($02 + 2$0, true)
2299 let tuple = match x {
2300 true => (fun_name(), true)
2305 fn $0fun_name() -> i32 {
2313 fn param_from_closure() {
2318 let lambda = |x: u32| $0x * 2$0;
2323 let lambda = |x: u32| fun_name(x);
2326 fn $0fun_name(x: u32) -> u32 {
2334 fn extract_return_stmt() {
2347 fn $0fun_name() -> u32 {
2355 fn does_not_add_extra_whitespace() {
2372 fn $0fun_name() -> u32 {
2397 fn $0fun_name() -> i32 {
2410 let v = $00f32 as u32$0;
2418 fn $0fun_name() -> u32 {
2426 fn return_not_applicable() {
2427 check_assist_not_applicable(extract_function, r"fn foo() { $0return$0; } ");
2431 fn method_to_freestanding() {
2438 fn foo(&self) -> i32 {
2447 fn foo(&self) -> i32 {
2452 fn $0fun_name() -> i32 {
2460 fn method_with_reference() {
2464 struct S { f: i32 };
2467 fn foo(&self) -> i32 {
2473 struct S { f: i32 };
2476 fn foo(&self) -> i32 {
2480 fn $0fun_name(&self) -> i32 {
2489 fn method_with_mut() {
2493 struct S { f: i32 };
2502 struct S { f: i32 };
2509 fn $0fun_name(&mut self) {
2518 fn variable_defined_inside_and_used_after_no_ret() {
2531 let k = fun_name(n);
2535 fn $0fun_name(n: i32) -> i32 {
2544 fn variable_defined_inside_and_used_after_mutably_no_ret() {
2550 $0let mut k = n * n;$0
2557 let mut k = fun_name(n);
2561 fn $0fun_name(n: i32) -> i32 {
2570 fn two_variables_defined_inside_and_used_after_no_ret() {
2584 let (k, m) = fun_name(n);
2588 fn $0fun_name(n: i32) -> (i32, i32) {
2598 fn multi_variables_defined_inside_and_used_after_mutably_no_ret() {
2604 $0let mut k = n * n;
2615 let (mut k, mut m, o) = fun_name(n);
2620 fn $0fun_name(n: i32) -> (i32, i32, i32) {
2632 fn nontrivial_patterns_define_variables() {
2636 struct Counter(i32);
2638 $0let Counter(n) = Counter(0);$0
2643 struct Counter(i32);
2649 fn $0fun_name() -> i32 {
2650 let Counter(n) = Counter(0);
2658 fn struct_with_two_fields_pattern_define_variables() {
2662 struct Counter { n: i32, m: i32 };
2664 $0let Counter { n, m: k } = Counter { n: 1, m: 2 };$0
2669 struct Counter { n: i32, m: i32 };
2671 let (n, k) = fun_name();
2675 fn $0fun_name() -> (i32, i32) {
2676 let Counter { n, m: k } = Counter { n: 1, m: 2 };
2684 fn mut_var_from_outer_scope() {
2701 fn $0fun_name(n: &mut i32) {
2709 fn mut_field_from_outer_scope() {
2715 let mut c = C { n: 0 };
2723 let mut c = C { n: 0 };
2728 fn $0fun_name(c: &mut C) {
2736 fn mut_nested_field_from_outer_scope() {
2743 let mut c = C { p: P { n: 0 } };
2744 let mut v = C { p: P { n: 0 } };
2745 let u = C { p: P { n: 0 } };
2747 let r = &mut v.p.n;$0
2748 let m = c.p.n + v.p.n + u.p.n;
2755 let mut c = C { p: P { n: 0 } };
2756 let mut v = C { p: P { n: 0 } };
2757 let u = C { p: P { n: 0 } };
2758 fun_name(&mut c, &u, &mut v);
2759 let m = c.p.n + v.p.n + u.p.n;
2762 fn $0fun_name(c: &mut C, u: &C, v: &mut C) {
2771 fn mut_param_many_usages_stmt() {
2777 fn succ(&self) -> Self;
2778 fn inc(&mut self) -> Self { let v = self.succ(); *self = v; v }
2781 fn succ(&self) -> Self { *self + 1 }
2800 fn succ(&self) -> Self;
2801 fn inc(&mut self) -> Self { let v = self.succ(); *self = v; v }
2804 fn succ(&self) -> Self { *self + 1 }
2812 fn $0fun_name(n: &mut i32) {
2828 fn mut_param_many_usages_expr() {
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 }
2859 fn succ(&self) -> Self;
2860 fn inc(&mut self) -> Self { let v = self.succ(); *self = v; v }
2863 fn succ(&self) -> Self { *self + 1 }
2871 fn $0fun_name(n: &mut i32) {
2887 fn mut_param_by_value() {
2902 fn $0fun_name(mut n: i32) {
2910 fn mut_param_because_of_mut_ref() {
2928 fn $0fun_name(n: &mut i32) {
2937 fn mut_param_by_value_because_of_mut_ref() {
2953 fn $0fun_name(mut n: i32) {
2962 fn mut_method_call() {
2970 fn inc(&mut self) { *self += 1 }
2982 fn inc(&mut self) { *self += 1 }
2989 fn $0fun_name(mut n: i32) {
2997 fn shared_method_call() {
3005 fn succ(&self) { *self + 1 }
3017 fn succ(&self) { *self + 1 }
3024 fn $0fun_name(n: i32) {
3032 fn mut_method_call_with_other_receiver() {
3037 fn inc(&mut self, n: i32);
3040 fn inc(&mut self, n: i32) { *self += n }
3050 fn inc(&mut self, n: i32);
3053 fn inc(&mut self, n: i32) { *self += n }
3060 fn $0fun_name(n: i32) {
3069 fn non_copy_without_usages_after() {
3073 struct Counter(i32);
3080 struct Counter(i32);
3086 fn $0fun_name(c: Counter) {
3094 fn non_copy_used_after() {
3098 struct Counter(i32);
3106 struct Counter(i32);
3113 fn $0fun_name(c: &Counter) {
3121 fn copy_used_after() {
3139 fn $0fun_name(n: i32) {
3147 fn copy_custom_used_after() {
3151 //- minicore: copy, derive
3152 #[derive(Clone, Copy)]
3153 struct Counter(i32);
3161 #[derive(Clone, Copy)]
3162 struct Counter(i32);
3169 fn $0fun_name(c: Counter) {
3177 fn indented_stmts() {
3208 fn indented_stmts_inside_mod() {
3247 //- minicore: option
3262 let k = match fun_name(n) {
3263 Some(value) => value,
3270 fn $0fun_name(n: i32) -> Option<i32> {
3281 fn return_to_parent() {
3285 //- minicore: copy, result
3297 let k = match fun_name(n) {
3299 Err(value) => return value,
3304 fn $0fun_name(n: i32) -> Result<i32, i64> {
3315 fn break_and_continue() {
3316 cov_mark::check!(external_control_flow_break_and_continue);
3317 check_assist_not_applicable(
3336 fn return_and_break() {
3337 cov_mark::check!(external_control_flow_return_and_bc);
3338 check_assist_not_applicable(
3357 fn break_loop_with_if() {
3373 use core::ops::ControlFlow;
3378 if let ControlFlow::Break(_) = fun_name(&mut n) {
3385 fn $0fun_name(n: &mut i32) -> ControlFlow<()> {
3387 return ControlFlow::Break(());
3389 ControlFlow::Continue(())
3396 fn break_loop_nested() {
3413 use core::ops::ControlFlow;
3418 if let ControlFlow::Break(_) = fun_name(n) {
3425 fn $0fun_name(n: i32) -> ControlFlow<()> {
3428 return ControlFlow::Break(());
3430 ControlFlow::Continue(())
3437 fn return_from_nested_loop() {
3457 let m = match fun_name() {
3458 Some(value) => value,
3465 fn $0fun_name() -> Option<i32> {
3478 fn break_from_nested_loop() {
3503 fn $0fun_name() -> i32 {
3516 fn break_from_nested_and_outer_loops() {
3539 let m = match fun_name() {
3540 Some(value) => value,
3547 fn $0fun_name() -> Option<i32> {
3563 fn return_from_nested_fn() {
3588 fn $0fun_name() -> i32 {
3601 fn break_with_value() {
3621 if let Some(value) = fun_name() {
3628 fn $0fun_name() -> Option<i32> {
3641 fn break_with_value_and_return() {
3661 let m = match fun_name() {
3663 Err(value) => break value,
3669 fn $0fun_name() -> Result<i32, i64> {
3686 //- minicore: option
3687 fn bar() -> Option<i32> { None }
3688 fn foo() -> Option<()> {
3697 fn bar() -> Option<i32> { None }
3698 fn foo() -> Option<()> {
3700 let m = fun_name()?;
3705 fn $0fun_name() -> Option<i32> {
3715 fn try_option_unit() {
3719 //- minicore: option
3720 fn foo() -> Option<()> {
3729 fn foo() -> Option<()> {
3736 fn $0fun_name() -> Option<()> {
3750 //- minicore: result
3751 fn foo() -> Result<(), i64> {
3760 fn foo() -> Result<(), i64> {
3762 let m = fun_name()?;
3767 fn $0fun_name() -> Result<i32, i64> {
3777 fn try_option_with_return() {
3781 //- minicore: option
3782 fn foo() -> Option<()> {
3794 fn foo() -> Option<()> {
3796 let m = fun_name()?;
3801 fn $0fun_name() -> Option<i32> {
3814 fn try_result_with_return() {
3818 //- minicore: result
3819 fn foo() -> Result<(), i64> {
3831 fn foo() -> Result<(), i64> {
3833 let m = fun_name()?;
3838 fn $0fun_name() -> Result<i32, i64> {
3851 fn try_and_break() {
3852 cov_mark::check!(external_control_flow_try_and_bc);
3853 check_assist_not_applicable(
3856 //- minicore: option
3857 fn foo() -> Option<()> {
3873 fn try_and_return_ok() {
3877 //- minicore: result
3878 fn foo() -> Result<(), i64> {
3890 fn foo() -> Result<(), i64> {
3892 let m = fun_name()?;
3897 fn $0fun_name() -> Result<i32, i64> {
3910 fn param_usage_in_macro() {
3915 ($val:expr) => { $val };
3920 $0let k = n * m!(n);$0
3926 ($val:expr) => { $val };
3931 let k = fun_name(n);
3935 fn $0fun_name(n: i32) -> i32 {
3944 fn extract_with_await() {
3949 $0some_function().await;$0
3952 async fn some_function() {
3961 async fn $0fun_name() {
3962 some_function().await;
3965 async fn some_function() {
3973 fn extract_with_await_and_result_not_producing_match_expr() {
3977 async fn foo() -> Result<(), ()> {
3983 async fn foo() -> Result<(), ()> {
3987 async fn $0fun_name() -> _ {
3996 fn extract_with_await_and_result_producing_match_expr() {
4000 async fn foo() -> i32 {
4003 let k = async { 1 }.await;
4013 async fn foo() -> i32 {
4016 let m = match fun_name().await {
4018 Err(value) => break value,
4024 async fn $0fun_name() -> Result<i32, i32> {
4025 let k = async { 1 }.await;
4037 fn extract_with_await_in_args() {
4042 $0function_call("a", some_function().await);$0
4045 async fn some_function() {
4054 async fn $0fun_name() {
4055 function_call("a", some_function().await);
4058 async fn some_function() {
4066 fn extract_does_not_extract_standalone_blocks() {
4067 check_assist_not_applicable(
4076 fn extract_adds_comma_for_match_arm() {
4095 fn $0fun_name() -> i32 {
4118 fn $0fun_name() -> i32 {
4126 fn extract_does_not_tear_comments_apart() {
4153 fn extract_does_not_wrap_res_in_res() {
4157 //- minicore: result
4158 fn foo() -> Result<(), i64> {
4159 $0Result::<i32, i64>::Ok(0)?;
4164 fn foo() -> Result<(), i64> {
4168 fn $0fun_name() -> Result<(), i64> {
4169 Result::<i32, i64>::Ok(0)?;
4177 fn extract_knows_const() {
4190 const fn $0fun_name() {
4207 const fn $0fun_name() {
4215 fn extract_does_not_move_outer_loop_vars() {
4234 fn $0fun_name(x: &mut i32) {
4257 fn $0fun_name(mut x: i32) {
4284 fn $0fun_name(x: &mut i32) {
4291 // regression test for #9822
4293 fn extract_mut_ref_param_has_no_mut_binding_in_loop() {
4299 fn foo(&mut self) {}
4313 fn foo(&mut self) {}
4324 fn $0fun_name(y: &mut Foo) {
4332 fn extract_with_macro_arg() {
4337 ($val:expr) => { $val };
4346 ($val:expr) => { $val };
4353 fn $0fun_name(bar: &str) {
4361 fn unresolveable_types_default_to_placeholder() {
4366 let a = __unresolved;
4372 let a = __unresolved;
4373 let _ = fun_name(a);
4376 fn $0fun_name(a: _) -> _ {
4384 fn reference_mutable_param_with_further_usages() {
4392 pub fn testfn(arg: &mut Foo) {
4394 // Simulating access after the extracted portion
4403 pub fn testfn(arg: &mut Foo) {
4405 // Simulating access after the extracted portion
4409 fn $0fun_name(arg: &mut Foo) {
4417 fn reference_mutable_param_without_further_usages() {
4425 pub fn testfn(arg: &mut Foo) {
4434 pub fn testfn(arg: &mut Foo) {
4438 fn $0fun_name(arg: &mut Foo) {
4446 fn extract_function_copies_comment_at_start() {
4471 fn extract_function_copies_comment_in_between() {
4500 fn extract_function_copies_comment_at_end() {
4525 fn extract_function_copies_comment_indented() {
4554 fn extract_function_does_not_preserve_whitespace() {
4580 fn extract_function_long_form_comment() {