Use parent + and_then instead of ancestors

This commit is contained in:
Lukas Wirth 2023-09-22 08:53:24 +02:00
parent 25b1b3e753
commit 93562dd5bd

View File

@ -111,7 +111,7 @@ fn find_bool_node(ctx: &AssistContext<'_>) -> Option<BoolNodeData> {
initializer: let_stmt.initializer(), initializer: let_stmt.initializer(),
definition: Definition::Local(def), definition: Definition::Local(def),
}) })
} else if let Some(const_) = name.syntax().ancestors().find_map(ast::Const::cast) { } else if let Some(const_) = name.syntax().parent().and_then(ast::Const::cast) {
let def = ctx.sema.to_def(&const_)?; let def = ctx.sema.to_def(&const_)?;
if !def.ty(ctx.db()).is_bool() { if !def.ty(ctx.db()).is_bool() {
cov_mark::hit!(not_applicable_non_bool_const); cov_mark::hit!(not_applicable_non_bool_const);
@ -125,7 +125,7 @@ fn find_bool_node(ctx: &AssistContext<'_>) -> Option<BoolNodeData> {
initializer: const_.body(), initializer: const_.body(),
definition: Definition::Const(def), definition: Definition::Const(def),
}) })
} else if let Some(static_) = name.syntax().ancestors().find_map(ast::Static::cast) { } else if let Some(static_) = name.syntax().parent().and_then(ast::Static::cast) {
let def = ctx.sema.to_def(&static_)?; let def = ctx.sema.to_def(&static_)?;
if !def.ty(ctx.db()).is_bool() { if !def.ty(ctx.db()).is_bool() {
cov_mark::hit!(not_applicable_non_bool_static); cov_mark::hit!(not_applicable_non_bool_static);
@ -140,7 +140,7 @@ fn find_bool_node(ctx: &AssistContext<'_>) -> Option<BoolNodeData> {
definition: Definition::Static(def), definition: Definition::Static(def),
}) })
} else { } else {
let field = name.syntax().ancestors().find_map(ast::RecordField::cast)?; let field = name.syntax().parent().and_then(ast::RecordField::cast)?;
if field.name()? != name { if field.name()? != name {
return None; return None;
} }