Skip to content

Commit

Permalink
Extend B904 to else branches (#2886)
Browse files Browse the repository at this point in the history
  • Loading branch information
charliermarsh committed Feb 14, 2023
1 parent b848397 commit 66a195f
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 10 deletions.
11 changes: 10 additions & 1 deletion crates/ruff/resources/test/fixtures/flake8_bugbear/B904.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
"""
Should emit:
B904 - on lines 10, 11 and 16
B904 - on lines 10, 11, 16, 62, and 64
"""

try:
Expand Down Expand Up @@ -53,3 +53,12 @@ def context_switch():
raise ValueError
except ValueError:
raise


try:
...
except Exception as e:
if ...:
raise RuntimeError("boom!")
else:
raise RuntimeError("bang!")
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
_ => {}
}
Expand All @@ -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);
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
source: src/rules/flake8_bugbear/mod.rs
source: crates/ruff/src/rules/flake8_bugbear/mod.rs
expression: diagnostics
---
- kind:
Expand Down Expand Up @@ -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: ~

0 comments on commit 66a195f

Please sign in to comment.