1
+ use ide_db:: {
2
+ assists:: { Assist , AssistId , AssistKind } ,
3
+ base_db:: FileRange ,
4
+ label:: Label ,
5
+ source_change:: SourceChange ,
6
+ } ;
7
+ use text_edit:: TextEdit ;
8
+
1
9
use crate :: { Diagnostic , DiagnosticCode , DiagnosticsContext } ;
2
10
3
11
// Diagnostic: unused-variables
@@ -8,15 +16,35 @@ pub(crate) fn unused_variables(
8
16
d : & hir:: UnusedVariable ,
9
17
) -> Diagnostic {
10
18
let ast = d. local . primary_source ( ctx. sema . db ) . syntax_ptr ( ) ;
19
+ let diagnostic_range = ctx. sema . diagnostics_display_range ( ast) ;
20
+ let var_name = d. local . primary_source ( ctx. sema . db ) . syntax ( ) . to_string ( ) ;
11
21
Diagnostic :: new_with_syntax_node_ptr (
12
22
ctx,
13
23
DiagnosticCode :: RustcLint ( "unused_variables" ) ,
14
24
"unused variable" ,
15
25
ast,
16
26
)
27
+ . with_fixes ( fixes ( & var_name, diagnostic_range, ast. file_id . is_macro ( ) ) )
17
28
. experimental ( )
18
29
}
19
30
31
+ fn fixes ( var_name : & String , diagnostic_range : FileRange , is_in_marco : bool ) -> Option < Vec < Assist > > {
32
+ if is_in_marco {
33
+ return None ;
34
+ }
35
+ Some ( vec ! [ Assist {
36
+ id: AssistId ( "unscore_unused_variable_name" , AssistKind :: QuickFix ) ,
37
+ label: Label :: new( format!( "Rename unused {} to _{}" , var_name, var_name) ) ,
38
+ group: None ,
39
+ target: diagnostic_range. range,
40
+ source_change: Some ( SourceChange :: from_text_edit(
41
+ diagnostic_range. file_id,
42
+ TextEdit :: replace( diagnostic_range. range, format!( "_{}" , var_name) ) ,
43
+ ) ) ,
44
+ trigger_signature_help: false ,
45
+ } ] )
46
+ }
47
+
20
48
#[ cfg( test) ]
21
49
mod tests {
22
50
use crate :: tests:: check_diagnostics;
0 commit comments