Unnamed repository; edit this file 'description' to name the repository.
Diffstat (limited to 'crates/ide-assists/src/handlers/promote_local_to_const.rs')
| -rw-r--r-- | crates/ide-assists/src/handlers/promote_local_to_const.rs | 44 |
1 files changed, 4 insertions, 40 deletions
diff --git a/crates/ide-assists/src/handlers/promote_local_to_const.rs b/crates/ide-assists/src/handlers/promote_local_to_const.rs index 7c2dc0e0c1..0cc771ff39 100644 --- a/crates/ide-assists/src/handlers/promote_local_to_const.rs +++ b/crates/ide-assists/src/handlers/promote_local_to_const.rs @@ -1,19 +1,17 @@ -use hir::{HirDisplay, ModuleDef, PathResolution, Semantics}; +use hir::HirDisplay; use ide_db::{ assists::{AssistId, AssistKind}, defs::Definition, - syntax_helpers::node_ext::preorder_expr, - RootDatabase, }; use stdx::to_upper_snake_case; use syntax::{ ast::{self, make, HasName}, - ted, AstNode, WalkEvent, + ted, AstNode, }; use crate::{ assist_context::{AssistContext, Assists}, - utils, + utils::{self}, }; // Assist: promote_local_to_const @@ -63,7 +61,7 @@ pub(crate) fn promote_local_to_const(acc: &mut Assists, ctx: &AssistContext<'_>) }; let initializer = let_stmt.initializer()?; - if !is_body_const(&ctx.sema, &initializer) { + if !utils::is_body_const(&ctx.sema, &initializer) { cov_mark::hit!(promote_local_non_const); return None; } @@ -103,40 +101,6 @@ pub(crate) fn promote_local_to_const(acc: &mut Assists, ctx: &AssistContext<'_>) ) } -fn is_body_const(sema: &Semantics<'_, RootDatabase>, expr: &ast::Expr) -> bool { - let mut is_const = true; - preorder_expr(expr, &mut |ev| { - let expr = match ev { - WalkEvent::Enter(_) if !is_const => return true, - WalkEvent::Enter(expr) => expr, - WalkEvent::Leave(_) => return false, - }; - match expr { - ast::Expr::CallExpr(call) => { - if let Some(ast::Expr::PathExpr(path_expr)) = call.expr() { - if let Some(PathResolution::Def(ModuleDef::Function(func))) = - path_expr.path().and_then(|path| sema.resolve_path(&path)) - { - is_const &= func.is_const(sema.db); - } - } - } - ast::Expr::MethodCallExpr(call) => { - is_const &= - sema.resolve_method_call(&call).map(|it| it.is_const(sema.db)).unwrap_or(true) - } - ast::Expr::ForExpr(_) - | ast::Expr::ReturnExpr(_) - | ast::Expr::TryExpr(_) - | ast::Expr::YieldExpr(_) - | ast::Expr::AwaitExpr(_) => is_const = false, - _ => (), - } - !is_const - }); - is_const -} - #[cfg(test)] mod tests { use crate::tests::{check_assist, check_assist_not_applicable}; |