Skip to content

Commit 6b77e32

Browse files
bors[bot]xffxff
andauthored
Merge #10306
10306: Generate function assist creates bad param names for const/static item args r=XFFXFF a=XFFXFF Try to fix #10278 Co-authored-by: zhoufan <[email protected]>
2 parents 254022c + 8690cfb commit 6b77e32

File tree

1 file changed

+87
-7
lines changed

1 file changed

+87
-7
lines changed

crates/ide_assists/src/handlers/generate_function.rs

+87-7
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
1-
use hir::{HasSource, HirDisplay, Module, TypeInfo};
2-
use ide_db::{base_db::FileId, helpers::SnippetCap};
1+
use hir::{HasSource, HirDisplay, Module, ModuleDef, Semantics, TypeInfo};
2+
use ide_db::{
3+
base_db::FileId,
4+
defs::{Definition, NameRefClass},
5+
helpers::SnippetCap,
6+
RootDatabase,
7+
};
38
use rustc_hash::{FxHashMap, FxHashSet};
49
use stdx::to_lower_snake_case;
510
use syntax::{
@@ -438,7 +443,7 @@ fn fn_args(
438443
let mut arg_names = Vec::new();
439444
let mut arg_types = Vec::new();
440445
for arg in call.arg_list()?.args() {
441-
arg_names.push(fn_arg_name(&arg));
446+
arg_names.push(fn_arg_name(&ctx.sema, &arg));
442447
arg_types.push(match fn_arg_type(ctx, target_module, &arg) {
443448
Some(ty) => {
444449
if !ty.is_empty() && ty.starts_with('&') {
@@ -503,12 +508,18 @@ fn deduplicate_arg_names(arg_names: &mut Vec<String>) {
503508
}
504509
}
505510

506-
fn fn_arg_name(arg_expr: &ast::Expr) -> String {
511+
fn fn_arg_name(sema: &Semantics<RootDatabase>, arg_expr: &ast::Expr) -> String {
507512
let name = (|| match arg_expr {
508-
ast::Expr::CastExpr(cast_expr) => Some(fn_arg_name(&cast_expr.expr()?)),
513+
ast::Expr::CastExpr(cast_expr) => Some(fn_arg_name(sema, &cast_expr.expr()?)),
509514
expr => {
510-
let s = expr.syntax().descendants().filter_map(ast::NameRef::cast).last()?.to_string();
511-
Some(to_lower_snake_case(&s))
515+
let name_ref = expr.syntax().descendants().filter_map(ast::NameRef::cast).last()?;
516+
if let Some(NameRefClass::Definition(Definition::ModuleDef(
517+
ModuleDef::Const(_) | ModuleDef::Static(_),
518+
))) = NameRefClass::classify(sema, &name_ref)
519+
{
520+
return Some(name_ref.to_string().to_lowercase());
521+
};
522+
Some(to_lower_snake_case(&name_ref.to_string()))
512523
}
513524
})();
514525
match name {
@@ -1683,6 +1694,75 @@ fn main() {
16831694
fn foo(arg0: ()) ${0:-> _} {
16841695
todo!()
16851696
}
1697+
",
1698+
)
1699+
}
1700+
1701+
#[test]
1702+
fn add_function_with_const_arg() {
1703+
check_assist(
1704+
generate_function,
1705+
r"
1706+
const VALUE: usize = 0;
1707+
fn main() {
1708+
foo$0(VALUE);
1709+
}
1710+
",
1711+
r"
1712+
const VALUE: usize = 0;
1713+
fn main() {
1714+
foo(VALUE);
1715+
}
1716+
1717+
fn foo(value: usize) ${0:-> _} {
1718+
todo!()
1719+
}
1720+
",
1721+
)
1722+
}
1723+
1724+
#[test]
1725+
fn add_function_with_static_arg() {
1726+
check_assist(
1727+
generate_function,
1728+
r"
1729+
static VALUE: usize = 0;
1730+
fn main() {
1731+
foo$0(VALUE);
1732+
}
1733+
",
1734+
r"
1735+
static VALUE: usize = 0;
1736+
fn main() {
1737+
foo(VALUE);
1738+
}
1739+
1740+
fn foo(value: usize) ${0:-> _} {
1741+
todo!()
1742+
}
1743+
",
1744+
)
1745+
}
1746+
1747+
#[test]
1748+
fn add_function_with_static_mut_arg() {
1749+
check_assist(
1750+
generate_function,
1751+
r"
1752+
static mut VALUE: usize = 0;
1753+
fn main() {
1754+
foo$0(VALUE);
1755+
}
1756+
",
1757+
r"
1758+
static mut VALUE: usize = 0;
1759+
fn main() {
1760+
foo(VALUE);
1761+
}
1762+
1763+
fn foo(value: usize) ${0:-> _} {
1764+
todo!()
1765+
}
16861766
",
16871767
)
16881768
}

0 commit comments

Comments
 (0)