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

Insert space to avoid syntax error in RSE fixes #6886

Merged
merged 1 commit into from
Aug 25, 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
11 changes: 11 additions & 0 deletions crates/ruff/resources/test/fixtures/flake8_raise/RSE102.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,3 +68,14 @@ def error():

# OK
raise ctypes.WinError(1)


# RSE102
raise IndexError()from ZeroDivisionError

raise IndexError()\
from ZeroDivisionError

raise IndexError() from ZeroDivisionError

raise IndexError();
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,25 @@ pub(crate) fn unnecessary_paren_on_raise_exception(checker: &mut Checker, expr:

let mut diagnostic = Diagnostic::new(UnnecessaryParenOnRaiseException, arguments.range());
if checker.patch(diagnostic.kind.rule()) {
diagnostic.set_fix(Fix::automatic(Edit::range_deletion(arguments.range())));
// If the arguments are immediately followed by a `from`, insert whitespace to avoid
// a syntax error, as in:
// ```python
// raise IndexError()from ZeroDivisionError
// ```
if checker
.locator()
.after(arguments.end())
.chars()
.next()
.is_some_and(char::is_alphanumeric)
{
diagnostic.set_fix(Fix::automatic(Edit::range_replacement(
" ".to_string(),
arguments.range(),
)));
} else {
diagnostic.set_fix(Fix::automatic(Edit::range_deletion(arguments.range())));
}
}
checker.diagnostics.push(diagnostic);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -179,4 +179,80 @@ RSE102.py:37:16: RSE102 [*] Unnecessary parentheses on raised exception
41 39 | # OK
42 40 | raise AssertionError

RSE102.py:74:17: RSE102 [*] Unnecessary parentheses on raised exception
|
73 | # RSE102
74 | raise IndexError()from ZeroDivisionError
| ^^ RSE102
75 |
76 | raise IndexError()\
|
= help: Remove unnecessary parentheses

ℹ Fix
71 71 |
72 72 |
73 73 | # RSE102
74 |-raise IndexError()from ZeroDivisionError
74 |+raise IndexError from ZeroDivisionError
75 75 |
76 76 | raise IndexError()\
77 77 | from ZeroDivisionError

RSE102.py:76:17: RSE102 [*] Unnecessary parentheses on raised exception
|
74 | raise IndexError()from ZeroDivisionError
75 |
76 | raise IndexError()\
| ^^ RSE102
77 | from ZeroDivisionError
|
= help: Remove unnecessary parentheses

ℹ Fix
73 73 | # RSE102
74 74 | raise IndexError()from ZeroDivisionError
75 75 |
76 |-raise IndexError()\
76 |+raise IndexError\
77 77 | from ZeroDivisionError
78 78 |
79 79 | raise IndexError() from ZeroDivisionError

RSE102.py:79:17: RSE102 [*] Unnecessary parentheses on raised exception
|
77 | from ZeroDivisionError
78 |
79 | raise IndexError() from ZeroDivisionError
| ^^ RSE102
80 |
81 | raise IndexError();
|
= help: Remove unnecessary parentheses

ℹ Fix
76 76 | raise IndexError()\
77 77 | from ZeroDivisionError
78 78 |
79 |-raise IndexError() from ZeroDivisionError
79 |+raise IndexError from ZeroDivisionError
80 80 |
81 81 | raise IndexError();

RSE102.py:81:17: RSE102 [*] Unnecessary parentheses on raised exception
|
79 | raise IndexError() from ZeroDivisionError
80 |
81 | raise IndexError();
| ^^ RSE102
|
= help: Remove unnecessary parentheses

ℹ Fix
78 78 |
79 79 | raise IndexError() from ZeroDivisionError
80 80 |
81 |-raise IndexError();
81 |+raise IndexError;


Loading