Skip to content

Commit

Permalink
check the else block contains local bindings
Browse files Browse the repository at this point in the history
  • Loading branch information
lapla-cogito committed Jan 8, 2025
1 parent 8ade7a6 commit f397659
Showing 1 changed file with 13 additions and 2 deletions.
15 changes: 13 additions & 2 deletions clippy_lints/src/redundant_else.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use clippy_utils::diagnostics::span_lint_and_sugg;
use clippy_utils::diagnostics::span_lint_and_then;
use clippy_utils::source::{indent_of, reindent_multiline, snippet};
use rustc_ast::ast::{Block, Expr, ExprKind, Stmt, StmtKind};
use rustc_ast::visit::{Visitor, walk_expr};
Expand Down Expand Up @@ -82,14 +82,25 @@ impl EarlyLintPass for RedundantElse {
}
}

let mut app = Applicability::MachineApplicable;
if let ExprKind::Block(block, _) = &els.kind {
for stmt in &block.stmts {
// If the `else` block contains a local binding, Clippy shouldn't auto-fix it
if let StmtKind::Let(_) = &stmt.kind {
app = Applicability::Unspecified;
break;
}
}
}

span_lint_and_sugg(
cx,
REDUNDANT_ELSE,
els.span.with_lo(then.span.hi()),
"redundant else block",
"remove the `else` block and move the contents out",
make_sugg(cx, els.span, "..", Some(expr.span)).to_string(),
Applicability::MachineApplicable,
app,
);
}
}
Expand Down

0 comments on commit f397659

Please sign in to comment.