feat: add fix for unused_variables
This commit is contained in:
parent
a0dd822972
commit
77136575da
@ -1,3 +1,11 @@
|
||||
use ide_db::{
|
||||
assists::{Assist, AssistId, AssistKind},
|
||||
base_db::FileRange,
|
||||
label::Label,
|
||||
source_change::SourceChange,
|
||||
};
|
||||
use text_edit::TextEdit;
|
||||
|
||||
use crate::{Diagnostic, DiagnosticCode, DiagnosticsContext};
|
||||
|
||||
// Diagnostic: unused-variables
|
||||
@ -8,15 +16,35 @@ pub(crate) fn unused_variables(
|
||||
d: &hir::UnusedVariable,
|
||||
) -> Diagnostic {
|
||||
let ast = d.local.primary_source(ctx.sema.db).syntax_ptr();
|
||||
let diagnostic_range = ctx.sema.diagnostics_display_range(ast);
|
||||
let var_name = d.local.primary_source(ctx.sema.db).syntax().to_string();
|
||||
Diagnostic::new_with_syntax_node_ptr(
|
||||
ctx,
|
||||
DiagnosticCode::RustcLint("unused_variables"),
|
||||
"unused variable",
|
||||
ast,
|
||||
)
|
||||
.with_fixes(fixes(&var_name, diagnostic_range, ast.file_id.is_macro()))
|
||||
.experimental()
|
||||
}
|
||||
|
||||
fn fixes(var_name: &String, diagnostic_range: FileRange, is_in_marco: bool) -> Option<Vec<Assist>> {
|
||||
if is_in_marco {
|
||||
return None;
|
||||
}
|
||||
Some(vec![Assist {
|
||||
id: AssistId("unscore_unused_variable_name", AssistKind::QuickFix),
|
||||
label: Label::new(format!("Rename unused {} to _{}", var_name, var_name)),
|
||||
group: None,
|
||||
target: diagnostic_range.range,
|
||||
source_change: Some(SourceChange::from_text_edit(
|
||||
diagnostic_range.file_id,
|
||||
TextEdit::replace(diagnostic_range.range, format!("_{}", var_name)),
|
||||
)),
|
||||
trigger_signature_help: false,
|
||||
}])
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use crate::tests::check_diagnostics;
|
||||
|
Loading…
x
Reference in New Issue
Block a user