Skip to content

Commit cb66bb8

Browse files
committed
Remove this semicolon
1 parent e2e6b70 commit cb66bb8

File tree

5 files changed

+74
-8
lines changed

5 files changed

+74
-8
lines changed

crates/hir/src/diagnostics.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,5 @@ pub use hir_expand::diagnostics::{
55
};
66
pub use hir_ty::diagnostics::{
77
IncorrectCase, MismatchedArgCount, MissingFields, MissingMatchArms, MissingOkInTailExpr,
8-
NoSuchField,
8+
NoSuchField, RemoveThisSemicolon
99
};

crates/hir_ty/src/diagnostics.rs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,30 @@ impl Diagnostic for MissingOkInTailExpr {
216216
}
217217
}
218218

219+
#[derive(Debug)]
220+
pub struct RemoveThisSemicolon {
221+
pub file: HirFileId,
222+
pub expr: AstPtr<ast::Expr>,
223+
}
224+
225+
impl Diagnostic for RemoveThisSemicolon {
226+
fn code(&self) -> DiagnosticCode {
227+
DiagnosticCode("remove-this-semicolon")
228+
}
229+
230+
fn message(&self) -> String {
231+
"Remove this semicolon".to_string()
232+
}
233+
234+
fn display_source(&self) -> InFile<SyntaxNodePtr> {
235+
InFile { file_id: self.file, value: self.expr.clone().into() }
236+
}
237+
238+
fn as_any(&self) -> &(dyn Any + Send + 'static) {
239+
self
240+
}
241+
}
242+
219243
// Diagnostic: break-outside-of-loop
220244
//
221245
// This diagnostic is triggered if `break` keyword is used outside of a loop.

crates/hir_ty/src/diagnostics/expr.rs

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
33
use std::sync::Arc;
44

5-
use hir_def::{path::path, resolver::HasResolver, AdtId, DefWithBodyId};
5+
use hir_def::{AdtId, DefWithBodyId, expr::Statement, path::path, resolver::HasResolver};
66
use hir_expand::diagnostics::DiagnosticSink;
77
use rustc_hash::FxHashSet;
88
use syntax::{ast, AstPtr};
@@ -23,6 +23,8 @@ pub(crate) use hir_def::{
2323
LocalFieldId, VariantId,
2424
};
2525

26+
use super::RemoveThisSemicolon;
27+
2628
pub(super) struct ExprValidator<'a, 'b: 'a> {
2729
owner: DefWithBodyId,
2830
infer: Arc<InferenceResult>,
@@ -78,6 +80,12 @@ impl<'a, 'b> ExprValidator<'a, 'b> {
7880
let body_expr = &body[body.body_expr];
7981
if let Expr::Block { tail: Some(t), .. } = body_expr {
8082
self.validate_results_in_tail_expr(body.body_expr, *t, db);
83+
} else {
84+
if let Expr::Block { statements, .. } = body_expr {
85+
if let Some(Statement::Expr(id)) = statements.last() {
86+
self.validate_missing_tail_expr(body.body_expr, *id, db);
87+
}
88+
}
8189
}
8290
}
8391

@@ -317,6 +325,23 @@ impl<'a, 'b> ExprValidator<'a, 'b> {
317325
}
318326
}
319327
}
328+
329+
fn validate_missing_tail_expr(&mut self, body_id: ExprId, possible_tail_id: ExprId, db: &dyn HirDatabase) {
330+
let mismatch = match self.infer.type_mismatch_for_expr(body_id) {
331+
Some(m) => m,
332+
None => return,
333+
};
334+
335+
if let Some(possible_tail_ty) = self.infer.type_of_expr.get(possible_tail_id) {
336+
if mismatch.actual == Ty::unit() && mismatch.expected == *possible_tail_ty {
337+
let (_, source_map) = db.body_with_source_map(self.owner.into());
338+
339+
if let Ok(source_ptr) = source_map.expr_syntax(possible_tail_id) {
340+
self.sink.push(RemoveThisSemicolon { file: source_ptr.file_id, expr: source_ptr.value });
341+
}
342+
}
343+
}
344+
}
320345
}
321346

322347
pub fn record_literal_missing_fields(

crates/ide/src/diagnostics.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,9 @@ pub(crate) fn diagnostics(
131131
.on::<hir::diagnostics::NoSuchField, _>(|d| {
132132
res.borrow_mut().push(diagnostic_with_fix(d, &sema));
133133
})
134+
.on::<hir::diagnostics::RemoveThisSemicolon, _>(|d| {
135+
res.borrow_mut().push(diagnostic_with_fix(d, &sema));
136+
})
134137
.on::<hir::diagnostics::IncorrectCase, _>(|d| {
135138
res.borrow_mut().push(warning_with_fix(d, &sema));
136139
})

crates/ide/src/diagnostics/fixes.rs

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use hir::{
44
db::AstDatabase,
55
diagnostics::{
66
Diagnostic, IncorrectCase, MissingFields, MissingOkInTailExpr, NoSuchField,
7-
UnresolvedModule,
7+
RemoveThisSemicolon, UnresolvedModule,
88
},
99
HasSource, HirDisplay, Semantics, VariantDef,
1010
};
@@ -13,11 +13,7 @@ use ide_db::{
1313
source_change::{FileSystemEdit, SourceFileEdit},
1414
RootDatabase,
1515
};
16-
use syntax::{
17-
algo,
18-
ast::{self, edit::IndentLevel, make},
19-
AstNode,
20-
};
16+
use syntax::{AstNode, Direction, T, algo, ast::{self, ExprStmt, edit::IndentLevel, make}};
2117
use text_edit::TextEdit;
2218

2319
use crate::{diagnostics::Fix, references::rename::rename_with_semantics, FilePosition};
@@ -102,6 +98,24 @@ impl DiagnosticWithFix for MissingOkInTailExpr {
10298
}
10399
}
104100

101+
impl DiagnosticWithFix for RemoveThisSemicolon {
102+
fn fix(&self, sema: &Semantics<RootDatabase>) -> Option<Fix> {
103+
let root = sema.db.parse_or_expand(self.file)?;
104+
105+
let semicolon = self.expr.to_node(&root)
106+
.syntax()
107+
.siblings_with_tokens(Direction::Next)
108+
.filter_map(|it| it.into_token())
109+
.find(|it| it.kind() == T![;])?
110+
.text_range();
111+
112+
let edit = TextEdit::delete(semicolon);
113+
let source_change = SourceFileEdit { file_id: self.file.original_file(sema.db), edit }.into();
114+
115+
Some(Fix::new("Remove this semicolon", source_change, semicolon))
116+
}
117+
}
118+
105119
impl DiagnosticWithFix for IncorrectCase {
106120
fn fix(&self, sema: &Semantics<RootDatabase>) -> Option<Fix> {
107121
let root = sema.db.parse_or_expand(self.file)?;

0 commit comments

Comments
 (0)