Skip to content

Commit 92ba350

Browse files
committed
internal: Move is_body_const to ide_assists::utils
1 parent 3dcda33 commit 92ba350

File tree

2 files changed

+44
-44
lines changed

2 files changed

+44
-44
lines changed

src/tools/rust-analyzer/crates/ide-assists/src/handlers/promote_local_to_const.rs

Lines changed: 4 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,17 @@
1-
use hir::{HirDisplay, ModuleDef, PathResolution, Semantics};
1+
use hir::HirDisplay;
22
use ide_db::{
33
assists::{AssistId, AssistKind},
44
defs::Definition,
5-
syntax_helpers::node_ext::preorder_expr,
6-
RootDatabase,
75
};
86
use stdx::to_upper_snake_case;
97
use syntax::{
108
ast::{self, make, HasName},
11-
ted, AstNode, WalkEvent,
9+
ted, AstNode,
1210
};
1311

1412
use crate::{
1513
assist_context::{AssistContext, Assists},
16-
utils,
14+
utils::{self},
1715
};
1816

1917
// Assist: promote_local_to_const
@@ -63,7 +61,7 @@ pub(crate) fn promote_local_to_const(acc: &mut Assists, ctx: &AssistContext<'_>)
6361
};
6462

6563
let initializer = let_stmt.initializer()?;
66-
if !is_body_const(&ctx.sema, &initializer) {
64+
if !utils::is_body_const(&ctx.sema, &initializer) {
6765
cov_mark::hit!(promote_local_non_const);
6866
return None;
6967
}
@@ -103,40 +101,6 @@ pub(crate) fn promote_local_to_const(acc: &mut Assists, ctx: &AssistContext<'_>)
103101
)
104102
}
105103

106-
fn is_body_const(sema: &Semantics<'_, RootDatabase>, expr: &ast::Expr) -> bool {
107-
let mut is_const = true;
108-
preorder_expr(expr, &mut |ev| {
109-
let expr = match ev {
110-
WalkEvent::Enter(_) if !is_const => return true,
111-
WalkEvent::Enter(expr) => expr,
112-
WalkEvent::Leave(_) => return false,
113-
};
114-
match expr {
115-
ast::Expr::CallExpr(call) => {
116-
if let Some(ast::Expr::PathExpr(path_expr)) = call.expr() {
117-
if let Some(PathResolution::Def(ModuleDef::Function(func))) =
118-
path_expr.path().and_then(|path| sema.resolve_path(&path))
119-
{
120-
is_const &= func.is_const(sema.db);
121-
}
122-
}
123-
}
124-
ast::Expr::MethodCallExpr(call) => {
125-
is_const &=
126-
sema.resolve_method_call(&call).map(|it| it.is_const(sema.db)).unwrap_or(true)
127-
}
128-
ast::Expr::ForExpr(_)
129-
| ast::Expr::ReturnExpr(_)
130-
| ast::Expr::TryExpr(_)
131-
| ast::Expr::YieldExpr(_)
132-
| ast::Expr::AwaitExpr(_) => is_const = false,
133-
_ => (),
134-
}
135-
!is_const
136-
});
137-
is_const
138-
}
139-
140104
#[cfg(test)]
141105
mod tests {
142106
use crate::tests::{check_assist, check_assist_not_applicable};

src/tools/rust-analyzer/crates/ide-assists/src/utils.rs

Lines changed: 40 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,13 @@
33
pub(crate) use gen_trait_fn_body::gen_trait_fn_body;
44
use hir::{
55
db::{ExpandDatabase, HirDatabase},
6-
HasAttrs as HirHasAttrs, HirDisplay, InFile, Semantics,
6+
HasAttrs as HirHasAttrs, HirDisplay, InFile, ModuleDef, PathResolution, Semantics,
77
};
88
use ide_db::{
9-
famous_defs::FamousDefs, path_transform::PathTransform,
10-
syntax_helpers::prettify_macro_expansion, RootDatabase,
9+
famous_defs::FamousDefs,
10+
path_transform::PathTransform,
11+
syntax_helpers::{node_ext::preorder_expr, prettify_macro_expansion},
12+
RootDatabase,
1113
};
1214
use stdx::format_to;
1315
use syntax::{
@@ -19,7 +21,7 @@ use syntax::{
1921
},
2022
ted, AstNode, AstToken, Direction, Edition, NodeOrToken, SourceFile,
2123
SyntaxKind::*,
22-
SyntaxNode, SyntaxToken, TextRange, TextSize, T,
24+
SyntaxNode, SyntaxToken, TextRange, TextSize, WalkEvent, T,
2325
};
2426

2527
use crate::assist_context::{AssistContext, SourceChangeBuilder};
@@ -966,3 +968,37 @@ pub(crate) fn tt_from_syntax(node: SyntaxNode) -> Vec<NodeOrToken<ast::TokenTree
966968

967969
tt_stack.pop().expect("parent token tree was closed before it was completed").1
968970
}
971+
972+
pub fn is_body_const(sema: &Semantics<'_, RootDatabase>, expr: &ast::Expr) -> bool {
973+
let mut is_const = true;
974+
preorder_expr(expr, &mut |ev| {
975+
let expr = match ev {
976+
WalkEvent::Enter(_) if !is_const => return true,
977+
WalkEvent::Enter(expr) => expr,
978+
WalkEvent::Leave(_) => return false,
979+
};
980+
match expr {
981+
ast::Expr::CallExpr(call) => {
982+
if let Some(ast::Expr::PathExpr(path_expr)) = call.expr() {
983+
if let Some(PathResolution::Def(ModuleDef::Function(func))) =
984+
path_expr.path().and_then(|path| sema.resolve_path(&path))
985+
{
986+
is_const &= func.is_const(sema.db);
987+
}
988+
}
989+
}
990+
ast::Expr::MethodCallExpr(call) => {
991+
is_const &=
992+
sema.resolve_method_call(&call).map(|it| it.is_const(sema.db)).unwrap_or(true)
993+
}
994+
ast::Expr::ForExpr(_)
995+
| ast::Expr::ReturnExpr(_)
996+
| ast::Expr::TryExpr(_)
997+
| ast::Expr::YieldExpr(_)
998+
| ast::Expr::AwaitExpr(_) => is_const = false,
999+
_ => (),
1000+
}
1001+
!is_const
1002+
});
1003+
is_const
1004+
}

0 commit comments

Comments
 (0)