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/hir_ty/src/diagnostics/expr.rs | 27 ++++++++++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) (limited to 'crates/hir_ty/src/diagnostics/expr.rs') diff --git a/crates/hir_ty/src/diagnostics/expr.rs b/crates/hir_ty/src/diagnostics/expr.rs index 434b19354..313422968 100644 --- a/crates/hir_ty/src/diagnostics/expr.rs +++ b/crates/hir_ty/src/diagnostics/expr.rs @@ -2,7 +2,7 @@ use std::sync::Arc; -use hir_def::{path::path, resolver::HasResolver, AdtId, DefWithBodyId}; +use hir_def::{AdtId, DefWithBodyId, expr::Statement, path::path, resolver::HasResolver}; use hir_expand::diagnostics::DiagnosticSink; use rustc_hash::FxHashSet; use syntax::{ast, AstPtr}; @@ -23,6 +23,8 @@ pub(crate) use hir_def::{ LocalFieldId, VariantId, }; +use super::RemoveThisSemicolon; + pub(super) struct ExprValidator<'a, 'b: 'a> { owner: DefWithBodyId, infer: Arc, @@ -78,6 +80,12 @@ impl<'a, 'b> ExprValidator<'a, 'b> { let body_expr = &body[body.body_expr]; if let Expr::Block { tail: Some(t), .. } = body_expr { self.validate_results_in_tail_expr(body.body_expr, *t, db); + } else { + if let Expr::Block { statements, .. } = body_expr { + if let Some(Statement::Expr(id)) = statements.last() { + self.validate_missing_tail_expr(body.body_expr, *id, db); + } + } } } @@ -317,6 +325,23 @@ impl<'a, 'b> ExprValidator<'a, 'b> { } } } + + fn validate_missing_tail_expr(&mut self, body_id: ExprId, possible_tail_id: ExprId, db: &dyn HirDatabase) { + let mismatch = match self.infer.type_mismatch_for_expr(body_id) { + Some(m) => m, + None => return, + }; + + if let Some(possible_tail_ty) = self.infer.type_of_expr.get(possible_tail_id) { + if mismatch.actual == Ty::unit() && mismatch.expected == *possible_tail_ty { + let (_, source_map) = db.body_with_source_map(self.owner.into()); + + if let Ok(source_ptr) = source_map.expr_syntax(possible_tail_id) { + self.sink.push(RemoveThisSemicolon { file: source_ptr.file_id, expr: source_ptr.value }); + } + } + } + } } pub fn record_literal_missing_fields( -- cgit v1.2.3 From 581567a4c8f418588bab5dbc760076c660111f81 Mon Sep 17 00:00:00 2001 From: ivan770 Date: Tue, 8 Dec 2020 20:50:13 +0200 Subject: Remove use via super --- crates/hir_ty/src/diagnostics/expr.rs | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) (limited to 'crates/hir_ty/src/diagnostics/expr.rs') diff --git a/crates/hir_ty/src/diagnostics/expr.rs b/crates/hir_ty/src/diagnostics/expr.rs index 313422968..6ec15c180 100644 --- a/crates/hir_ty/src/diagnostics/expr.rs +++ b/crates/hir_ty/src/diagnostics/expr.rs @@ -11,7 +11,7 @@ use crate::{ db::HirDatabase, diagnostics::{ match_check::{is_useful, MatchCheckCtx, Matrix, PatStack, Usefulness}, - MismatchedArgCount, MissingFields, MissingMatchArms, MissingOkInTailExpr, MissingPatFields, + MismatchedArgCount, MissingFields, MissingMatchArms, MissingOkInTailExpr, MissingPatFields, RemoveThisSemicolon }, utils::variant_data, ApplicationTy, InferenceResult, Ty, TypeCtor, @@ -23,8 +23,6 @@ pub(crate) use hir_def::{ LocalFieldId, VariantId, }; -use super::RemoveThisSemicolon; - pub(super) struct ExprValidator<'a, 'b: 'a> { owner: DefWithBodyId, infer: Arc, -- cgit v1.2.3 From 7738467e0a11c9878d9e9486daeb0dc18d93b8e8 Mon Sep 17 00:00:00 2001 From: ivan770 Date: Tue, 8 Dec 2020 19:25:21 +0000 Subject: Format code --- crates/hir_ty/src/diagnostics/expr.rs | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) (limited to 'crates/hir_ty/src/diagnostics/expr.rs') diff --git a/crates/hir_ty/src/diagnostics/expr.rs b/crates/hir_ty/src/diagnostics/expr.rs index 6ec15c180..98b3cade2 100644 --- a/crates/hir_ty/src/diagnostics/expr.rs +++ b/crates/hir_ty/src/diagnostics/expr.rs @@ -2,7 +2,7 @@ use std::sync::Arc; -use hir_def::{AdtId, DefWithBodyId, expr::Statement, path::path, resolver::HasResolver}; +use hir_def::{expr::Statement, path::path, resolver::HasResolver, AdtId, DefWithBodyId}; use hir_expand::diagnostics::DiagnosticSink; use rustc_hash::FxHashSet; use syntax::{ast, AstPtr}; @@ -11,7 +11,8 @@ use crate::{ db::HirDatabase, diagnostics::{ match_check::{is_useful, MatchCheckCtx, Matrix, PatStack, Usefulness}, - MismatchedArgCount, MissingFields, MissingMatchArms, MissingOkInTailExpr, MissingPatFields, RemoveThisSemicolon + MismatchedArgCount, MissingFields, MissingMatchArms, MissingOkInTailExpr, MissingPatFields, + RemoveThisSemicolon, }, utils::variant_data, ApplicationTy, InferenceResult, Ty, TypeCtor, @@ -324,7 +325,12 @@ impl<'a, 'b> ExprValidator<'a, 'b> { } } - fn validate_missing_tail_expr(&mut self, body_id: ExprId, possible_tail_id: ExprId, db: &dyn HirDatabase) { + fn validate_missing_tail_expr( + &mut self, + body_id: ExprId, + possible_tail_id: ExprId, + db: &dyn HirDatabase, + ) { let mismatch = match self.infer.type_mismatch_for_expr(body_id) { Some(m) => m, None => return, @@ -335,7 +341,10 @@ impl<'a, 'b> ExprValidator<'a, 'b> { let (_, source_map) = db.body_with_source_map(self.owner.into()); if let Ok(source_ptr) = source_map.expr_syntax(possible_tail_id) { - self.sink.push(RemoveThisSemicolon { file: source_ptr.file_id, expr: source_ptr.value }); + self.sink.push(RemoveThisSemicolon { + file: source_ptr.file_id, + expr: source_ptr.value, + }); } } } -- cgit v1.2.3 From 86c183716cc6dfd44da15c86a7726d0a117c214e Mon Sep 17 00:00:00 2001 From: ivan770 Date: Wed, 9 Dec 2020 10:17:28 +0200 Subject: Apply suggestions from code review Co-authored-by: bjorn3 --- crates/hir_ty/src/diagnostics/expr.rs | 37 +++++++++++++++++++---------------- 1 file changed, 20 insertions(+), 17 deletions(-) (limited to 'crates/hir_ty/src/diagnostics/expr.rs') diff --git a/crates/hir_ty/src/diagnostics/expr.rs b/crates/hir_ty/src/diagnostics/expr.rs index 98b3cade2..27ffcf291 100644 --- a/crates/hir_ty/src/diagnostics/expr.rs +++ b/crates/hir_ty/src/diagnostics/expr.rs @@ -77,13 +77,11 @@ impl<'a, 'b> ExprValidator<'a, 'b> { } } let body_expr = &body[body.body_expr]; - if let Expr::Block { tail: Some(t), .. } = body_expr { - self.validate_results_in_tail_expr(body.body_expr, *t, db); - } else { - if let Expr::Block { statements, .. } = body_expr { - if let Some(Statement::Expr(id)) = statements.last() { - self.validate_missing_tail_expr(body.body_expr, *id, db); - } + if let Expr::Block { statements, tail, .. } = body_expr { + if let Some(t) = tail { + self.validate_results_in_tail_expr(body.body_expr, *t, db); + } else if let Some(Statement::Expr(id)) = statements.last() { + self.validate_missing_tail_expr(body.body_expr, *id, db); } } } @@ -336,17 +334,22 @@ impl<'a, 'b> ExprValidator<'a, 'b> { None => return, }; - if let Some(possible_tail_ty) = self.infer.type_of_expr.get(possible_tail_id) { - if mismatch.actual == Ty::unit() && mismatch.expected == *possible_tail_ty { - let (_, source_map) = db.body_with_source_map(self.owner.into()); + let possible_tail_ty = if let Some(possible_tail_ty) = self.infer.type_of_expr.get(possible_tail_id) { + possible_tail_ty + } else { + return; + }; - if let Ok(source_ptr) = source_map.expr_syntax(possible_tail_id) { - self.sink.push(RemoveThisSemicolon { - file: source_ptr.file_id, - expr: source_ptr.value, - }); - } - } + if mismatch.actual != Ty::unit() || mismatch.expected != *possible_tail_ty { + return; + } + + let (_, source_map) = db.body_with_source_map(self.owner.into()); + if let Ok(source_ptr) = source_map.expr_syntax(possible_tail_id) { + self.sink.push(RemoveThisSemicolon { + file: source_ptr.file_id, + expr: source_ptr.value, + }); } } } -- cgit v1.2.3 From 35006eba79b9a445c4ebec66f1a93b3170398e0e Mon Sep 17 00:00:00 2001 From: ivan770 Date: Wed, 9 Dec 2020 08:22:13 +0000 Subject: Apply rustfmt changes --- crates/hir_ty/src/diagnostics/expr.rs | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) (limited to 'crates/hir_ty/src/diagnostics/expr.rs') diff --git a/crates/hir_ty/src/diagnostics/expr.rs b/crates/hir_ty/src/diagnostics/expr.rs index 27ffcf291..9e461e0b0 100644 --- a/crates/hir_ty/src/diagnostics/expr.rs +++ b/crates/hir_ty/src/diagnostics/expr.rs @@ -334,11 +334,12 @@ impl<'a, 'b> ExprValidator<'a, 'b> { None => return, }; - let possible_tail_ty = if let Some(possible_tail_ty) = self.infer.type_of_expr.get(possible_tail_id) { - possible_tail_ty - } else { - return; - }; + let possible_tail_ty = + if let Some(possible_tail_ty) = self.infer.type_of_expr.get(possible_tail_id) { + possible_tail_ty + } else { + return; + }; if mismatch.actual != Ty::unit() || mismatch.expected != *possible_tail_ty { return; @@ -346,10 +347,8 @@ impl<'a, 'b> ExprValidator<'a, 'b> { let (_, source_map) = db.body_with_source_map(self.owner.into()); if let Ok(source_ptr) = source_map.expr_syntax(possible_tail_id) { - self.sink.push(RemoveThisSemicolon { - file: source_ptr.file_id, - expr: source_ptr.value, - }); + self.sink + .push(RemoveThisSemicolon { file: source_ptr.file_id, expr: source_ptr.value }); } } } -- cgit v1.2.3 From bbb0bc7b041278480edbfaa7c3cdadc5a704fc03 Mon Sep 17 00:00:00 2001 From: ivan770 Date: Thu, 10 Dec 2020 18:10:39 +0200 Subject: Cast to ExprStmt, style fixes --- crates/hir_ty/src/diagnostics/expr.rs | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) (limited to 'crates/hir_ty/src/diagnostics/expr.rs') diff --git a/crates/hir_ty/src/diagnostics/expr.rs b/crates/hir_ty/src/diagnostics/expr.rs index 9e461e0b0..849415706 100644 --- a/crates/hir_ty/src/diagnostics/expr.rs +++ b/crates/hir_ty/src/diagnostics/expr.rs @@ -334,18 +334,17 @@ impl<'a, 'b> ExprValidator<'a, 'b> { None => return, }; - let possible_tail_ty = - if let Some(possible_tail_ty) = self.infer.type_of_expr.get(possible_tail_id) { - possible_tail_ty - } else { - return; - }; + let possible_tail_ty = match self.infer.type_of_expr.get(possible_tail_id) { + Some(ty) => ty, + None => return, + }; if mismatch.actual != Ty::unit() || mismatch.expected != *possible_tail_ty { return; } let (_, source_map) = db.body_with_source_map(self.owner.into()); + if let Ok(source_ptr) = source_map.expr_syntax(possible_tail_id) { self.sink .push(RemoveThisSemicolon { file: source_ptr.file_id, expr: source_ptr.value }); -- cgit v1.2.3