Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit d5e6aa3

Browse files
author
Tiddo Langerak
committed
Pre-cast impl nodes to ast::Impl in find_non_trait_impl
1 parent 7402366 commit d5e6aa3

File tree

1 file changed

+13
-19
lines changed

1 file changed

+13
-19
lines changed

crates/ide-assists/src/handlers/extract_function.rs

Lines changed: 13 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1276,32 +1276,26 @@ fn node_to_insert_after(body: &FunctionBody, anchor: Anchor) -> Option<SyntaxNod
12761276
last_ancestor
12771277
}
12781278

1279-
fn find_non_trait_impl(trait_impl: &SyntaxNode) -> Option<SyntaxNode> {
1280-
let impl_type = Some(impl_type_name(trait_impl)?);
1279+
fn find_non_trait_impl(trait_impl: &SyntaxNode) -> Option<ast::Impl> {
1280+
let as_impl = ast::Impl::cast(trait_impl.clone())?;
1281+
let impl_type = Some(impl_type_name(&as_impl)?);
12811282

1282-
let mut sibblings = trait_impl.parent()?.children();
1283-
sibblings.find(|s| impl_type_name(s) == impl_type && !is_trait_impl(s))
1283+
let sibblings = trait_impl.parent()?.children();
1284+
sibblings.filter_map(ast::Impl::cast)
1285+
.find(|s| impl_type_name(s) == impl_type && !is_trait_impl(s))
12841286
}
12851287

1286-
fn last_impl_member(impl_node: &SyntaxNode) -> Option<SyntaxNode> {
1287-
impl_node.children().find(|c| c.kind() == SyntaxKind::ASSOC_ITEM_LIST)?.last_child()
1288+
fn last_impl_member(impl_node: &ast::Impl) -> Option<SyntaxNode> {
1289+
let last_child = impl_node.assoc_item_list()?.assoc_items().last()?;
1290+
Some(last_child.syntax().clone())
12881291
}
12891292

1290-
fn is_trait_impl(node: &SyntaxNode) -> bool {
1291-
if !ast::Impl::can_cast(node.kind()) {
1292-
return false;
1293-
}
1294-
match ast::Impl::cast(node.clone()) {
1295-
Some(c) => c.trait_().is_some(),
1296-
None => false,
1297-
}
1293+
fn is_trait_impl(node: &ast::Impl) -> bool {
1294+
node.trait_().is_some()
12981295
}
12991296

1300-
fn impl_type_name(impl_node: &SyntaxNode) -> Option<String> {
1301-
if !ast::Impl::can_cast(impl_node.kind()) {
1302-
return None;
1303-
}
1304-
Some(ast::Impl::cast(impl_node.clone())?.self_ty()?.to_string())
1297+
fn impl_type_name(impl_node: &ast::Impl) -> Option<String> {
1298+
Some(impl_node.self_ty()?.to_string())
13051299
}
13061300

13071301
fn make_call(ctx: &AssistContext<'_>, fun: &Function, indent: IndentLevel) -> String {

0 commit comments

Comments
 (0)