Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions crates/ruff/resources/test/fixtures/perflint/PERF203.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,29 @@
print("error")

i += 1

# OK - no other way to write this
for i in range(10):
try:
print(f"{i}")
break
except:
print("error")

# OK - no other way to write this
for i in range(10):
try:
print(f"{i}")
continue
except:
print("error")


# OK - no other way to write this
for i in range(10):
try:
print(f"{i}")
if i > 0:
break
except:
print("error")
37 changes: 34 additions & 3 deletions crates/ruff/src/rules/perflint/rules/try_except_in_loop.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use ruff_python_ast::{self as ast, Ranged, Stmt};

use ruff_diagnostics::{Diagnostic, Violation};
use ruff_macros::{derive_message_formats, violation};
use ruff_python_ast::statement_visitor::{walk_stmt, StatementVisitor};
use ruff_python_ast::{self as ast, Ranged, Stmt};

use crate::checkers::ast::Checker;
use crate::settings::types::PythonVersion;
Expand Down Expand Up @@ -35,6 +35,7 @@ use crate::settings::types::PythonVersion;
/// int_numbers.append(int(num))
/// except ValueError as e:
/// print(f"Couldn't convert to integer: {e}")
/// break
/// ```
///
/// Use instead:
Expand Down Expand Up @@ -67,15 +68,45 @@ pub(crate) fn try_except_in_loop(checker: &mut Checker, body: &[Stmt]) {
return;
}

let [Stmt::Try(ast::StmtTry { handlers, .. })] = body else {
let [Stmt::Try(ast::StmtTry { handlers, body, .. })] = body else {
return;
};

let Some(handler) = handlers.first() else {
return;
};

// Avoid flagging `try`-`except` blocks that contain `break` or `continue`,
// which rely on the exception handling mechanism.
if has_break_or_continue(body) {
return;
}

checker
.diagnostics
.push(Diagnostic::new(TryExceptInLoop, handler.range()));
}

/// Returns `true` if a `break` or `continue` statement is present in `body`.
fn has_break_or_continue(body: &[Stmt]) -> bool {
let mut visitor = LoopControlFlowVisitor::default();
visitor.visit_body(body);
visitor.has_break_or_continue
}

#[derive(Debug, Default)]
struct LoopControlFlowVisitor {
has_break_or_continue: bool,
}

impl StatementVisitor<'_> for LoopControlFlowVisitor {
fn visit_stmt(&mut self, stmt: &Stmt) {
match stmt {
Stmt::Break(_) | Stmt::Continue(_) => self.has_break_or_continue = true,
Stmt::FunctionDef(_) | Stmt::ClassDef(_) => {
// Don't recurse.
}
_ => walk_stmt(self, stmt),
}
}
}