Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow non-verbose raise when cause is present #2816

Merged
merged 1 commit into from
Feb 12, 2023
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
10 changes: 10 additions & 0 deletions crates/ruff/resources/test/fixtures/tryceratops/TRY201.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ def good():
logger.exception("process failed")
raise


def still_good():
try:
process()
Expand All @@ -35,6 +36,14 @@ def still_good():
raise


def still_good_too():
try:
process()
except MyException as e:
print(e)
raise e from None


def still_actually_good():
try:
process()
Expand All @@ -60,5 +69,6 @@ def bad_that_needs_recursion_2():
except MyException as e:
logger.exception("process failed")
if True:

def foo():
raise e
23 changes: 14 additions & 9 deletions crates/ruff/src/rules/tryceratops/rules/verbose_raise.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ impl Violation for VerboseRaise {

#[derive(Default)]
struct RaiseStatementVisitor<'a> {
raises: Vec<Option<&'a Expr>>,
raises: Vec<(Option<&'a Expr>, Option<&'a Expr>)>,
}

impl<'a, 'b> Visitor<'b> for RaiseStatementVisitor<'a>
Expand All @@ -29,7 +29,7 @@ where
{
fn visit_stmt(&mut self, stmt: &'b Stmt) {
match &stmt.node {
StmtKind::Raise { exc, .. } => self.raises.push(exc.as_ref().map(|expr| &**expr)),
StmtKind::Raise { exc, cause } => self.raises.push((exc.as_deref(), cause.as_deref())),
StmtKind::Try {
body, finalbody, ..
} => {
Expand Down Expand Up @@ -59,13 +59,18 @@ pub fn verbose_raise(checker: &mut Checker, handlers: &[Excepthandler]) {
}
visitor.raises
};
for expr in raises.into_iter().flatten() {
// ...and the raised object is bound to the same name...
if let ExprKind::Name { id, .. } = &expr.node {
if id == exception_name {
checker
.diagnostics
.push(Diagnostic::new(VerboseRaise, Range::from_located(expr)));
for (exc, cause) in raises {
if cause.is_some() {
continue;
}
if let Some(exc) = exc {
// ...and the raised object is bound to the same name...
if let ExprKind::Name { id, .. } = &exc.node {
if id == exception_name {
checker
.diagnostics
.push(Diagnostic::new(VerboseRaise, Range::from_located(exc)));
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
source: src/rules/tryceratops/mod.rs
source: crates/ruff/src/rules/tryceratops/mod.rs
expression: diagnostics
---
- kind:
Expand All @@ -15,20 +15,20 @@ expression: diagnostics
- kind:
VerboseRaise: ~
location:
row: 54
row: 63
column: 18
end_location:
row: 54
row: 63
column: 19
fix: ~
parent: ~
- kind:
VerboseRaise: ~
location:
row: 64
row: 74
column: 22
end_location:
row: 64
row: 74
column: 23
fix: ~
parent: ~
Expand Down