Skip to content

Commit

Permalink
Make unnecessary-paren-on-raise-exception an unsafe edit
Browse files Browse the repository at this point in the history
  • Loading branch information
charliermarsh committed Oct 25, 2023
1 parent 9792b15 commit 257d985
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,13 @@ use crate::checkers::ast::Checker;
///
/// Removing the parentheses makes the code more concise.
///
/// ## Known problems
/// Parentheses can only be omitted if the exception is a class, as opposed to
/// a function call. This rule isn't always capable of distinguishing between
/// the two. For example, if you define a method `get_exception` that itself
/// returns an exception object, this rule will falsy mark the parentheses
/// in `raise get_exception()` as unnecessary.
///
/// ## Example
/// ```python
/// raise TypeError()
Expand Down Expand Up @@ -73,6 +80,7 @@ pub(crate) fn unnecessary_paren_on_raise_exception(checker: &mut Checker, expr:
}

let mut diagnostic = Diagnostic::new(UnnecessaryParenOnRaiseException, arguments.range());

// If the arguments are immediately followed by a `from`, insert whitespace to avoid
// a syntax error, as in:
// ```python
Expand All @@ -85,13 +93,14 @@ pub(crate) fn unnecessary_paren_on_raise_exception(checker: &mut Checker, expr:
.next()
.is_some_and(char::is_alphanumeric)
{
diagnostic.set_fix(Fix::safe_edit(Edit::range_replacement(
diagnostic.set_fix(Fix::unsafe_edit(Edit::range_replacement(
" ".to_string(),
arguments.range(),
)));
} else {
diagnostic.set_fix(Fix::safe_edit(Edit::range_deletion(arguments.range())));
diagnostic.set_fix(Fix::unsafe_edit(Edit::range_deletion(arguments.range())));
}

checker.diagnostics.push(diagnostic);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ RSE102.py:5:21: RSE102 [*] Unnecessary parentheses on raised exception
|
= help: Remove unnecessary parentheses

Fix
Suggested fix
2 2 | y = 6 + "7"
3 3 | except TypeError:
4 4 | # RSE102
Expand All @@ -32,7 +32,7 @@ RSE102.py:13:16: RSE102 [*] Unnecessary parentheses on raised exception
|
= help: Remove unnecessary parentheses

Fix
Suggested fix
10 10 | raise
11 11 |
12 12 | # RSE102
Expand All @@ -52,7 +52,7 @@ RSE102.py:16:17: RSE102 [*] Unnecessary parentheses on raised exception
|
= help: Remove unnecessary parentheses

Fix
Suggested fix
13 13 | raise TypeError()
14 14 |
15 15 | # RSE102
Expand All @@ -73,7 +73,7 @@ RSE102.py:20:5: RSE102 [*] Unnecessary parentheses on raised exception
|
= help: Remove unnecessary parentheses

Fix
Suggested fix
17 17 |
18 18 | # RSE102
19 19 | raise TypeError \
Expand All @@ -94,7 +94,7 @@ RSE102.py:24:5: RSE102 [*] Unnecessary parentheses on raised exception
|
= help: Remove unnecessary parentheses

Fix
Suggested fix
21 21 |
22 22 | # RSE102
23 23 | raise TypeError \
Expand All @@ -117,7 +117,7 @@ RSE102.py:27:16: RSE102 [*] Unnecessary parentheses on raised exception
|
= help: Remove unnecessary parentheses

Fix
Suggested fix
24 24 | ();
25 25 |
26 26 | # RSE102
Expand All @@ -142,7 +142,7 @@ RSE102.py:32:19: RSE102 [*] Unnecessary parentheses on raised exception
|
= help: Remove unnecessary parentheses

Fix
Suggested fix
29 29 | )
30 30 |
31 31 | # RSE102
Expand All @@ -167,7 +167,7 @@ RSE102.py:37:16: RSE102 [*] Unnecessary parentheses on raised exception
|
= help: Remove unnecessary parentheses

Fix
Suggested fix
34 34 | )
35 35 |
36 36 | # RSE102
Expand All @@ -189,7 +189,7 @@ RSE102.py:74:17: RSE102 [*] Unnecessary parentheses on raised exception
|
= help: Remove unnecessary parentheses

Fix
Suggested fix
71 71 |
72 72 |
73 73 | # RSE102
Expand All @@ -209,7 +209,7 @@ RSE102.py:76:17: RSE102 [*] Unnecessary parentheses on raised exception
|
= help: Remove unnecessary parentheses

Fix
Suggested fix
73 73 | # RSE102
74 74 | raise IndexError()from ZeroDivisionError
75 75 |
Expand All @@ -230,7 +230,7 @@ RSE102.py:79:17: RSE102 [*] Unnecessary parentheses on raised exception
|
= help: Remove unnecessary parentheses

Fix
Suggested fix
76 76 | raise IndexError()\
77 77 | from ZeroDivisionError
78 78 |
Expand All @@ -248,7 +248,7 @@ RSE102.py:81:17: RSE102 [*] Unnecessary parentheses on raised exception
|
= help: Remove unnecessary parentheses

Fix
Suggested fix
78 78 |
79 79 | raise IndexError() from ZeroDivisionError
80 80 |
Expand Down

0 comments on commit 257d985

Please sign in to comment.