mirror of
https://github.com/rust-lang/rust-analyzer
synced 2024-11-15 17:28:09 +00:00
Auto merge of #15431 - alibektas:deunwrap/extract_function, r=Veykril
minor : Deunwrap extract_function #15398 subtask 5.
This commit is contained in:
commit
cc6c8209cb
1 changed files with 21 additions and 22 deletions
|
@ -531,7 +531,7 @@ impl FunctionBody {
|
||||||
|
|
||||||
fn extracted_from_trait_impl(&self) -> bool {
|
fn extracted_from_trait_impl(&self) -> bool {
|
||||||
match self.node().ancestors().find_map(ast::Impl::cast) {
|
match self.node().ancestors().find_map(ast::Impl::cast) {
|
||||||
Some(c) => return c.trait_().is_some(),
|
Some(c) => c.trait_().is_some(),
|
||||||
None => false,
|
None => false,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1048,23 +1048,17 @@ impl GenericParent {
|
||||||
fn generic_parents(parent: &SyntaxNode) -> Vec<GenericParent> {
|
fn generic_parents(parent: &SyntaxNode) -> Vec<GenericParent> {
|
||||||
let mut list = Vec::new();
|
let mut list = Vec::new();
|
||||||
if let Some(parent_item) = parent.ancestors().find_map(ast::Item::cast) {
|
if let Some(parent_item) = parent.ancestors().find_map(ast::Item::cast) {
|
||||||
match parent_item {
|
if let ast::Item::Fn(ref fn_) = parent_item {
|
||||||
ast::Item::Fn(ref fn_) => {
|
if let Some(parent_parent) =
|
||||||
if let Some(parent_parent) = parent_item
|
parent_item.syntax().parent().and_then(|it| it.parent()).and_then(ast::Item::cast)
|
||||||
.syntax()
|
{
|
||||||
.parent()
|
match parent_parent {
|
||||||
.and_then(|it| it.parent())
|
ast::Item::Impl(impl_) => list.push(GenericParent::Impl(impl_)),
|
||||||
.and_then(ast::Item::cast)
|
ast::Item::Trait(trait_) => list.push(GenericParent::Trait(trait_)),
|
||||||
{
|
_ => (),
|
||||||
match parent_parent {
|
|
||||||
ast::Item::Impl(impl_) => list.push(GenericParent::Impl(impl_)),
|
|
||||||
ast::Item::Trait(trait_) => list.push(GenericParent::Trait(trait_)),
|
|
||||||
_ => (),
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
list.push(GenericParent::Fn(fn_.clone()));
|
|
||||||
}
|
}
|
||||||
_ => (),
|
list.push(GenericParent::Fn(fn_.clone()));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
list
|
list
|
||||||
|
@ -1728,7 +1722,7 @@ fn make_body(
|
||||||
let block = match &fun.body {
|
let block = match &fun.body {
|
||||||
FunctionBody::Expr(expr) => {
|
FunctionBody::Expr(expr) => {
|
||||||
let expr = rewrite_body_segment(ctx, &fun.params, &handler, expr.syntax());
|
let expr = rewrite_body_segment(ctx, &fun.params, &handler, expr.syntax());
|
||||||
let expr = ast::Expr::cast(expr).unwrap();
|
let expr = ast::Expr::cast(expr).expect("Body segment should be an expr");
|
||||||
match expr {
|
match expr {
|
||||||
ast::Expr::BlockExpr(block) => {
|
ast::Expr::BlockExpr(block) => {
|
||||||
// If the extracted expression is itself a block, there is no need to wrap it inside another block.
|
// If the extracted expression is itself a block, there is no need to wrap it inside another block.
|
||||||
|
@ -1868,9 +1862,8 @@ fn with_tail_expr(block: ast::BlockExpr, tail_expr: ast::Expr) -> ast::BlockExpr
|
||||||
|
|
||||||
if let Some(stmt_list) = block.stmt_list() {
|
if let Some(stmt_list) = block.stmt_list() {
|
||||||
stmt_list.syntax().children_with_tokens().for_each(|node_or_token| {
|
stmt_list.syntax().children_with_tokens().for_each(|node_or_token| {
|
||||||
match &node_or_token {
|
if let syntax::NodeOrToken::Token(_) = &node_or_token {
|
||||||
syntax::NodeOrToken::Token(_) => elements.push(node_or_token),
|
elements.push(node_or_token)
|
||||||
_ => (),
|
|
||||||
};
|
};
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
@ -1934,12 +1927,18 @@ fn fix_param_usages(ctx: &AssistContext<'_>, params: &[Param], syntax: &SyntaxNo
|
||||||
Some(ast::Expr::RefExpr(node))
|
Some(ast::Expr::RefExpr(node))
|
||||||
if param.kind() == ParamKind::MutRef && node.mut_token().is_some() =>
|
if param.kind() == ParamKind::MutRef && node.mut_token().is_some() =>
|
||||||
{
|
{
|
||||||
ted::replace(node.syntax(), node.expr().unwrap().syntax());
|
ted::replace(
|
||||||
|
node.syntax(),
|
||||||
|
node.expr().expect("RefExpr::expr() cannot be None").syntax(),
|
||||||
|
);
|
||||||
}
|
}
|
||||||
Some(ast::Expr::RefExpr(node))
|
Some(ast::Expr::RefExpr(node))
|
||||||
if param.kind() == ParamKind::SharedRef && node.mut_token().is_none() =>
|
if param.kind() == ParamKind::SharedRef && node.mut_token().is_none() =>
|
||||||
{
|
{
|
||||||
ted::replace(node.syntax(), node.expr().unwrap().syntax());
|
ted::replace(
|
||||||
|
node.syntax(),
|
||||||
|
node.expr().expect("RefExpr::expr() cannot be None").syntax(),
|
||||||
|
);
|
||||||
}
|
}
|
||||||
Some(_) | None => {
|
Some(_) | None => {
|
||||||
let p = &make::expr_prefix(T![*], usage.clone()).clone_for_update();
|
let p = &make::expr_prefix(T![*], usage.clone()).clone_for_update();
|
||||||
|
|
Loading…
Reference in a new issue