1 use std::{hash::BuildHasherDefault, iter};
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},
17 use itertools::Itertools;
18 use rustc_hash::FxHasher;
23 edit::{AstNodeEdit, IndentLevel},
26 match_ast, ted, SyntaxElement,
27 SyntaxKind::{self, COMMENT},
28 SyntaxNode, SyntaxToken, TextRange, TextSize, TokenAtOffset, WalkEvent, T,
32 assist_context::{AssistContext, Assists, TreeMutator},
36 type FxIndexSet<T> = indexmap::IndexSet<T, BuildHasherDefault<FxHasher>>;
38 // Assist: extract_function
40 // Extracts selected statements into new function.
58 // fn $0fun_name(n: i32) {
63 pub(crate) fn extract_function(acc: &mut Assists, ctx: &AssistContext) -> Option<()> {
64 let range = ctx.selection_trimmed();
69 let node = ctx.covering_element();
70 if node.kind() == COMMENT {
71 cov_mark::hit!(extract_function_in_comment_is_not_applicable);
75 let node = match node {
76 syntax::NodeOrToken::Node(n) => n,
77 syntax::NodeOrToken::Token(t) => t.parent()?,
79 let body = extraction_target(&node, range)?;
80 let container_info = body.analyze_container(&ctx.sema)?;
82 let (locals_used, self_param) = body.analyze(&ctx.sema);
84 let anchor = if self_param.is_some() { Anchor::Method } else { Anchor::Freestanding };
85 let insert_after = node_to_insert_after(&body, anchor)?;
86 let module = ctx.sema.scope(&insert_after).module()?;
88 let ret_ty = body.return_ty(ctx)?;
89 let control_flow = body.external_control_flow(ctx, &container_info)?;
90 let ret_values = body.ret_values(ctx, node.parent().as_ref().unwrap_or(&node));
92 let target_range = body.text_range();
94 let scope = ImportScope::find_insert_use_container(&node, &ctx.sema)?;
97 AssistId("extract_function", crate::AssistKind::RefactorExtract),
98 "Extract into function",
101 let outliving_locals: Vec<_> = ret_values.collect();
102 if stdx::never!(!outliving_locals.is_empty() && !ret_ty.is_unit()) {
103 // We should not have variables that outlive body if we have expression block
108 body.extracted_function_params(ctx, &container_info, locals_used.iter().copied());
111 name: make::name_ref("fun_name"),
118 mods: container_info,
121 let new_indent = IndentLevel::from_node(&insert_after);
122 let old_indent = fun.body.indent_level();
124 builder.replace(target_range, make_call(ctx, &fun, old_indent));
126 let fn_def = format_function(ctx, module, &fun, old_indent, new_indent);
127 let insert_offset = insert_after.text_range().end();
129 if fn_def.contains("ControlFlow") {
130 let scope = match scope {
131 ImportScope::File(it) => ImportScope::File(builder.make_mut(it)),
132 ImportScope::Module(it) => ImportScope::Module(builder.make_mut(it)),
133 ImportScope::Block(it) => ImportScope::Block(builder.make_mut(it)),
136 let control_flow_enum =
137 FamousDefs(&ctx.sema, Some(module.krate())).core_ops_ControlFlow();
139 if let Some(control_flow_enum) = control_flow_enum {
140 let mod_path = module.find_use_path_prefixed(
142 ModuleDef::from(control_flow_enum),
143 ctx.config.insert_use.prefix_kind,
146 if let Some(mod_path) = mod_path {
147 insert_use(&scope, mod_path_to_ast(&mod_path), &ctx.config.insert_use);
152 match ctx.config.snippet_cap {
153 Some(cap) => builder.insert_snippet(cap, insert_offset, fn_def),
154 None => builder.insert(insert_offset, fn_def),
160 /// Try to guess what user wants to extract
162 /// We have basically have two cases:
163 /// * We want whole node, like `loop {}`, `2 + 2`, `{ let n = 1; }` exprs.
164 /// Then we can use `ast::Expr`
165 /// * We want a few statements for a block. E.g.
167 /// fn foo() -> i32 {
177 fn extraction_target(node: &SyntaxNode, selection_range: TextRange) -> Option<FunctionBody> {
178 if let Some(stmt) = ast::Stmt::cast(node.clone()) {
180 ast::Stmt::Item(_) => None,
181 ast::Stmt::ExprStmt(_) | ast::Stmt::LetStmt(_) => Some(FunctionBody::from_range(
182 node.parent().and_then(ast::StmtList::cast)?,
188 // Covering element returned the parent block of one or multiple statements that have been selected
189 if let Some(stmt_list) = ast::StmtList::cast(node.clone()) {
190 if let Some(block_expr) = stmt_list.syntax().parent().and_then(ast::BlockExpr::cast) {
191 if block_expr.syntax().text_range() == selection_range {
192 return FunctionBody::from_expr(block_expr.into());
196 // Extract the full statements.
197 return Some(FunctionBody::from_range(stmt_list, selection_range));
200 let expr = ast::Expr::cast(node.clone())?;
201 // A node got selected fully
202 if node.text_range() == selection_range {
203 return FunctionBody::from_expr(expr);
206 node.ancestors().find_map(ast::Expr::cast).and_then(FunctionBody::from_expr)
212 self_param: Option<ast::SelfParam>,
214 control_flow: ControlFlow,
217 outliving_locals: Vec<OutlivedLocal>,
230 #[derive(Debug, Clone, Copy, PartialEq, Eq)]
238 #[derive(Debug, Eq, PartialEq)]
242 Tuple(Vec<hir::Type>),
245 /// Where to put extracted function definition
248 /// Extract free function and put right after current top-level function
250 /// Extract method and put right after current function in the impl-block
254 // FIXME: ControlFlow and ContainerInfo both track some function modifiers, feels like these two should
255 // probably be merged somehow.
258 kind: Option<FlowKind>,
263 /// The thing whose expression we are extracting from. Can be a function, const, static, const arg, ...
264 #[derive(Clone, Debug)]
265 struct ContainerInfo {
268 parent_loop: Option<SyntaxNode>,
269 /// The function's return type, const's type etc.
270 ret_type: Option<hir::Type>,
273 /// Control flow that is exported from extracted function
285 #[derive(Debug, Clone)]
287 /// Return with value (`return $expr;`)
288 Return(Option<ast::Expr>),
292 /// Break with value (`break $expr;`)
293 Break(Option<ast::Expr>),
298 #[derive(Debug, Clone)]
301 Result { ty: hir::Type },
311 fn is_unit(&self) -> bool {
313 RetType::Expr(ty) => ty.is_unit(),
314 RetType::Stmt => true,
319 /// Semantically same as `ast::Expr`, but preserves identity when using only part of the Block
320 /// This is the future function body, the part that is being extracted.
324 Span { parent: ast::StmtList, text_range: TextRange },
328 struct OutlivedLocal {
330 mut_usage_outside_body: bool,
333 /// Container of local variable usages
335 /// Semanticall same as `UsageSearchResult`, but provides more convenient interface
336 struct LocalUsages(ide_db::search::UsageSearchResult);
339 fn find_local_usages(ctx: &AssistContext, var: Local) -> Self {
341 Definition::Local(var)
343 .in_scope(SearchScope::single_file(ctx.file_id()))
348 fn iter(&self) -> impl Iterator<Item = &FileReference> + '_ {
349 self.0.iter().flat_map(|(_, rs)| rs)
354 fn return_type(&self, ctx: &AssistContext) -> FunType {
356 RetType::Expr(ty) if ty.is_unit() => FunType::Unit,
357 RetType::Expr(ty) => FunType::Single(ty.clone()),
358 RetType::Stmt => match self.outliving_locals.as_slice() {
360 [var] => FunType::Single(var.local.ty(ctx.db())),
362 let types = vars.iter().map(|v| v.local.ty(ctx.db())).collect();
363 FunType::Tuple(types)
371 fn is_ref(&self) -> bool {
372 matches!(self, ParamKind::SharedRef | ParamKind::MutRef)
377 fn kind(&self) -> ParamKind {
378 match (self.move_local, self.requires_mut, self.is_copy) {
379 (false, true, _) => ParamKind::MutRef,
380 (false, false, false) => ParamKind::SharedRef,
381 (true, true, _) => ParamKind::MutValue,
382 (_, false, _) => ParamKind::Value,
386 fn to_arg(&self, ctx: &AssistContext) -> ast::Expr {
387 let var = path_expr_from_local(ctx, self.var);
389 ParamKind::Value | ParamKind::MutValue => var,
390 ParamKind::SharedRef => make::expr_ref(var, false),
391 ParamKind::MutRef => make::expr_ref(var, true),
395 fn to_param(&self, ctx: &AssistContext, module: hir::Module) -> ast::Param {
396 let var = self.var.name(ctx.db()).unwrap().to_string();
397 let var_name = make::name(&var);
398 let pat = match self.kind() {
399 ParamKind::MutValue => make::ident_pat(false, true, var_name),
400 ParamKind::Value | ParamKind::SharedRef | ParamKind::MutRef => {
401 make::ext::simple_ident_pat(var_name)
405 let ty = make_ty(&self.ty, ctx, module);
406 let ty = match self.kind() {
407 ParamKind::Value | ParamKind::MutValue => ty,
408 ParamKind::SharedRef => make::ty_ref(ty, false),
409 ParamKind::MutRef => make::ty_ref(ty, true),
412 make::param(pat.into(), ty)
417 fn of_ty(ty: hir::Type, ctx: &AssistContext) -> Option<TryKind> {
419 // We favour Result for `expr?`
420 return Some(TryKind::Result { ty });
422 let adt = ty.as_adt()?;
423 let name = adt.name(ctx.db());
424 // FIXME: use lang items to determine if it is std type or user defined
425 // E.g. if user happens to define type named `Option`, we would have false positive
426 match name.to_string().as_str() {
427 "Option" => Some(TryKind::Option),
428 "Result" => Some(TryKind::Result { ty }),
435 fn make_result_handler(&self, expr: Option<ast::Expr>) -> ast::Expr {
437 FlowKind::Return(_) => make::expr_return(expr),
438 FlowKind::Break(_) => make::expr_break(expr),
439 FlowKind::Try { .. } => {
440 stdx::never!("cannot have result handler with try");
441 expr.unwrap_or_else(|| make::expr_return(None))
443 FlowKind::Continue => {
444 stdx::always!(expr.is_none(), "continue with value is not possible");
445 make::expr_continue()
450 fn expr_ty(&self, ctx: &AssistContext) -> Option<hir::Type> {
452 FlowKind::Return(Some(expr)) | FlowKind::Break(Some(expr)) => {
453 ctx.sema.type_of_expr(expr).map(TypeInfo::adjusted)
455 FlowKind::Try { .. } => {
456 stdx::never!("try does not have defined expr_ty");
465 fn parent(&self) -> Option<SyntaxNode> {
467 FunctionBody::Expr(expr) => expr.syntax().parent(),
468 FunctionBody::Span { parent, .. } => Some(parent.syntax().clone()),
472 fn from_expr(expr: ast::Expr) -> Option<Self> {
474 ast::Expr::BreakExpr(it) => it.expr().map(Self::Expr),
475 ast::Expr::ReturnExpr(it) => it.expr().map(Self::Expr),
476 ast::Expr::BlockExpr(it) if !it.is_standalone() => None,
477 expr => Some(Self::Expr(expr)),
481 fn from_range(parent: ast::StmtList, selected: TextRange) -> FunctionBody {
482 let mut text_range = parent
484 .map(|stmt| stmt.syntax().text_range())
485 .filter(|&stmt| selected.intersect(stmt).filter(|it| !it.is_empty()).is_some())
486 .fold1(|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;
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)
1426 let block = match &fun.body {
1427 FunctionBody::Expr(expr) => {
1428 let expr = rewrite_body_segment(ctx, &fun.params, &handler, expr.syntax());
1429 let expr = ast::Expr::cast(expr).unwrap();
1431 ast::Expr::BlockExpr(block) => {
1432 // If the extracted expression is itself a block, there is no need to wrap it inside another block.
1433 let block = block.dedent(old_indent);
1434 // Recreate the block for formatting consistency with other extracted functions.
1435 make::block_expr(block.statements(), block.tail_expr())
1438 let expr = expr.dedent(old_indent).indent(IndentLevel(1));
1440 make::block_expr(Vec::new(), Some(expr))
1444 FunctionBody::Span { parent, text_range } => {
1445 let mut elements: Vec<_> = parent
1448 .filter(|it| text_range.contains_range(it.text_range()))
1449 .map(|it| rewrite_body_segment(ctx, &fun.params, &handler, &it))
1452 let mut tail_expr = match elements.pop() {
1453 Some(node) => ast::Expr::cast(node.clone()).or_else(|| {
1454 elements.push(node);
1460 if tail_expr.is_none() {
1461 match fun.outliving_locals.as_slice() {
1464 tail_expr = Some(path_expr_from_local(ctx, var.local));
1467 let exprs = vars.iter().map(|var| path_expr_from_local(ctx, var.local));
1468 let expr = make::expr_tuple(exprs);
1469 tail_expr = Some(expr);
1474 let elements = elements.into_iter().filter_map(|node| match ast::Stmt::cast(node) {
1475 Some(stmt) => Some(stmt),
1477 stdx::never!("block contains non-statement");
1482 let body_indent = IndentLevel(1);
1483 let elements = elements.map(|stmt| stmt.dedent(old_indent).indent(body_indent));
1484 let tail_expr = tail_expr.map(|expr| expr.dedent(old_indent).indent(body_indent));
1486 make::block_expr(elements, tail_expr)
1490 let block = match &handler {
1491 FlowHandler::None => block,
1492 FlowHandler::Try { kind } => {
1493 let block = with_default_tail_expr(block, make::expr_unit());
1494 map_tail_expr(block, |tail_expr| {
1495 let constructor = match kind {
1496 TryKind::Option => "Some",
1497 TryKind::Result { .. } => "Ok",
1499 let func = make::expr_path(make::ext::ident_path(constructor));
1500 let args = make::arg_list(iter::once(tail_expr));
1501 make::expr_call(func, args)
1504 FlowHandler::If { .. } => {
1505 let controlflow_continue = make::expr_call(
1506 make::expr_path(make::path_from_text("ControlFlow::Continue")),
1507 make::arg_list(iter::once(make::expr_unit())),
1509 with_tail_expr(block, controlflow_continue.into())
1511 FlowHandler::IfOption { .. } => {
1512 let none = make::expr_path(make::ext::ident_path("None"));
1513 with_tail_expr(block, none)
1515 FlowHandler::MatchOption { .. } => map_tail_expr(block, |tail_expr| {
1516 let some = make::expr_path(make::ext::ident_path("Some"));
1517 let args = make::arg_list(iter::once(tail_expr));
1518 make::expr_call(some, args)
1520 FlowHandler::MatchResult { .. } => map_tail_expr(block, |tail_expr| {
1521 let ok = make::expr_path(make::ext::ident_path("Ok"));
1522 let args = make::arg_list(iter::once(tail_expr));
1523 make::expr_call(ok, args)
1527 block.indent(new_indent)
1530 fn map_tail_expr(block: ast::BlockExpr, f: impl FnOnce(ast::Expr) -> ast::Expr) -> ast::BlockExpr {
1531 let tail_expr = match block.tail_expr() {
1532 Some(tail_expr) => tail_expr,
1533 None => return block,
1535 make::block_expr(block.statements(), Some(f(tail_expr)))
1538 fn with_default_tail_expr(block: ast::BlockExpr, tail_expr: ast::Expr) -> ast::BlockExpr {
1539 match block.tail_expr() {
1541 None => make::block_expr(block.statements(), Some(tail_expr)),
1545 fn with_tail_expr(block: ast::BlockExpr, tail_expr: ast::Expr) -> ast::BlockExpr {
1546 let stmt_tail = block.tail_expr().map(|expr| make::expr_stmt(expr).into());
1547 let stmts = block.statements().chain(stmt_tail);
1548 make::block_expr(stmts, Some(tail_expr))
1551 fn format_type(ty: &hir::Type, ctx: &AssistContext, module: hir::Module) -> String {
1552 ty.display_source_code(ctx.db(), module.into()).ok().unwrap_or_else(|| "_".to_string())
1555 fn make_ty(ty: &hir::Type, ctx: &AssistContext, module: hir::Module) -> ast::Type {
1556 let ty_str = format_type(ty, ctx, module);
1560 fn rewrite_body_segment(
1561 ctx: &AssistContext,
1563 handler: &FlowHandler,
1564 syntax: &SyntaxNode,
1566 let syntax = fix_param_usages(ctx, params, syntax);
1567 update_external_control_flow(handler, &syntax);
1571 /// change all usages to account for added `&`/`&mut` for some params
1572 fn fix_param_usages(ctx: &AssistContext, params: &[Param], syntax: &SyntaxNode) -> SyntaxNode {
1573 let mut usages_for_param: Vec<(&Param, Vec<ast::Expr>)> = Vec::new();
1575 let tm = TreeMutator::new(syntax);
1577 for param in params {
1578 if !param.kind().is_ref() {
1582 let usages = LocalUsages::find_local_usages(ctx, param.var);
1585 .filter(|reference| syntax.text_range().contains_range(reference.range))
1586 .filter_map(|reference| path_element_of_reference(syntax, reference))
1587 .map(|expr| tm.make_mut(&expr));
1589 usages_for_param.push((param, usages.collect()));
1592 let res = tm.make_syntax_mut(syntax);
1594 for (param, usages) in usages_for_param {
1595 for usage in usages {
1596 match usage.syntax().ancestors().skip(1).find_map(ast::Expr::cast) {
1597 Some(ast::Expr::MethodCallExpr(_) | ast::Expr::FieldExpr(_)) => {
1600 Some(ast::Expr::RefExpr(node))
1601 if param.kind() == ParamKind::MutRef && node.mut_token().is_some() =>
1603 ted::replace(node.syntax(), node.expr().unwrap().syntax());
1605 Some(ast::Expr::RefExpr(node))
1606 if param.kind() == ParamKind::SharedRef && node.mut_token().is_none() =>
1608 ted::replace(node.syntax(), node.expr().unwrap().syntax());
1611 let p = &make::expr_prefix(T![*], usage.clone()).clone_for_update();
1612 ted::replace(usage.syntax(), p.syntax())
1621 fn update_external_control_flow(handler: &FlowHandler, syntax: &SyntaxNode) {
1622 let mut nested_loop = None;
1623 let mut nested_scope = None;
1624 for event in syntax.preorder() {
1626 WalkEvent::Enter(e) => match e.kind() {
1627 SyntaxKind::LOOP_EXPR | SyntaxKind::WHILE_EXPR | SyntaxKind::FOR_EXPR => {
1628 if nested_loop.is_none() {
1629 nested_loop = Some(e.clone());
1634 | SyntaxKind::STATIC
1636 | SyntaxKind::MODULE => {
1637 if nested_scope.is_none() {
1638 nested_scope = Some(e.clone());
1643 WalkEvent::Leave(e) => {
1644 if nested_scope.is_none() {
1645 if let Some(expr) = ast::Expr::cast(e.clone()) {
1647 ast::Expr::ReturnExpr(return_expr) if nested_scope.is_none() => {
1648 let expr = return_expr.expr();
1649 if let Some(replacement) = make_rewritten_flow(handler, expr) {
1650 ted::replace(return_expr.syntax(), replacement.syntax())
1653 ast::Expr::BreakExpr(break_expr) if nested_loop.is_none() => {
1654 let expr = break_expr.expr();
1655 if let Some(replacement) = make_rewritten_flow(handler, expr) {
1656 ted::replace(break_expr.syntax(), replacement.syntax())
1659 ast::Expr::ContinueExpr(continue_expr) if nested_loop.is_none() => {
1660 if let Some(replacement) = make_rewritten_flow(handler, None) {
1661 ted::replace(continue_expr.syntax(), replacement.syntax())
1671 if nested_loop.as_ref() == Some(&e) {
1674 if nested_scope.as_ref() == Some(&e) {
1675 nested_scope = None;
1682 fn make_rewritten_flow(handler: &FlowHandler, arg_expr: Option<ast::Expr>) -> Option<ast::Expr> {
1683 let value = match handler {
1684 FlowHandler::None | FlowHandler::Try { .. } => return None,
1685 FlowHandler::If { .. } => make::expr_call(
1686 make::expr_path(make::path_from_text("ControlFlow::Break")),
1687 make::arg_list(iter::once(make::expr_unit())),
1689 FlowHandler::IfOption { .. } => {
1690 let expr = arg_expr.unwrap_or_else(|| make::expr_tuple(Vec::new()));
1691 let args = make::arg_list(iter::once(expr));
1692 make::expr_call(make::expr_path(make::ext::ident_path("Some")), args)
1694 FlowHandler::MatchOption { .. } => make::expr_path(make::ext::ident_path("None")),
1695 FlowHandler::MatchResult { .. } => {
1696 let expr = arg_expr.unwrap_or_else(|| make::expr_tuple(Vec::new()));
1697 let args = make::arg_list(iter::once(expr));
1698 make::expr_call(make::expr_path(make::ext::ident_path("Err")), args)
1701 Some(make::expr_return(Some(value)).clone_for_update())
1706 use crate::tests::{check_assist, check_assist_not_applicable};
1711 fn no_args_from_binary_expr() {
1724 fn $0fun_name() -> i32 {
1732 fn no_args_from_binary_expr_in_module() {
1748 fn $0fun_name() -> i32 {
1757 fn no_args_from_binary_expr_indented() {
1770 fn $0fun_name() -> i32 {
1778 fn no_args_from_stmt_with_last_expr() {
1794 fn $0fun_name() -> i32 {
1803 fn no_args_from_stmt_unit() {
1851 fn no_args_if_else() {
1856 $0if true { 1 } else { 2 }$0
1864 fn $0fun_name() -> i32 {
1865 if true { 1 } else { 2 }
1872 fn no_args_if_let_else() {
1877 $0if let true = false { 1 } else { 2 }$0
1885 fn $0fun_name() -> i32 {
1886 if let true = false { 1 } else { 2 }
1893 fn no_args_match() {
1909 fn $0fun_name() -> i32 {
1920 fn no_args_while() {
1946 $0for v in &[0, 1] { }$0
1955 for v in &[0, 1] { }
1962 fn no_args_from_loop_unit() {
1977 fn $0fun_name() -> ! {
1987 fn no_args_from_loop_with_return() {
2003 fn $0fun_name() -> i32 {
2014 fn no_args_from_match() {
2019 let v: i32 = $0match Some(1) {
2027 let v: i32 = fun_name();
2030 fn $0fun_name() -> i32 {
2041 fn extract_partial_block_single_line() {
2047 let mut v = $0n * n;$0
2054 let mut v = fun_name(n);
2058 fn $0fun_name(n: i32) -> i32 {
2067 fn extract_partial_block() {
2074 let mut v = m $0* n;
2084 let (mut v, mut w) = fun_name(m, n);
2089 fn $0fun_name(m: i32, n: i32) -> (i32, i32) {
2099 fn argument_form_expr() {
2114 fn $0fun_name(n: u32) -> u32 {
2122 fn argument_used_twice_form_expr() {
2137 fn $0fun_name(n: u32) -> u32 {
2145 fn two_arguments_form_expr() {
2162 fn $0fun_name(n: u32, m: u32) -> u32 {
2170 fn argument_and_locals() {
2186 fn $0fun_name(n: u32) -> u32 {
2195 fn in_comment_is_not_applicable() {
2196 cov_mark::check!(extract_function_in_comment_is_not_applicable);
2197 check_assist_not_applicable(extract_function, r"fn main() { 1 + /* $0comment$0 */ 1; }");
2201 fn part_of_expr_stmt() {
2214 fn $0fun_name() -> i32 {
2222 fn function_expr() {
2243 fn extract_from_nested() {
2249 let tuple = match x {
2250 true => ($02 + 2$0, true)
2258 let tuple = match x {
2259 true => (fun_name(), true)
2264 fn $0fun_name() -> i32 {
2272 fn param_from_closure() {
2277 let lambda = |x: u32| $0x * 2$0;
2282 let lambda = |x: u32| fun_name(x);
2285 fn $0fun_name(x: u32) -> u32 {
2293 fn extract_return_stmt() {
2306 fn $0fun_name() -> u32 {
2314 fn does_not_add_extra_whitespace() {
2331 fn $0fun_name() -> u32 {
2356 fn $0fun_name() -> i32 {
2369 let v = $00f32 as u32$0;
2377 fn $0fun_name() -> u32 {
2385 fn return_not_applicable() {
2386 check_assist_not_applicable(extract_function, r"fn foo() { $0return$0; } ");
2390 fn method_to_freestanding() {
2397 fn foo(&self) -> i32 {
2406 fn foo(&self) -> i32 {
2411 fn $0fun_name() -> i32 {
2419 fn method_with_reference() {
2423 struct S { f: i32 };
2426 fn foo(&self) -> i32 {
2432 struct S { f: i32 };
2435 fn foo(&self) -> i32 {
2439 fn $0fun_name(&self) -> i32 {
2448 fn method_with_mut() {
2452 struct S { f: i32 };
2461 struct S { f: i32 };
2468 fn $0fun_name(&mut self) {
2477 fn variable_defined_inside_and_used_after_no_ret() {
2490 let k = fun_name(n);
2494 fn $0fun_name(n: i32) -> i32 {
2503 fn variable_defined_inside_and_used_after_mutably_no_ret() {
2509 $0let mut k = n * n;$0
2516 let mut k = fun_name(n);
2520 fn $0fun_name(n: i32) -> i32 {
2529 fn two_variables_defined_inside_and_used_after_no_ret() {
2543 let (k, m) = fun_name(n);
2547 fn $0fun_name(n: i32) -> (i32, i32) {
2557 fn multi_variables_defined_inside_and_used_after_mutably_no_ret() {
2563 $0let mut k = n * n;
2574 let (mut k, mut m, o) = fun_name(n);
2579 fn $0fun_name(n: i32) -> (i32, i32, i32) {
2591 fn nontrivial_patterns_define_variables() {
2595 struct Counter(i32);
2597 $0let Counter(n) = Counter(0);$0
2602 struct Counter(i32);
2608 fn $0fun_name() -> i32 {
2609 let Counter(n) = Counter(0);
2617 fn struct_with_two_fields_pattern_define_variables() {
2621 struct Counter { n: i32, m: i32 };
2623 $0let Counter { n, m: k } = Counter { n: 1, m: 2 };$0
2628 struct Counter { n: i32, m: i32 };
2630 let (n, k) = fun_name();
2634 fn $0fun_name() -> (i32, i32) {
2635 let Counter { n, m: k } = Counter { n: 1, m: 2 };
2643 fn mut_var_from_outer_scope() {
2660 fn $0fun_name(n: &mut i32) {
2668 fn mut_field_from_outer_scope() {
2674 let mut c = C { n: 0 };
2682 let mut c = C { n: 0 };
2687 fn $0fun_name(c: &mut C) {
2695 fn mut_nested_field_from_outer_scope() {
2702 let mut c = C { p: P { n: 0 } };
2703 let mut v = C { p: P { n: 0 } };
2704 let u = C { p: P { n: 0 } };
2706 let r = &mut v.p.n;$0
2707 let m = c.p.n + v.p.n + u.p.n;
2714 let mut c = C { p: P { n: 0 } };
2715 let mut v = C { p: P { n: 0 } };
2716 let u = C { p: P { n: 0 } };
2717 fun_name(&mut c, &u, &mut v);
2718 let m = c.p.n + v.p.n + u.p.n;
2721 fn $0fun_name(c: &mut C, u: &C, v: &mut C) {
2730 fn mut_param_many_usages_stmt() {
2736 fn succ(&self) -> Self;
2737 fn inc(&mut self) -> Self { let v = self.succ(); *self = v; v }
2740 fn succ(&self) -> Self { *self + 1 }
2759 fn succ(&self) -> Self;
2760 fn inc(&mut self) -> Self { let v = self.succ(); *self = v; v }
2763 fn succ(&self) -> Self { *self + 1 }
2771 fn $0fun_name(n: &mut i32) {
2787 fn mut_param_many_usages_expr() {
2793 fn succ(&self) -> Self;
2794 fn inc(&mut self) -> Self { let v = self.succ(); *self = v; v }
2797 fn succ(&self) -> Self { *self + 1 }
2818 fn succ(&self) -> Self;
2819 fn inc(&mut self) -> Self { let v = self.succ(); *self = v; v }
2822 fn succ(&self) -> Self { *self + 1 }
2830 fn $0fun_name(n: &mut i32) {
2846 fn mut_param_by_value() {
2861 fn $0fun_name(mut n: i32) {
2869 fn mut_param_because_of_mut_ref() {
2887 fn $0fun_name(n: &mut i32) {
2896 fn mut_param_by_value_because_of_mut_ref() {
2912 fn $0fun_name(mut n: i32) {
2921 fn mut_method_call() {
2929 fn inc(&mut self) { *self += 1 }
2941 fn inc(&mut self) { *self += 1 }
2948 fn $0fun_name(mut n: i32) {
2956 fn shared_method_call() {
2964 fn succ(&self) { *self + 1 }
2976 fn succ(&self) { *self + 1 }
2983 fn $0fun_name(n: i32) {
2991 fn mut_method_call_with_other_receiver() {
2996 fn inc(&mut self, n: i32);
2999 fn inc(&mut self, n: i32) { *self += n }
3009 fn inc(&mut self, n: i32);
3012 fn inc(&mut self, n: i32) { *self += n }
3019 fn $0fun_name(n: i32) {
3028 fn non_copy_without_usages_after() {
3032 struct Counter(i32);
3039 struct Counter(i32);
3045 fn $0fun_name(c: Counter) {
3053 fn non_copy_used_after() {
3057 struct Counter(i32);
3065 struct Counter(i32);
3072 fn $0fun_name(c: &Counter) {
3080 fn copy_used_after() {
3098 fn $0fun_name(n: i32) {
3106 fn copy_custom_used_after() {
3110 //- minicore: copy, derive
3111 #[derive(Clone, Copy)]
3112 struct Counter(i32);
3120 #[derive(Clone, Copy)]
3121 struct Counter(i32);
3128 fn $0fun_name(c: Counter) {
3136 fn indented_stmts() {
3167 fn indented_stmts_inside_mod() {
3206 //- minicore: option
3221 let k = match fun_name(n) {
3222 Some(value) => value,
3229 fn $0fun_name(n: i32) -> Option<i32> {
3240 fn return_to_parent() {
3244 //- minicore: copy, result
3256 let k = match fun_name(n) {
3258 Err(value) => return value,
3263 fn $0fun_name(n: i32) -> Result<i32, i64> {
3274 fn break_and_continue() {
3275 cov_mark::check!(external_control_flow_break_and_continue);
3276 check_assist_not_applicable(
3295 fn return_and_break() {
3296 cov_mark::check!(external_control_flow_return_and_bc);
3297 check_assist_not_applicable(
3316 fn break_loop_with_if() {
3332 use core::ops::ControlFlow;
3337 if let ControlFlow::Break(_) = fun_name(&mut n) {
3344 fn $0fun_name(n: &mut i32) -> ControlFlow<()> {
3346 return ControlFlow::Break(());
3348 ControlFlow::Continue(())
3355 fn break_loop_nested() {
3372 use core::ops::ControlFlow;
3377 if let ControlFlow::Break(_) = fun_name(n) {
3384 fn $0fun_name(n: i32) -> ControlFlow<()> {
3387 return ControlFlow::Break(());
3389 ControlFlow::Continue(())
3396 fn return_from_nested_loop() {
3416 let m = match fun_name() {
3417 Some(value) => value,
3424 fn $0fun_name() -> Option<i32> {
3437 fn break_from_nested_loop() {
3462 fn $0fun_name() -> i32 {
3475 fn break_from_nested_and_outer_loops() {
3498 let m = match fun_name() {
3499 Some(value) => value,
3506 fn $0fun_name() -> Option<i32> {
3522 fn return_from_nested_fn() {
3547 fn $0fun_name() -> i32 {
3560 fn break_with_value() {
3580 if let Some(value) = fun_name() {
3587 fn $0fun_name() -> Option<i32> {
3600 fn break_with_value_and_return() {
3620 let m = match fun_name() {
3622 Err(value) => break value,
3628 fn $0fun_name() -> Result<i32, i64> {
3645 //- minicore: option
3646 fn bar() -> Option<i32> { None }
3647 fn foo() -> Option<()> {
3656 fn bar() -> Option<i32> { None }
3657 fn foo() -> Option<()> {
3659 let m = fun_name()?;
3664 fn $0fun_name() -> Option<i32> {
3674 fn try_option_unit() {
3678 //- minicore: option
3679 fn foo() -> Option<()> {
3688 fn foo() -> Option<()> {
3695 fn $0fun_name() -> Option<()> {
3709 //- minicore: result
3710 fn foo() -> Result<(), i64> {
3719 fn foo() -> Result<(), i64> {
3721 let m = fun_name()?;
3726 fn $0fun_name() -> Result<i32, i64> {
3736 fn try_option_with_return() {
3740 //- minicore: option
3741 fn foo() -> Option<()> {
3753 fn foo() -> Option<()> {
3755 let m = fun_name()?;
3760 fn $0fun_name() -> Option<i32> {
3773 fn try_result_with_return() {
3777 //- minicore: result
3778 fn foo() -> Result<(), i64> {
3790 fn foo() -> Result<(), i64> {
3792 let m = fun_name()?;
3797 fn $0fun_name() -> Result<i32, i64> {
3810 fn try_and_break() {
3811 cov_mark::check!(external_control_flow_try_and_bc);
3812 check_assist_not_applicable(
3815 //- minicore: option
3816 fn foo() -> Option<()> {
3832 fn try_and_return_ok() {
3836 //- minicore: result
3837 fn foo() -> Result<(), i64> {
3849 fn foo() -> Result<(), i64> {
3851 let m = fun_name()?;
3856 fn $0fun_name() -> Result<i32, i64> {
3869 fn param_usage_in_macro() {
3874 ($val:expr) => { $val };
3879 $0let k = n * m!(n);$0
3885 ($val:expr) => { $val };
3890 let k = fun_name(n);
3894 fn $0fun_name(n: i32) -> i32 {
3903 fn extract_with_await() {
3908 $0some_function().await;$0
3911 async fn some_function() {
3920 async fn $0fun_name() {
3921 some_function().await;
3924 async fn some_function() {
3932 fn extract_with_await_and_result_not_producing_match_expr() {
3936 async fn foo() -> Result<(), ()> {
3942 async fn foo() -> Result<(), ()> {
3946 async fn $0fun_name() -> _ {
3955 fn extract_with_await_and_result_producing_match_expr() {
3959 async fn foo() -> i32 {
3962 let k = async { 1 }.await;
3972 async fn foo() -> i32 {
3975 let m = match fun_name().await {
3977 Err(value) => break value,
3983 async fn $0fun_name() -> Result<i32, i32> {
3984 let k = async { 1 }.await;
3996 fn extract_with_await_in_args() {
4001 $0function_call("a", some_function().await);$0
4004 async fn some_function() {
4013 async fn $0fun_name() {
4014 function_call("a", some_function().await);
4017 async fn some_function() {
4025 fn extract_does_not_extract_standalone_blocks() {
4026 check_assist_not_applicable(
4035 fn extract_adds_comma_for_match_arm() {
4054 fn $0fun_name() -> i32 {
4077 fn $0fun_name() -> i32 {
4085 fn extract_does_not_tear_comments_apart() {
4112 fn extract_does_not_wrap_res_in_res() {
4116 //- minicore: result
4117 fn foo() -> Result<(), i64> {
4118 $0Result::<i32, i64>::Ok(0)?;
4123 fn foo() -> Result<(), i64> {
4127 fn $0fun_name() -> Result<(), i64> {
4128 Result::<i32, i64>::Ok(0)?;
4136 fn extract_knows_const() {
4149 const fn $0fun_name() {
4166 const fn $0fun_name() {
4174 fn extract_does_not_move_outer_loop_vars() {
4193 fn $0fun_name(x: &mut i32) {
4216 fn $0fun_name(mut x: i32) {
4243 fn $0fun_name(x: &mut i32) {
4250 // regression test for #9822
4252 fn extract_mut_ref_param_has_no_mut_binding_in_loop() {
4258 fn foo(&mut self) {}
4272 fn foo(&mut self) {}
4283 fn $0fun_name(y: &mut Foo) {
4291 fn extract_with_macro_arg() {
4296 ($val:expr) => { $val };
4305 ($val:expr) => { $val };
4312 fn $0fun_name(bar: &str) {
4320 fn unresolveable_types_default_to_placeholder() {
4325 let a = __unresolved;
4331 let a = __unresolved;
4332 let _ = fun_name(a);
4335 fn $0fun_name(a: _) -> _ {