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

[tryceratops] Add BaseException to raise-vanilla-class rule (TRY002) #12620

Merged
merged 7 commits into from
Aug 5, 2024
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_linter/resources/test/fixtures/tryceratops/TRY002.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,13 @@ def anotherfunc():
a = 1
if a == 1:
raise exceptions.Exception("Another except") # That's fine


def yetanotherfunc():
a = 1
if a == 1:
raise BaseException("Custom message")

b = 1
if b == 1:
raise BaseException
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,17 @@ pub(crate) fn raise_vanilla_class(checker: &mut Checker, expr: &Expr) {
} else {
expr
};
if checker.semantic().match_builtin_expr(node, "Exception") {

if checker
.semantic()
.resolve_qualified_name(node)
.is_some_and(|qualified_name| {
matches!(
qualified_name.segments(),
["" | "builtins", "Exception" | "BaseException"]
)
})
{
checker
.diagnostics
.push(Diagnostic::new(RaiseVanillaClass, expr.range()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,22 @@ TRY002.py:17:15: TRY002 Create your own exception
| ^^^^^^^^^ TRY002
|

TRY002.py:37:15: TRY002 Create your own exception
|
35 | a = 1
36 | if a == 1:
37 | raise BaseException("Custom message")
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ TRY002
38 |
39 | b = 1
|

TRY002.py:41:15: TRY002 Create your own exception
|
39 | b = 1
40 | if b == 1:
41 | raise BaseException
| ^^^^^^^^^^^^^ TRY002
|


Loading