Skip to content

Commit 49dd380

Browse files
committed
Auto merge of rust-lang#16009 - werifu:fix-extract-function, r=Veykril
fix: bug in extract_function.rs There is a little bug in extract_function: It appends `use path::to::ControlFlow;` if the function created contains string "ControlFlow". A case below (also in the test named `does_not_import_control_flow` which will fail in the original code) <img width="322" alt="image" src="https://github.com/rust-lang/rust-analyzer/assets/53432474/4b80bb58-0cfd-4d56-b64c-d9649eed336e"> <img width="391" alt="image" src="https://github.com/rust-lang/rust-analyzer/assets/53432474/3d7262f4-8a4c-44ea-822d-304b8b23fe28"> Now I have changed the condition determining whether adding import statement. Only when the new function body contains ControlFlow::Break or ControlFlow::Continue can the import statement be added. Last related PR: rust-lang/rust-analyzer#10309
2 parents 9975650 + 05e8b92 commit 49dd380

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)