Skip to content

Commit 05e8b92

Browse files
committed
fix: bug in extract_function: should not import ControlFlow in some cases
1 parent e402c49 commit 05e8b92

File tree

1 file changed

+27
-1
lines changed

1 file changed

+27
-1
lines changed

crates/ide-assists/src/handlers/extract_function.rs

+27-1
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,12 @@ pub(crate) fn extract_function(acc: &mut Assists, ctx: &AssistContext<'_>) -> Op
147147
_ => format_function(ctx, module, &fun, old_indent, new_indent),
148148
};
149149

150-
if fn_def.contains("ControlFlow") {
150+
// There are external control flows
151+
if fun
152+
.control_flow
153+
.kind
154+
.is_some_and(|kind| matches!(kind, FlowKind::Break(_, _) | FlowKind::Continue(_)))
155+
{
151156
let scope = match scope {
152157
ImportScope::File(it) => ImportScope::File(builder.make_mut(it)),
153158
ImportScope::Module(it) => ImportScope::Module(builder.make_mut(it)),
@@ -4968,6 +4973,27 @@ pub fn testfn(arg: &mut Foo) {
49684973
fn $0fun_name(arg: &mut Foo) {
49694974
arg.field = 8;
49704975
}
4976+
"#,
4977+
);
4978+
}
4979+
#[test]
4980+
fn does_not_import_control_flow() {
4981+
check_assist(
4982+
extract_function,
4983+
r#"
4984+
//- minicore: try
4985+
fn func() {
4986+
$0let cf = "I'm ControlFlow";$0
4987+
}
4988+
"#,
4989+
r#"
4990+
fn func() {
4991+
fun_name();
4992+
}
4993+
4994+
fn $0fun_name() {
4995+
let cf = "I'm ControlFlow";
4996+
}
49714997
"#,
49724998
);
49734999
}

0 commit comments

Comments
 (0)