Skip to content

Commit

Permalink
Move except-handler flag into visit_except_handler (#5796)
Browse files Browse the repository at this point in the history
## Summary

This is more similar to how these flags work in other contexts (e.g.,
`visit_annotation`), and also ensures that we unset it prior to visit
the `orelse` and `finalbody` (a subtle bug).
  • Loading branch information
charliermarsh authored Jul 16, 2023
1 parent c7ff743 commit 59dfd0e
Showing 1 changed file with 13 additions and 11 deletions.
24 changes: 13 additions & 11 deletions crates/ruff/src/checkers/ast/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1558,6 +1558,14 @@ where
finalbody,
range: _,
}) => {
if self.enabled(Rule::JumpStatementInFinally) {
flake8_bugbear::rules::jump_statement_in_finally(self, finalbody);
}
if self.enabled(Rule::ContinueInFinally) {
if self.settings.target_version <= PythonVersion::Py38 {
pylint::rules::continue_in_finally(self, finalbody);
}
}
if self.enabled(Rule::DefaultExceptNotLast) {
if let Some(diagnostic) =
pyflakes::rules::default_except_not_last(handlers, self.locator)
Expand Down Expand Up @@ -1944,20 +1952,9 @@ where
}

self.semantic.handled_exceptions.push(handled_exceptions);

if self.enabled(Rule::JumpStatementInFinally) {
flake8_bugbear::rules::jump_statement_in_finally(self, finalbody);
}
if self.enabled(Rule::ContinueInFinally) {
if self.settings.target_version <= PythonVersion::Py38 {
pylint::rules::continue_in_finally(self, finalbody);
}
}

self.visit_body(body);
self.semantic.handled_exceptions.pop();

self.semantic.flags |= SemanticModelFlags::EXCEPTION_HANDLER;
for except_handler in handlers {
self.visit_except_handler(except_handler);
}
Expand Down Expand Up @@ -3873,6 +3870,9 @@ where
}

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

match except_handler {
ExceptHandler::ExceptHandler(ast::ExceptHandlerExceptHandler {
type_,
Expand Down Expand Up @@ -3992,6 +3992,8 @@ where
}
}
}

self.semantic.flags = flags_snapshot;
}

fn visit_format_spec(&mut self, format_spec: &'b Expr) {
Expand Down

0 comments on commit 59dfd0e

Please sign in to comment.