Skip to content

Commit 91b8441

Browse files
committed
Auto merge of rust-lang#17055 - wyatt-herkamp:fix_panic_at_unused_variable, r=Veykril
fix: Replace Just the variable name in Unused Variable Diagnostic Fix Changes Unused Variable diagnostic to just look at the variable name, not the entire syntax range. Also added a test for an unused variable in an array destructure. Closes rust-lang#17053
2 parents d07f024 + 0eb7b6c commit 91b8441

File tree

1 file changed

+34
-2
lines changed

1 file changed

+34
-2
lines changed

crates/ide-diagnostics/src/handlers/unused_variables.rs

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ use ide_db::{
66
source_change::SourceChange,
77
RootDatabase,
88
};
9+
use syntax::TextRange;
910
use text_edit::TextEdit;
1011

1112
use crate::{Diagnostic, DiagnosticCode, DiagnosticsContext};
@@ -23,6 +24,16 @@ pub(crate) fn unused_variables(
2324
return None;
2425
}
2526
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 = d
29+
.local
30+
.primary_source(ctx.sema.db)
31+
.name()
32+
.map(|v| v.syntax().original_file_range_rooted(ctx.sema.db))
33+
.filter(|it| {
34+
Some(it.file_id) == ast.file_id.file_id()
35+
&& diagnostic_range.range.contains_range(it.range)
36+
});
2637
let var_name = d.local.name(ctx.sema.db);
2738
Some(
2839
Diagnostic::new_with_syntax_node_ptr(
@@ -31,20 +42,24 @@ pub(crate) fn unused_variables(
3142
"unused variable",
3243
ast,
3344
)
34-
.with_fixes(fixes(ctx.sema.db, var_name, diagnostic_range, ast.file_id.is_macro()))
45+
.with_fixes(name_range.and_then(|it| {
46+
fixes(ctx.sema.db, var_name, it.range, diagnostic_range, ast.file_id.is_macro())
47+
}))
3548
.experimental(),
3649
)
3750
}
3851

3952
fn fixes(
4053
db: &RootDatabase,
4154
var_name: Name,
55+
name_range: TextRange,
4256
diagnostic_range: FileRange,
4357
is_in_marco: bool,
4458
) -> Option<Vec<Assist>> {
4559
if is_in_marco {
4660
return None;
4761
}
62+
4863
Some(vec![Assist {
4964
id: AssistId("unscore_unused_variable_name", AssistKind::QuickFix),
5065
label: Label::new(format!(
@@ -56,7 +71,7 @@ fn fixes(
5671
target: diagnostic_range.range,
5772
source_change: Some(SourceChange::from_text_edit(
5873
diagnostic_range.file_id,
59-
TextEdit::replace(diagnostic_range.range, format!("_{}", var_name.display(db))),
74+
TextEdit::replace(name_range, format!("_{}", var_name.display(db))),
6075
)),
6176
trigger_signature_help: false,
6277
}])
@@ -221,6 +236,23 @@ macro_rules! my_macro {
221236
fn main() {
222237
my_macro!();
223238
}
239+
"#,
240+
);
241+
}
242+
#[test]
243+
fn unused_variable_in_array_destructure() {
244+
check_fix(
245+
r#"
246+
fn main() {
247+
let arr = [1, 2, 3, 4, 5];
248+
let [_x, y$0 @ ..] = arr;
249+
}
250+
"#,
251+
r#"
252+
fn main() {
253+
let arr = [1, 2, 3, 4, 5];
254+
let [_x, _y @ ..] = arr;
255+
}
224256
"#,
225257
);
226258
}

0 commit comments

Comments
 (0)