aboutsummaryrefslogtreecommitdiff
path: root/crates/ide_diagnostics/src/handlers/remove_this_semicolon.rs
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ide_diagnostics/src/handlers/remove_this_semicolon.rs')
-rw-r--r--crates/ide_diagnostics/src/handlers/remove_this_semicolon.rs61
1 files changed, 61 insertions, 0 deletions
diff --git a/crates/ide_diagnostics/src/handlers/remove_this_semicolon.rs b/crates/ide_diagnostics/src/handlers/remove_this_semicolon.rs
new file mode 100644
index 000000000..4e639f214
--- /dev/null
+++ b/crates/ide_diagnostics/src/handlers/remove_this_semicolon.rs
@@ -0,0 +1,61 @@
1use hir::db::AstDatabase;
2use ide_db::source_change::SourceChange;
3use syntax::{ast, AstNode};
4use text_edit::TextEdit;
5
6use crate::{fix, Assist, Diagnostic, DiagnosticsContext};
7
8// Diagnostic: remove-this-semicolon
9//
10// This diagnostic is triggered when there's an erroneous `;` at the end of the block.
11pub(crate) fn remove_this_semicolon(
12 ctx: &DiagnosticsContext<'_>,
13 d: &hir::RemoveThisSemicolon,
14) -> Diagnostic {
15 Diagnostic::new(
16 "remove-this-semicolon",
17 "remove this semicolon",
18 ctx.sema.diagnostics_display_range(d.expr.clone().map(|it| it.into())).range,
19 )
20 .with_fixes(fixes(ctx, d))
21}
22
23fn fixes(ctx: &DiagnosticsContext<'_>, d: &hir::RemoveThisSemicolon) -> Option<Vec<Assist>> {
24 let root = ctx.sema.db.parse_or_expand(d.expr.file_id)?;
25
26 let semicolon = d
27 .expr
28 .value
29 .to_node(&root)
30 .syntax()
31 .parent()
32 .and_then(ast::ExprStmt::cast)
33 .and_then(|expr| expr.semicolon_token())?
34 .text_range();
35
36 let edit = TextEdit::delete(semicolon);
37 let source_change =
38 SourceChange::from_text_edit(d.expr.file_id.original_file(ctx.sema.db), edit);
39
40 Some(vec![fix("remove_semicolon", "Remove this semicolon", source_change, semicolon)])
41}
42
43#[cfg(test)]
44mod tests {
45 use crate::tests::{check_diagnostics, check_fix};
46
47 #[test]
48 fn missing_semicolon() {
49 check_diagnostics(
50 r#"
51fn test() -> i32 { 123; }
52 //^^^ 💡 error: remove this semicolon
53"#,
54 );
55 }
56
57 #[test]
58 fn remove_semicolon() {
59 check_fix(r#"fn f() -> i32 { 92$0; }"#, r#"fn f() -> i32 { 92 }"#);
60 }
61}