mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-09-28 21:05:02 +00:00
Merge #9663
9663: fix: Don't offer extract_variable assist when there is no surrounding block r=Veykril a=Veykril Fixes #9143 bors r+ Co-authored-by: Lukas Wirth <lukastw97@gmail.com>
This commit is contained in:
commit
f2736c969c
1 changed files with 45 additions and 30 deletions
|
@ -35,7 +35,10 @@ pub(crate) fn extract_variable(acc: &mut Assists, ctx: &AssistContext) -> Option
|
||||||
cov_mark::hit!(extract_var_in_comment_is_not_applicable);
|
cov_mark::hit!(extract_var_in_comment_is_not_applicable);
|
||||||
return None;
|
return None;
|
||||||
}
|
}
|
||||||
let to_extract = node.ancestors().find_map(valid_target_expr)?;
|
let to_extract = node
|
||||||
|
.ancestors()
|
||||||
|
.take_while(|it| it.text_range().contains_range(ctx.frange.range))
|
||||||
|
.find_map(valid_target_expr)?;
|
||||||
if let Some(ty) = ctx.sema.type_of_expr(&to_extract) {
|
if let Some(ty) = ctx.sema.type_of_expr(&to_extract) {
|
||||||
if ty.is_unit() {
|
if ty.is_unit() {
|
||||||
return None;
|
return None;
|
||||||
|
@ -142,41 +145,43 @@ enum Anchor {
|
||||||
|
|
||||||
impl Anchor {
|
impl Anchor {
|
||||||
fn from(to_extract: &ast::Expr) -> Option<Anchor> {
|
fn from(to_extract: &ast::Expr) -> Option<Anchor> {
|
||||||
to_extract.syntax().ancestors().find_map(|node| {
|
to_extract.syntax().ancestors().take_while(|it| !ast::Item::can_cast(it.kind())).find_map(
|
||||||
if let Some(expr) =
|
|node| {
|
||||||
node.parent().and_then(ast::BlockExpr::cast).and_then(|it| it.tail_expr())
|
if let Some(expr) =
|
||||||
{
|
node.parent().and_then(ast::BlockExpr::cast).and_then(|it| it.tail_expr())
|
||||||
if expr.syntax() == &node {
|
{
|
||||||
cov_mark::hit!(test_extract_var_last_expr);
|
if expr.syntax() == &node {
|
||||||
return Some(Anchor::Before(node));
|
cov_mark::hit!(test_extract_var_last_expr);
|
||||||
|
return Some(Anchor::Before(node));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
if let Some(parent) = node.parent() {
|
if let Some(parent) = node.parent() {
|
||||||
if parent.kind() == CLOSURE_EXPR {
|
if parent.kind() == CLOSURE_EXPR {
|
||||||
cov_mark::hit!(test_extract_var_in_closure_no_block);
|
cov_mark::hit!(test_extract_var_in_closure_no_block);
|
||||||
return Some(Anchor::WrapInBlock(node));
|
|
||||||
}
|
|
||||||
if parent.kind() == MATCH_ARM {
|
|
||||||
if node.kind() == MATCH_GUARD {
|
|
||||||
cov_mark::hit!(test_extract_var_in_match_guard);
|
|
||||||
} else {
|
|
||||||
cov_mark::hit!(test_extract_var_in_match_arm_no_block);
|
|
||||||
return Some(Anchor::WrapInBlock(node));
|
return Some(Anchor::WrapInBlock(node));
|
||||||
}
|
}
|
||||||
}
|
if parent.kind() == MATCH_ARM {
|
||||||
}
|
if node.kind() == MATCH_GUARD {
|
||||||
|
cov_mark::hit!(test_extract_var_in_match_guard);
|
||||||
if let Some(stmt) = ast::Stmt::cast(node.clone()) {
|
} else {
|
||||||
if let ast::Stmt::ExprStmt(stmt) = stmt {
|
cov_mark::hit!(test_extract_var_in_match_arm_no_block);
|
||||||
if stmt.expr().as_ref() == Some(to_extract) {
|
return Some(Anchor::WrapInBlock(node));
|
||||||
return Some(Anchor::Replace(stmt));
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return Some(Anchor::Before(node));
|
|
||||||
}
|
if let Some(stmt) = ast::Stmt::cast(node.clone()) {
|
||||||
None
|
if let ast::Stmt::ExprStmt(stmt) = stmt {
|
||||||
})
|
if stmt.expr().as_ref() == Some(to_extract) {
|
||||||
|
return Some(Anchor::Replace(stmt));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return Some(Anchor::Before(node));
|
||||||
|
}
|
||||||
|
None
|
||||||
|
},
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn syntax(&self) -> &SyntaxNode {
|
fn syntax(&self) -> &SyntaxNode {
|
||||||
|
@ -844,4 +849,14 @@ fn main() {
|
||||||
"2 + 2",
|
"2 + 2",
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn extract_var_no_block_body() {
|
||||||
|
check_assist_not_applicable(
|
||||||
|
extract_variable,
|
||||||
|
r"
|
||||||
|
const X: usize = $0100$0;
|
||||||
|
",
|
||||||
|
);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue