From d5e6aa39c1cd6d45a6048fb41281ae23915cb89d Mon Sep 17 00:00:00 2001 From: Tiddo Langerak Date: Thu, 1 Sep 2022 08:37:30 +0300 Subject: [PATCH] Pre-cast impl nodes to ast::Impl in find_non_trait_impl --- .../src/handlers/extract_function.rs | 32 ++++++++----------- 1 file changed, 13 insertions(+), 19 deletions(-) diff --git a/crates/ide-assists/src/handlers/extract_function.rs b/crates/ide-assists/src/handlers/extract_function.rs index 969b0c1fb3f..c40fb291a32 100644 --- a/crates/ide-assists/src/handlers/extract_function.rs +++ b/crates/ide-assists/src/handlers/extract_function.rs @@ -1276,32 +1276,26 @@ fn node_to_insert_after(body: &FunctionBody, anchor: Anchor) -> Option Option { - let impl_type = Some(impl_type_name(trait_impl)?); +fn find_non_trait_impl(trait_impl: &SyntaxNode) -> Option { + let as_impl = ast::Impl::cast(trait_impl.clone())?; + let impl_type = Some(impl_type_name(&as_impl)?); - let mut sibblings = trait_impl.parent()?.children(); - sibblings.find(|s| impl_type_name(s) == impl_type && !is_trait_impl(s)) + let sibblings = trait_impl.parent()?.children(); + sibblings.filter_map(ast::Impl::cast) + .find(|s| impl_type_name(s) == impl_type && !is_trait_impl(s)) } -fn last_impl_member(impl_node: &SyntaxNode) -> Option { - impl_node.children().find(|c| c.kind() == SyntaxKind::ASSOC_ITEM_LIST)?.last_child() +fn last_impl_member(impl_node: &ast::Impl) -> Option { + let last_child = impl_node.assoc_item_list()?.assoc_items().last()?; + Some(last_child.syntax().clone()) } -fn is_trait_impl(node: &SyntaxNode) -> bool { - if !ast::Impl::can_cast(node.kind()) { - return false; - } - match ast::Impl::cast(node.clone()) { - Some(c) => c.trait_().is_some(), - None => false, - } +fn is_trait_impl(node: &ast::Impl) -> bool { + node.trait_().is_some() } -fn impl_type_name(impl_node: &SyntaxNode) -> Option { - if !ast::Impl::can_cast(impl_node.kind()) { - return None; - } - Some(ast::Impl::cast(impl_node.clone())?.self_ty()?.to_string()) +fn impl_type_name(impl_node: &ast::Impl) -> Option { + Some(impl_node.self_ty()?.to_string()) } fn make_call(ctx: &AssistContext<'_>, fun: &Function, indent: IndentLevel) -> String {