Auto merge of #14540 - AmrDeveloper:disallow_extract_fun_single_brace, r=Veykril

Fix allow extracting function from single brace of block expression

Fix allow extracting function when selecting either `{` or `}`

Fix #14514
This commit is contained in:
bors 2023-04-11 10:10:25 +00:00
commit f9f443076a

View File

@ -70,6 +70,11 @@ pub(crate) fn extract_function(acc: &mut Assists, ctx: &AssistContext<'_>) -> Op
}
let node = ctx.covering_element();
if matches!(node.kind(), T!['{'] | T!['}'] | T!['('] | T![')'] | T!['['] | T![']']) {
cov_mark::hit!(extract_function_in_braces_is_not_applicable);
return None;
}
if node.kind() == COMMENT {
cov_mark::hit!(extract_function_in_comment_is_not_applicable);
return None;
@ -5800,4 +5805,40 @@ fn $0fun_name() -> ControlFlow<()> {
"#,
);
}
#[test]
fn in_left_curly_is_not_applicable() {
cov_mark::check!(extract_function_in_braces_is_not_applicable);
check_assist_not_applicable(extract_function, r"fn foo() { $0}$0");
}
#[test]
fn in_right_curly_is_not_applicable() {
cov_mark::check!(extract_function_in_braces_is_not_applicable);
check_assist_not_applicable(extract_function, r"fn foo() $0{$0 }");
}
#[test]
fn in_left_paren_is_not_applicable() {
cov_mark::check!(extract_function_in_braces_is_not_applicable);
check_assist_not_applicable(extract_function, r"fn foo( $0)$0 { }");
}
#[test]
fn in_right_paren_is_not_applicable() {
cov_mark::check!(extract_function_in_braces_is_not_applicable);
check_assist_not_applicable(extract_function, r"fn foo $0($0 ) { }");
}
#[test]
fn in_left_brack_is_not_applicable() {
cov_mark::check!(extract_function_in_braces_is_not_applicable);
check_assist_not_applicable(extract_function, r"fn foo(arr: &mut [i32$0]$0) {}");
}
#[test]
fn in_right_brack_is_not_applicable() {
cov_mark::check!(extract_function_in_braces_is_not_applicable);
check_assist_not_applicable(extract_function, r"fn foo(arr: &mut $0[$0i32]) {}");
}
}