Skip to content

Commit

Permalink
Simplify while and for handling
Browse files Browse the repository at this point in the history
  • Loading branch information
MichaReiser authored and InSyncWithFoo committed Jan 5, 2025
1 parent 9ad4bc3 commit 0e35d80
Showing 1 changed file with 10 additions and 29 deletions.
39 changes: 10 additions & 29 deletions crates/ruff_linter/src/rules/ruff/rules/needless_else.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use ruff_diagnostics::{AlwaysFixableViolation, Diagnostic, Edit, Fix};
use ruff_macros::{derive_message_formats, ViolationMetadata};
use ruff_python_ast::identifier::{else_loop, else_try};
use ruff_python_ast::{ExceptHandler, Expr, Stmt, StmtExpr};
use ruff_python_ast::{ExceptHandler, Expr, Stmt, StmtExpr, StmtFor, StmtWhile};
use ruff_source_file::LineRanges;
use ruff_text_size::{Ranged, TextRange};

Expand Down Expand Up @@ -42,47 +42,28 @@ impl AlwaysFixableViolation for NeedlessElse {

/// RUF047
pub(crate) fn needless_else(checker: &mut Checker, stmt: &Stmt) {
let (comment_ranges, source) = (checker.comment_ranges(), checker.source());
let comment_ranges = checker.comment_ranges();
let source = checker.source();

let (body, report_range, remove_range, comment_range) = match stmt {
Stmt::For(for_stmt) => {
let (previous, else_body) = (&for_stmt.body, &for_stmt.orelse[..]);
let (body, diagnostic_range, remove_range, comment_range) = match stmt {
Stmt::For(StmtFor { body, orelse, .. }) | Stmt::While(StmtWhile { body, orelse, .. }) => {
let (previous, else_body) = (&body, &orelse[..]);

let Some(keyword_range) = else_loop(stmt, source) else {
return;
};
let Some(last_stmt) = else_body.last() else {
return;
};
let report_range = TextRange::new(keyword_range.start(), last_stmt.end());

let Some((remove_range, comment_range)) =
remove_and_comment_range(source, previous, else_body)
else {
return;
};

(else_body, report_range, remove_range, comment_range)
}

Stmt::While(while_stmt) => {
let (previous, else_body) = (&while_stmt.body, &while_stmt.orelse[..]);

let Some(keyword_range) = else_loop(stmt, source) else {
return;
};
let Some(last_stmt) = else_body.last() else {
return;
};
let report_range = TextRange::new(keyword_range.start(), last_stmt.end());
let diagnostic_range = TextRange::new(keyword_range.start(), last_stmt.end());

let Some((remove_range, comment_range)) =
remove_and_comment_range(source, previous, else_body)
else {
return;
};

(else_body, report_range, remove_range, comment_range)
(else_body, diagnostic_range, remove_range, comment_range)
}

Stmt::Try(try_stmt) => {
Expand All @@ -98,7 +79,7 @@ pub(crate) fn needless_else(checker: &mut Checker, stmt: &Stmt) {

let mut previous = &try_stmt.body[..];

if let [.., ExceptHandler::ExceptHandler(last_handler)] = &&try_stmt.handlers[..] {
if let [.., ExceptHandler::ExceptHandler(last_handler)] = &try_stmt.handlers[..] {
previous = &last_handler.body[..];
};

Expand Down Expand Up @@ -145,7 +126,7 @@ pub(crate) fn needless_else(checker: &mut Checker, stmt: &Stmt) {
let edit = Edit::range_deletion(remove_range);
let fix = Fix::safe_edit(edit);

let diagnostic = Diagnostic::new(NeedlessElse, report_range);
let diagnostic = Diagnostic::new(NeedlessElse, diagnostic_range);

checker.diagnostics.push(diagnostic.with_fix(fix));
}
Expand Down

0 comments on commit 0e35d80

Please sign in to comment.