diff options
author | bors[bot] <26634292+bors[bot]@users.noreply.github.com> | 2021-06-14 17:46:25 +0100 |
---|---|---|
committer | GitHub <[email protected]> | 2021-06-14 17:46:25 +0100 |
commit | 38ae18b7592f97a7058d97928307bccbd881a582 (patch) | |
tree | 5f6f59f48f05999495654bf2e4250e029e6f010f /crates/ide_diagnostics/src/handlers/remove_this_semicolon.rs | |
parent | 401d79ac0674ec62689949c3a531836420cb9beb (diff) | |
parent | 4768e5fb23c058eba90f0a1dcd6e9d5c0ecdee1b (diff) |
Merge #9272
9272: internal: move diagnostics to a dedicated crate r=matklad a=matklad
bors r+
Co-authored-by: Aleksey Kladov <[email protected]>
Diffstat (limited to 'crates/ide_diagnostics/src/handlers/remove_this_semicolon.rs')
-rw-r--r-- | crates/ide_diagnostics/src/handlers/remove_this_semicolon.rs | 61 |
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..b52e4dc84 --- /dev/null +++ b/crates/ide_diagnostics/src/handlers/remove_this_semicolon.rs | |||
@@ -0,0 +1,61 @@ | |||
1 | use hir::db::AstDatabase; | ||
2 | use ide_db::source_change::SourceChange; | ||
3 | use syntax::{ast, AstNode}; | ||
4 | use text_edit::TextEdit; | ||
5 | |||
6 | use 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. | ||
11 | pub(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 | |||
23 | fn 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)] | ||
44 | mod tests { | ||
45 | use crate::tests::{check_diagnostics, check_fix}; | ||
46 | |||
47 | #[test] | ||
48 | fn missing_semicolon() { | ||
49 | check_diagnostics( | ||
50 | r#" | ||
51 | fn test() -> i32 { 123; } | ||
52 | //^^^ 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 | } | ||