@@ -6,6 +6,7 @@ use ide_db::{
6
6
source_change:: SourceChange ,
7
7
RootDatabase ,
8
8
} ;
9
+ use syntax:: TextRange ;
9
10
use text_edit:: TextEdit ;
10
11
11
12
use crate :: { Diagnostic , DiagnosticCode , DiagnosticsContext } ;
@@ -23,6 +24,13 @@ pub(crate) fn unused_variables(
23
24
return None ;
24
25
}
25
26
let diagnostic_range = ctx. sema . diagnostics_display_range ( ast) ;
27
+ // The range for the Actual Name. We don't want to replace the entire declarition. Using the diagnostic range causes issues within in Array Destructuring.
28
+ let name_range =
29
+ d. local . primary_source ( ctx. sema . db ) . name ( ) . map ( |v| v. syntax ( ) . value . text_range ( ) ) ?;
30
+ // Make sure we are within the diagnostic range for the variable
31
+ if !diagnostic_range. range . contains_range ( name_range) {
32
+ return None ;
33
+ }
26
34
let var_name = d. local . name ( ctx. sema . db ) ;
27
35
Some (
28
36
Diagnostic :: new_with_syntax_node_ptr (
@@ -31,20 +39,28 @@ pub(crate) fn unused_variables(
31
39
"unused variable" ,
32
40
ast,
33
41
)
34
- . with_fixes ( fixes ( ctx. sema . db , var_name, diagnostic_range, ast. file_id . is_macro ( ) ) )
42
+ . with_fixes ( fixes (
43
+ ctx. sema . db ,
44
+ var_name,
45
+ name_range,
46
+ diagnostic_range,
47
+ ast. file_id . is_macro ( ) ,
48
+ ) )
35
49
. experimental ( ) ,
36
50
)
37
51
}
38
52
39
53
fn fixes (
40
54
db : & RootDatabase ,
41
55
var_name : Name ,
56
+ name_range : TextRange ,
42
57
diagnostic_range : FileRange ,
43
58
is_in_marco : bool ,
44
59
) -> Option < Vec < Assist > > {
45
60
if is_in_marco {
46
61
return None ;
47
62
}
63
+
48
64
Some ( vec ! [ Assist {
49
65
id: AssistId ( "unscore_unused_variable_name" , AssistKind :: QuickFix ) ,
50
66
label: Label :: new( format!(
@@ -56,7 +72,7 @@ fn fixes(
56
72
target: diagnostic_range. range,
57
73
source_change: Some ( SourceChange :: from_text_edit(
58
74
diagnostic_range. file_id,
59
- TextEdit :: replace( diagnostic_range . range , format!( "_{}" , var_name. display( db) ) ) ,
75
+ TextEdit :: replace( name_range , format!( "_{}" , var_name. display( db) ) ) ,
60
76
) ) ,
61
77
trigger_signature_help: false ,
62
78
} ] )
@@ -221,6 +237,23 @@ macro_rules! my_macro {
221
237
fn main() {
222
238
my_macro!();
223
239
}
240
+ "# ,
241
+ ) ;
242
+ }
243
+ #[ test]
244
+ fn unused_variable_in_array_destructure ( ) {
245
+ check_fix (
246
+ r#"
247
+ fn main() {
248
+ let arr = [1, 2, 3, 4, 5];
249
+ let [_x, y$0 @ ..] = arr;
250
+ }
251
+ "# ,
252
+ r#"
253
+ fn main() {
254
+ let arr = [1, 2, 3, 4, 5];
255
+ let [_x, _y @ ..] = arr;
256
+ }
224
257
"# ,
225
258
) ;
226
259
}
0 commit comments