Extend B904 to else branches (#2886)

This commit is contained in:
Charlie Marsh 2023-02-13 22:58:15 -05:00 committed by GitHub
parent b8483975a4
commit 66a195f805
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 39 additions and 10 deletions

View File

@ -1,6 +1,6 @@
""" """
Should emit: Should emit:
B904 - on lines 10, 11 and 16 B904 - on lines 10, 11, 16, 62, and 64
""" """
try: try:
@ -53,3 +53,12 @@ except ImportError:
raise ValueError raise ValueError
except ValueError: except ValueError:
raise raise
try:
...
except Exception as e:
if ...:
raise RuntimeError("boom!")
else:
raise RuntimeError("bang!")

View File

@ -3,6 +3,7 @@ use ruff_python::string::is_lower;
use rustpython_parser::ast::{ExprKind, Stmt, StmtKind}; use rustpython_parser::ast::{ExprKind, Stmt, StmtKind};
use crate::ast::types::Range; use crate::ast::types::Range;
use crate::ast::visitor;
use crate::ast::visitor::Visitor; use crate::ast::visitor::Visitor;
use crate::checkers::ast::Checker; use crate::checkers::ast::Checker;
use crate::registry::Diagnostic; use crate::registry::Diagnostic;
@ -44,15 +45,16 @@ impl<'a> Visitor<'a> for RaiseVisitor {
| StmtKind::FunctionDef { .. } | StmtKind::FunctionDef { .. }
| StmtKind::AsyncFunctionDef { .. } | StmtKind::AsyncFunctionDef { .. }
| StmtKind::Try { .. } => {} | StmtKind::Try { .. } => {}
StmtKind::If { body, .. } StmtKind::If { body, orelse, .. } => {
| StmtKind::While { body, .. } visitor::walk_body(self, body);
visitor::walk_body(self, orelse);
}
StmtKind::While { body, .. }
| StmtKind::With { body, .. } | StmtKind::With { body, .. }
| StmtKind::AsyncWith { body, .. } | StmtKind::AsyncWith { body, .. }
| StmtKind::For { body, .. } | StmtKind::For { body, .. }
| StmtKind::AsyncFor { body, .. } => { | StmtKind::AsyncFor { body, .. } => {
for stmt in body { visitor::walk_body(self, body);
self.visit_stmt(stmt);
}
} }
_ => {} _ => {}
} }
@ -63,8 +65,6 @@ pub fn raise_without_from_inside_except(checker: &mut Checker, body: &[Stmt]) {
let mut visitor = RaiseVisitor { let mut visitor = RaiseVisitor {
diagnostics: vec![], diagnostics: vec![],
}; };
for stmt in body { visitor::walk_body(&mut visitor, body);
visitor.visit_stmt(stmt);
}
checker.diagnostics.extend(visitor.diagnostics); checker.diagnostics.extend(visitor.diagnostics);
} }

View File

@ -1,5 +1,5 @@
--- ---
source: src/rules/flake8_bugbear/mod.rs source: crates/ruff/src/rules/flake8_bugbear/mod.rs
expression: diagnostics expression: diagnostics
--- ---
- kind: - kind:
@ -32,4 +32,24 @@ expression: diagnostics
column: 39 column: 39
fix: ~ fix: ~
parent: ~ 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: ~