Skip to content

Commit

Permalink
Insert space to avoid syntax error in RSE fixes (#6886)
Browse files Browse the repository at this point in the history
Closes #6810.
  • Loading branch information
charliermarsh authored Aug 25, 2023
1 parent bd625b1 commit 2883ae4
Show file tree
Hide file tree
Showing 3 changed files with 106 additions and 1 deletion.
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;


0 comments on commit 2883ae4

Please sign in to comment.