From cb66bb8ff9609d4fc3702ac1ed6197802b54e473 Mon Sep 17 00:00:00 2001 From: ivan770 Date: Tue, 8 Dec 2020 20:47:20 +0200 Subject: Remove this semicolon --- crates/ide/src/diagnostics.rs | 3 +++ crates/ide/src/diagnostics/fixes.rs | 26 ++++++++++++++++++++------ 2 files changed, 23 insertions(+), 6 deletions(-) (limited to 'crates/ide') diff --git a/crates/ide/src/diagnostics.rs b/crates/ide/src/diagnostics.rs index c8453edb3..9157704dc 100644 --- a/crates/ide/src/diagnostics.rs +++ b/crates/ide/src/diagnostics.rs @@ -131,6 +131,9 @@ pub(crate) fn diagnostics( .on::(|d| { res.borrow_mut().push(diagnostic_with_fix(d, &sema)); }) + .on::(|d| { + res.borrow_mut().push(diagnostic_with_fix(d, &sema)); + }) .on::(|d| { res.borrow_mut().push(warning_with_fix(d, &sema)); }) diff --git a/crates/ide/src/diagnostics/fixes.rs b/crates/ide/src/diagnostics/fixes.rs index d275dd75b..cb4d49ad9 100644 --- a/crates/ide/src/diagnostics/fixes.rs +++ b/crates/ide/src/diagnostics/fixes.rs @@ -4,7 +4,7 @@ use hir::{ db::AstDatabase, diagnostics::{ Diagnostic, IncorrectCase, MissingFields, MissingOkInTailExpr, NoSuchField, - UnresolvedModule, + RemoveThisSemicolon, UnresolvedModule, }, HasSource, HirDisplay, Semantics, VariantDef, }; @@ -13,11 +13,7 @@ use ide_db::{ source_change::{FileSystemEdit, SourceFileEdit}, RootDatabase, }; -use syntax::{ - algo, - ast::{self, edit::IndentLevel, make}, - AstNode, -}; +use syntax::{AstNode, Direction, T, algo, ast::{self, ExprStmt, edit::IndentLevel, make}}; use text_edit::TextEdit; use crate::{diagnostics::Fix, references::rename::rename_with_semantics, FilePosition}; @@ -102,6 +98,24 @@ impl DiagnosticWithFix for MissingOkInTailExpr { } } +impl DiagnosticWithFix for RemoveThisSemicolon { + fn fix(&self, sema: &Semantics) -> Option { + let root = sema.db.parse_or_expand(self.file)?; + + let semicolon = self.expr.to_node(&root) + .syntax() + .siblings_with_tokens(Direction::Next) + .filter_map(|it| it.into_token()) + .find(|it| it.kind() == T![;])? + .text_range(); + + let edit = TextEdit::delete(semicolon); + let source_change = SourceFileEdit { file_id: self.file.original_file(sema.db), edit }.into(); + + Some(Fix::new("Remove this semicolon", source_change, semicolon)) + } +} + impl DiagnosticWithFix for IncorrectCase { fn fix(&self, sema: &Semantics) -> Option { let root = sema.db.parse_or_expand(self.file)?; -- cgit v1.2.3