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

Avoid false-positives for parens-on-raise with future.exception() #10206

Merged
merged 1 commit into from
Mar 3, 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
12 changes: 12 additions & 0 deletions crates/ruff_linter/resources/test/fixtures/flake8_raise/RSE102.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,3 +93,15 @@ def func():

# OK
raise func()


# OK
future = executor.submit(float, "a")
if future.exception():
raise future.exception()


# RSE102
future = executor.submit(float, "a")
if future.exception():
raise future.Exception()
Original file line number Diff line number Diff line change
Expand Up @@ -76,15 +76,34 @@ pub(crate) fn unnecessary_paren_on_raise_exception(checker: &mut Checker, expr:
None
};

// `ctypes.WinError()` is a function, not a class. It's part of the standard library, so
// we might as well get it right.
if exception_type.is_none()
&& checker
if exception_type.is_none() {
// If the method name doesn't _look_ like a class (i.e., it's lowercase), it's
// probably a function call, not a class.
let identifier = match func.as_ref() {
Expr::Name(ast::ExprName { id, .. }) => Some(id.as_str()),
Expr::Attribute(ast::ExprAttribute { attr, .. }) => Some(attr.as_str()),
_ => None,
};
if identifier.is_some_and(|identifier| {
identifier
.strip_prefix('_')
.unwrap_or(identifier)
.chars()
.next()
.is_some_and(char::is_lowercase)
}) {
return;
}

// `ctypes.WinError()` is a function, not a class. It's part of the standard library, so
// we might as well get it right.
if checker
.semantic()
.resolve_call_path(func)
.is_some_and(|call_path| matches!(call_path.as_slice(), ["ctypes", "WinError"]))
{
return;
{
return;
}
}

let mut diagnostic = Diagnostic::new(UnnecessaryParenOnRaiseException, arguments.range());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -281,4 +281,18 @@ RSE102.py:84:10: RSE102 [*] Unnecessary parentheses on raised exception
86 86 | # OK
87 87 | raise ctypes.WinError()

RSE102.py:107:27: RSE102 [*] Unnecessary parentheses on raised exception
|
105 | future = executor.submit(float, "a")
106 | if future.exception():
107 | raise future.Exception()
| ^^ RSE102
|
= help: Remove unnecessary parentheses

ℹ Unsafe fix
104 104 | # RSE102
105 105 | future = executor.submit(float, "a")
106 106 | if future.exception():
107 |- raise future.Exception()
107 |+ raise future.Exception
Loading