Skip to content

Commit 7912d3a

Browse files
committed
Generate function assist creates bad param names for const/static item args
1 parent 254022c commit 7912d3a

File tree

1 file changed

+93
-7
lines changed

1 file changed

+93
-7
lines changed

crates/ide_assists/src/handlers/generate_function.rs

Lines changed: 93 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
1-
use hir::{HasSource, HirDisplay, Module, TypeInfo};
2-
use ide_db::{base_db::FileId, helpers::SnippetCap};
1+
use hir::{HasSource, HirDisplay, Module, ModuleDef, TypeInfo};
2+
use ide_db::{
3+
base_db::FileId,
4+
defs::{Definition, NameRefClass},
5+
helpers::SnippetCap,
6+
};
37
use rustc_hash::{FxHashMap, FxHashSet};
48
use stdx::to_lower_snake_case;
59
use syntax::{
@@ -438,7 +442,7 @@ fn fn_args(
438442
let mut arg_names = Vec::new();
439443
let mut arg_types = Vec::new();
440444
for arg in call.arg_list()?.args() {
441-
arg_names.push(fn_arg_name(&arg));
445+
arg_names.push(fn_arg_name(ctx, &arg));
442446
arg_types.push(match fn_arg_type(ctx, target_module, &arg) {
443447
Some(ty) => {
444448
if !ty.is_empty() && ty.starts_with('&') {
@@ -503,12 +507,25 @@ fn deduplicate_arg_names(arg_names: &mut Vec<String>) {
503507
}
504508
}
505509

506-
fn fn_arg_name(arg_expr: &ast::Expr) -> String {
510+
fn fn_arg_name(ctx: &AssistContext, arg_expr: &ast::Expr) -> String {
507511
let name = (|| match arg_expr {
508-
ast::Expr::CastExpr(cast_expr) => Some(fn_arg_name(&cast_expr.expr()?)),
512+
ast::Expr::CastExpr(cast_expr) => Some(fn_arg_name(ctx, &cast_expr.expr()?)),
509513
expr => {
510-
let s = expr.syntax().descendants().filter_map(ast::NameRef::cast).last()?.to_string();
511-
Some(to_lower_snake_case(&s))
514+
let name_ref = expr.syntax().descendants().filter_map(ast::NameRef::cast).last()?;
515+
if let Some(NameRefClass::Definition(def)) =
516+
NameRefClass::classify(&ctx.sema, &name_ref)
517+
{
518+
match def {
519+
Definition::ModuleDef(ModuleDef::Const(_)) => {
520+
return Some(name_ref.to_string().to_lowercase());
521+
}
522+
Definition::ModuleDef(ModuleDef::Static(_)) => {
523+
return Some(name_ref.to_string().to_lowercase());
524+
}
525+
_ => {}
526+
}
527+
};
528+
Some(to_lower_snake_case(&name_ref.to_string()))
512529
}
513530
})();
514531
match name {
@@ -1683,6 +1700,75 @@ fn main() {
16831700
fn foo(arg0: ()) ${0:-> _} {
16841701
todo!()
16851702
}
1703+
",
1704+
)
1705+
}
1706+
1707+
#[test]
1708+
fn add_function_with_const_arg() {
1709+
check_assist(
1710+
generate_function,
1711+
r"
1712+
const VALUE: usize = 0;
1713+
fn main() {
1714+
foo$0(VALUE);
1715+
}
1716+
",
1717+
r"
1718+
const VALUE: usize = 0;
1719+
fn main() {
1720+
foo(VALUE);
1721+
}
1722+
1723+
fn foo(value: usize) ${0:-> _} {
1724+
todo!()
1725+
}
1726+
",
1727+
)
1728+
}
1729+
1730+
#[test]
1731+
fn add_function_with_static_arg() {
1732+
check_assist(
1733+
generate_function,
1734+
r"
1735+
static VALUE: usize = 0;
1736+
fn main() {
1737+
foo$0(VALUE);
1738+
}
1739+
",
1740+
r"
1741+
static VALUE: usize = 0;
1742+
fn main() {
1743+
foo(VALUE);
1744+
}
1745+
1746+
fn foo(value: usize) ${0:-> _} {
1747+
todo!()
1748+
}
1749+
",
1750+
)
1751+
}
1752+
1753+
#[test]
1754+
fn add_function_with_static_mut_arg() {
1755+
check_assist(
1756+
generate_function,
1757+
r"
1758+
static mut VALUE: usize = 0;
1759+
fn main() {
1760+
foo$0(VALUE);
1761+
}
1762+
",
1763+
r"
1764+
static mut VALUE: usize = 0;
1765+
fn main() {
1766+
foo(VALUE);
1767+
}
1768+
1769+
fn foo(value: usize) ${0:-> _} {
1770+
todo!()
1771+
}
16861772
",
16871773
)
16881774
}

0 commit comments

Comments
 (0)