Skip to content

Commit

Permalink
handle B029
Browse files Browse the repository at this point in the history
  • Loading branch information
smokyabdulrahman committed Dec 6, 2024
1 parent caa4c52 commit e57470a
Show file tree
Hide file tree
Showing 9 changed files with 60 additions and 18 deletions.
14 changes: 12 additions & 2 deletions crates/ruff_linter/resources/test/fixtures/flake8_bugbear/B029.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
"""
Should emit:
B029 - on lines 8 and 13
B029 - on lines 8, 13, 18 and 23
"""

try:
Expand All @@ -11,4 +11,14 @@
try:
pass
except () as e:
pass
pass

try:
pass
except* ():
pass

try:
pass
except* () as e:
pass
4 changes: 2 additions & 2 deletions crates/ruff_linter/src/checkers/ast/analyze/except_handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use crate::rules::{
};

/// Run lint rules over an [`ExceptHandler`] syntax node.
pub(crate) fn except_handler(except_handler: &ExceptHandler, checker: &mut Checker) {
pub(crate) fn except_handler(except_handler: &ExceptHandler, checker: &mut Checker, is_star: bool) {
match except_handler {
ExceptHandler::ExceptHandler(ast::ExceptHandlerExceptHandler {
type_,
Expand Down Expand Up @@ -60,7 +60,7 @@ pub(crate) fn except_handler(except_handler: &ExceptHandler, checker: &mut Check
);
}
if checker.enabled(Rule::ExceptWithEmptyTuple) {
flake8_bugbear::rules::except_with_empty_tuple(checker, except_handler);
flake8_bugbear::rules::except_with_empty_tuple(checker, except_handler, is_star);
}
if checker.enabled(Rule::ExceptWithNonExceptionClasses) {
flake8_bugbear::rules::except_with_non_exception_classes(checker, except_handler);
Expand Down
7 changes: 4 additions & 3 deletions crates/ruff_linter/src/checkers/ast/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -892,6 +892,7 @@ impl<'a> Visitor<'a> for Checker<'a> {
handlers,
orelse,
finalbody,
is_star,
..
},
) => {
Expand All @@ -908,7 +909,7 @@ impl<'a> Visitor<'a> for Checker<'a> {

for except_handler in handlers {
self.semantic.push_branch();
self.visit_except_handler(except_handler);
self.visit_except_handler(except_handler, *is_star);
self.semantic.pop_branch();
}

Expand Down Expand Up @@ -1564,7 +1565,7 @@ impl<'a> Visitor<'a> for Checker<'a> {
self.semantic.pop_node();
}

fn visit_except_handler(&mut self, except_handler: &'a ExceptHandler) {
fn visit_except_handler(&mut self, except_handler: &'a ExceptHandler, is_star: bool) {
let flags_snapshot = self.semantic.flags;
self.semantic.flags |= SemanticModelFlags::EXCEPTION_HANDLER;

Expand Down Expand Up @@ -1609,7 +1610,7 @@ impl<'a> Visitor<'a> for Checker<'a> {
}

// Step 4: Analysis
analyze::except_handler(except_handler, self);
analyze::except_handler(except_handler, self, is_star);

self.semantic.flags = flags_snapshot;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,18 +34,27 @@ use crate::checkers::ast::Checker;
/// ## References
/// - [Python documentation: `except` clause](https://docs.python.org/3/reference/compound_stmts.html#except-clause)
#[derive(ViolationMetadata)]
pub(crate) struct ExceptWithEmptyTuple;
pub(crate) struct ExceptWithEmptyTuple {
is_star: bool,
}

impl Violation for ExceptWithEmptyTuple {
#[derive_message_formats]
fn message(&self) -> String {
"Using `except ():` with an empty tuple does not catch anything; add exceptions to handle"
.to_string()
if self.is_star {
"Using `except* ():` with an empty tuple does not catch anything; add exceptions to handle".to_string()
} else {
"Using `except ():` with an empty tuple does not catch anything; add exceptions to handle".to_string()
}
}
}

/// B029
pub(crate) fn except_with_empty_tuple(checker: &mut Checker, except_handler: &ExceptHandler) {
pub(crate) fn except_with_empty_tuple(
checker: &mut Checker,
except_handler: &ExceptHandler,
is_star: bool,
) {
let ExceptHandler::ExceptHandler(ast::ExceptHandlerExceptHandler { type_, .. }) =
except_handler;
let Some(type_) = type_ else {
Expand All @@ -56,7 +65,7 @@ pub(crate) fn except_with_empty_tuple(checker: &mut Checker, except_handler: &Ex
};
if elts.is_empty() {
checker.diagnostics.push(Diagnostic::new(
ExceptWithEmptyTuple,
ExceptWithEmptyTuple { is_star },
except_handler.range(),
));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,26 @@ B029.py:13:1: B029 Using `except ():` with an empty tuple does not catch anythin
13 | / except () as e:
14 | | pass
| |________^ B029
15 |
16 | try:
|

B029.py:18:1: B029 Using `except* ():` with an empty tuple does not catch anything; add exceptions to handle
|
16 | try:
17 | pass
18 | / except* ():
19 | | pass
| |________^ B029
20 |
21 | try:
|

B029.py:23:1: B029 Using `except* ():` with an empty tuple does not catch anything; add exceptions to handle
|
21 | try:
22 | pass
23 | / except* () as e:
24 | | pass
| |________^ B029
|
Original file line number Diff line number Diff line change
Expand Up @@ -617,7 +617,7 @@ impl<'a> BodyVisitor<'a> {
}

impl<'a> Visitor<'a> for BodyVisitor<'a> {
fn visit_except_handler(&mut self, handler: &'a ast::ExceptHandler) {
fn visit_except_handler(&mut self, handler: &'a ast::ExceptHandler, _is_star: bool) {
let ast::ExceptHandler::ExceptHandler(handler_inner) = handler;
self.currently_suspended_exceptions = handler_inner.type_.as_deref();
visitor::walk_except_handler(self, handler);
Expand Down
2 changes: 1 addition & 1 deletion crates/ruff_linter/src/rules/tryceratops/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ impl<'b> Visitor<'b> for LoggerCandidateVisitor<'_, 'b> {
visitor::walk_expr(self, expr);
}

fn visit_except_handler(&mut self, _except_handler: &'b ExceptHandler) {
fn visit_except_handler(&mut self, _except_handler: &'b ExceptHandler, _is_star: bool) {
// Don't recurse into exception handlers, since we'll re-run the visitor on any such
// handlers.
}
Expand Down
6 changes: 3 additions & 3 deletions crates/ruff_python_ast/src/visitor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ pub trait Visitor<'a> {
fn visit_comprehension(&mut self, comprehension: &'a Comprehension) {
walk_comprehension(self, comprehension);
}
fn visit_except_handler(&mut self, except_handler: &'a ExceptHandler) {
fn visit_except_handler(&mut self, except_handler: &'a ExceptHandler, _is_star: bool) {
walk_except_handler(self, except_handler);
}
fn visit_arguments(&mut self, arguments: &'a Arguments) {
Expand Down Expand Up @@ -286,12 +286,12 @@ pub fn walk_stmt<'a, V: Visitor<'a> + ?Sized>(visitor: &mut V, stmt: &'a Stmt) {
handlers,
orelse,
finalbody,
is_star: _,
is_star,
range: _,
}) => {
visitor.visit_body(body);
for except_handler in handlers {
visitor.visit_except_handler(except_handler);
visitor.visit_except_handler(except_handler, *is_star);
}
visitor.visit_body(orelse);
visitor.visit_body(finalbody);
Expand Down
2 changes: 1 addition & 1 deletion crates/ruff_python_ast_integration_tests/tests/visitor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ impl Visitor<'_> for RecordVisitor {
self.exit_node();
}

fn visit_except_handler(&mut self, except_handler: &ExceptHandler) {
fn visit_except_handler(&mut self, except_handler: &ExceptHandler, _is_star: bool) {
self.enter_node(except_handler);
walk_except_handler(self, except_handler);
self.exit_node();
Expand Down

0 comments on commit e57470a

Please sign in to comment.