@@ -2,7 +2,7 @@ use crate::assist_context::{AssistContext, Assists};
2
2
use ide_db:: { LineIndexDatabase , assists:: AssistId , defs:: Definition } ;
3
3
use syntax:: {
4
4
AstNode ,
5
- ast:: { self , edit_in_place:: Indent } ,
5
+ ast:: { self , HasName , edit_in_place:: Indent } ,
6
6
} ;
7
7
8
8
// Assist: bind_unused_param
@@ -22,6 +22,7 @@ pub(crate) fn bind_unused_param(acc: &mut Assists, ctx: &AssistContext<'_>) -> O
22
22
let param: ast:: Param = ctx. find_node_at_offset ( ) ?;
23
23
24
24
let Some ( ast:: Pat :: IdentPat ( ident_pat) ) = param. pat ( ) else { return None } ;
25
+ let name = ident_pat. name ( ) . filter ( |n| !n. text ( ) . starts_with ( '_' ) ) ?;
25
26
26
27
let param_def = {
27
28
let local = ctx. sema . to_def ( & ident_pat) ?;
@@ -39,14 +40,14 @@ pub(crate) fn bind_unused_param(acc: &mut Assists, ctx: &AssistContext<'_>) -> O
39
40
40
41
acc. add (
41
42
AssistId :: quick_fix ( "bind_unused_param" ) ,
42
- format ! ( "Bind as `let _ = {ident_pat };`" ) ,
43
+ format ! ( "Bind as `let _ = {name };`" ) ,
43
44
param. syntax ( ) . text_range ( ) ,
44
45
|builder| {
45
46
let line_index = ctx. db ( ) . line_index ( ctx. vfs_file_id ( ) ) ;
46
47
47
48
let indent = func. indent_level ( ) ;
48
49
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 };" ) ;
50
51
51
52
let left_line = line_index. line_col ( l_curly_range. end ( ) ) . line ;
52
53
let right_line = line_index. line_col ( r_curly_range. start ( ) ) . line ;
@@ -83,6 +84,22 @@ fn foo(y: i32) {
83
84
) ;
84
85
}
85
86
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
+
86
103
#[ test]
87
104
fn bind_unused_empty_block_with_newline ( ) {
88
105
check_assist (
@@ -149,6 +166,16 @@ impl Trait for () {
149
166
bind_unused_param,
150
167
r#"
151
168
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) {}
152
179
"# ,
153
180
) ;
154
181
}
0 commit comments