diff --git a/crates/ruff/resources/test/fixtures/flake8_bugbear/B904.py b/crates/ruff/resources/test/fixtures/flake8_bugbear/B904.py index 9fa57c945c..f4615806e5 100644 --- a/crates/ruff/resources/test/fixtures/flake8_bugbear/B904.py +++ b/crates/ruff/resources/test/fixtures/flake8_bugbear/B904.py @@ -1,6 +1,6 @@ """ Should emit: -B904 - on lines 10, 11 and 16 +B904 - on lines 10, 11, 16, 62, and 64 """ try: @@ -53,3 +53,12 @@ except ImportError: raise ValueError except ValueError: raise + + +try: + ... +except Exception as e: + if ...: + raise RuntimeError("boom!") + else: + raise RuntimeError("bang!") diff --git a/crates/ruff/src/rules/flake8_bugbear/rules/raise_without_from_inside_except.rs b/crates/ruff/src/rules/flake8_bugbear/rules/raise_without_from_inside_except.rs index b2d75d6983..67cd5cee16 100644 --- a/crates/ruff/src/rules/flake8_bugbear/rules/raise_without_from_inside_except.rs +++ b/crates/ruff/src/rules/flake8_bugbear/rules/raise_without_from_inside_except.rs @@ -3,6 +3,7 @@ use ruff_python::string::is_lower; use rustpython_parser::ast::{ExprKind, Stmt, StmtKind}; use crate::ast::types::Range; +use crate::ast::visitor; use crate::ast::visitor::Visitor; use crate::checkers::ast::Checker; use crate::registry::Diagnostic; @@ -44,15 +45,16 @@ impl<'a> Visitor<'a> for RaiseVisitor { | StmtKind::FunctionDef { .. } | StmtKind::AsyncFunctionDef { .. } | StmtKind::Try { .. } => {} - StmtKind::If { body, .. } - | StmtKind::While { body, .. } + StmtKind::If { body, orelse, .. } => { + visitor::walk_body(self, body); + visitor::walk_body(self, orelse); + } + StmtKind::While { body, .. } | StmtKind::With { body, .. } | StmtKind::AsyncWith { body, .. } | StmtKind::For { body, .. } | StmtKind::AsyncFor { body, .. } => { - for stmt in body { - self.visit_stmt(stmt); - } + visitor::walk_body(self, body); } _ => {} } @@ -63,8 +65,6 @@ pub fn raise_without_from_inside_except(checker: &mut Checker, body: &[Stmt]) { let mut visitor = RaiseVisitor { diagnostics: vec![], }; - for stmt in body { - visitor.visit_stmt(stmt); - } + visitor::walk_body(&mut visitor, body); checker.diagnostics.extend(visitor.diagnostics); } diff --git a/crates/ruff/src/rules/flake8_bugbear/snapshots/ruff__rules__flake8_bugbear__tests__B904_B904.py.snap b/crates/ruff/src/rules/flake8_bugbear/snapshots/ruff__rules__flake8_bugbear__tests__B904_B904.py.snap index b2dbae3ce0..db1715b2a1 100644 --- a/crates/ruff/src/rules/flake8_bugbear/snapshots/ruff__rules__flake8_bugbear__tests__B904_B904.py.snap +++ b/crates/ruff/src/rules/flake8_bugbear/snapshots/ruff__rules__flake8_bugbear__tests__B904_B904.py.snap @@ -1,5 +1,5 @@ --- -source: src/rules/flake8_bugbear/mod.rs +source: crates/ruff/src/rules/flake8_bugbear/mod.rs expression: diagnostics --- - kind: @@ -32,4 +32,24 @@ expression: diagnostics column: 39 fix: ~ parent: ~ +- kind: + RaiseWithoutFromInsideExcept: ~ + location: + row: 62 + column: 8 + end_location: + row: 62 + column: 35 + fix: ~ + parent: ~ +- kind: + RaiseWithoutFromInsideExcept: ~ + location: + row: 64 + column: 8 + end_location: + row: 64 + column: 35 + fix: ~ + parent: ~