refactor: remove body parameter for "unnecessary else" diagnostic

This commit is contained in:
davidsemakula 2024-02-19 15:35:47 +03:00
parent 7dfeb2cdcc
commit f2218e7278

View file

@ -109,7 +109,7 @@ impl ExprValidator {
self.check_for_trailing_return(*body_expr, &body); self.check_for_trailing_return(*body_expr, &body);
} }
Expr::If { .. } => { Expr::If { .. } => {
self.check_for_unnecessary_else(id, expr, &body, db); self.check_for_unnecessary_else(id, expr, db);
} }
Expr::Block { .. } => { Expr::Block { .. } => {
self.validate_block(db, expr); self.validate_block(db, expr);
@ -337,18 +337,12 @@ impl ExprValidator {
} }
} }
fn check_for_unnecessary_else( fn check_for_unnecessary_else(&mut self, id: ExprId, expr: &Expr, db: &dyn HirDatabase) {
&mut self,
id: ExprId,
expr: &Expr,
body: &Body,
db: &dyn HirDatabase,
) {
if let Expr::If { condition: _, then_branch, else_branch } = expr { if let Expr::If { condition: _, then_branch, else_branch } = expr {
if else_branch.is_none() { if else_branch.is_none() {
return; return;
} }
if let Expr::Block { statements, tail, .. } = &body.exprs[*then_branch] { if let Expr::Block { statements, tail, .. } = &self.body.exprs[*then_branch] {
let last_then_expr = tail.or_else(|| match statements.last()? { let last_then_expr = tail.or_else(|| match statements.last()? {
Statement::Expr { expr, .. } => Some(*expr), Statement::Expr { expr, .. } => Some(*expr),
_ => None, _ => None,