Skip to content

Commit b465ae0

Browse files
authored
Merge pull request #20723 from A4-Tacks/fix-bind-unused-applicable-underscore
Fix applicable on underscore for bind_unused_param
2 parents 3249fc7 + fef1ab5 commit b465ae0

File tree

1 file changed

+30
-3
lines changed

1 file changed

+30
-3
lines changed

crates/ide-assists/src/handlers/bind_unused_param.rs

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use crate::assist_context::{AssistContext, Assists};
22
use ide_db::{LineIndexDatabase, assists::AssistId, defs::Definition};
33
use syntax::{
44
AstNode,
5-
ast::{self, edit_in_place::Indent},
5+
ast::{self, HasName, edit_in_place::Indent},
66
};
77

88
// Assist: bind_unused_param
@@ -22,6 +22,7 @@ pub(crate) fn bind_unused_param(acc: &mut Assists, ctx: &AssistContext<'_>) -> O
2222
let param: ast::Param = ctx.find_node_at_offset()?;
2323

2424
let Some(ast::Pat::IdentPat(ident_pat)) = param.pat() else { return None };
25+
let name = ident_pat.name().filter(|n| !n.text().starts_with('_'))?;
2526

2627
let param_def = {
2728
let local = ctx.sema.to_def(&ident_pat)?;
@@ -39,14 +40,14 @@ pub(crate) fn bind_unused_param(acc: &mut Assists, ctx: &AssistContext<'_>) -> O
3940

4041
acc.add(
4142
AssistId::quick_fix("bind_unused_param"),
42-
format!("Bind as `let _ = {ident_pat};`"),
43+
format!("Bind as `let _ = {name};`"),
4344
param.syntax().text_range(),
4445
|builder| {
4546
let line_index = ctx.db().line_index(ctx.vfs_file_id());
4647

4748
let indent = func.indent_level();
4849
let text_indent = indent + 1;
49-
let mut text = format!("\n{text_indent}let _ = {ident_pat};");
50+
let mut text = format!("\n{text_indent}let _ = {name};");
5051

5152
let left_line = line_index.line_col(l_curly_range.end()).line;
5253
let right_line = line_index.line_col(r_curly_range.start()).line;
@@ -83,6 +84,22 @@ fn foo(y: i32) {
8384
);
8485
}
8586

87+
#[test]
88+
fn bind_unused_ref_ident_pat() {
89+
cov_mark::check!(single_line);
90+
check_assist(
91+
bind_unused_param,
92+
r#"
93+
fn foo(ref $0y: i32) {}
94+
"#,
95+
r#"
96+
fn foo(ref y: i32) {
97+
let _ = y;
98+
}
99+
"#,
100+
);
101+
}
102+
86103
#[test]
87104
fn bind_unused_empty_block_with_newline() {
88105
check_assist(
@@ -149,6 +166,16 @@ impl Trait for () {
149166
bind_unused_param,
150167
r#"
151168
fn foo(x: i32, $0y: i32) { y; }
169+
"#,
170+
);
171+
}
172+
173+
#[test]
174+
fn keep_underscore_used() {
175+
check_assist_not_applicable(
176+
bind_unused_param,
177+
r#"
178+
fn foo($0_x: i32, y: i32) {}
152179
"#,
153180
);
154181
}

0 commit comments

Comments
 (0)