-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Enable autofix for unnecessary-paren-on-raise-exception
- Loading branch information
1 parent
291ef98
commit 3c0b8b1
Showing
5 changed files
with
173 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
23 changes: 21 additions & 2 deletions
23
crates/ruff/resources/test/fixtures/flake8_raise/RSE102.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
38 changes: 27 additions & 11 deletions
38
crates/ruff/src/rules/flake8_raise/rules/unnecessary_paren_on_raise_exception.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,31 +1,47 @@ | ||
use rustpython_ast::{Expr, ExprKind}; | ||
|
||
use ruff_macros::derive_message_formats; | ||
|
||
use crate::ast::types::Range; | ||
use crate::ast::helpers::match_parens; | ||
use crate::checkers::ast::Checker; | ||
use crate::define_violation; | ||
use crate::fix::Fix; | ||
use crate::registry::Diagnostic; | ||
use crate::violation::Violation; | ||
use rustpython_ast::{Expr, ExprKind}; | ||
use crate::violation::AlwaysAutofixableViolation; | ||
|
||
define_violation!( | ||
pub struct UnnecessaryParenOnRaiseException; | ||
); | ||
impl Violation for UnnecessaryParenOnRaiseException { | ||
impl AlwaysAutofixableViolation for UnnecessaryParenOnRaiseException { | ||
#[derive_message_formats] | ||
fn message(&self) -> String { | ||
format!("Unnecessary parentheses on raised exception") | ||
} | ||
|
||
fn autofix_title(&self) -> String { | ||
format!("Remove unnecessary parentheses") | ||
} | ||
} | ||
|
||
/// RSE102 | ||
pub fn unnecessary_paren_on_raise_exception(checker: &mut Checker, expr: &Expr) { | ||
match &expr.node { | ||
ExprKind::Call { args, keywords, .. } if args.is_empty() && keywords.is_empty() => { | ||
checker.diagnostics.push(Diagnostic::new( | ||
UnnecessaryParenOnRaiseException, | ||
Range::from_located(expr), | ||
)); | ||
if let ExprKind::Call { | ||
func, | ||
args, | ||
keywords, | ||
} = &expr.node | ||
{ | ||
if args.is_empty() && keywords.is_empty() { | ||
let range = match_parens(func.end_location.unwrap(), checker.locator) | ||
.expect("Expected call to include parentheses"); | ||
let mut diagnostic = Diagnostic::new(UnnecessaryParenOnRaiseException, range); | ||
if checker.patch(diagnostic.kind.rule()) { | ||
diagnostic.amend(Fix::deletion( | ||
func.end_location.unwrap(), | ||
range.end_location, | ||
)); | ||
} | ||
checker.diagnostics.push(diagnostic); | ||
} | ||
_ => (), | ||
} | ||
} |
106 changes: 97 additions & 9 deletions
106
...ots/ruff__rules__flake8_raise__tests__unnecessary-paren-on-raise-exception_RSE102.py.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,25 +1,113 @@ | ||
--- | ||
source: src/rules/flake8_raise/mod.rs | ||
source: crates/ruff/src/rules/flake8_raise/mod.rs | ||
expression: diagnostics | ||
--- | ||
- kind: | ||
UnnecessaryParenOnRaiseException: ~ | ||
location: | ||
row: 4 | ||
column: 10 | ||
row: 5 | ||
column: 20 | ||
end_location: | ||
row: 4 | ||
row: 5 | ||
column: 22 | ||
fix: ~ | ||
fix: | ||
content: | ||
- "" | ||
location: | ||
row: 5 | ||
column: 20 | ||
end_location: | ||
row: 5 | ||
column: 22 | ||
parent: ~ | ||
- kind: | ||
UnnecessaryParenOnRaiseException: ~ | ||
location: | ||
row: 11 | ||
column: 6 | ||
row: 13 | ||
column: 15 | ||
end_location: | ||
row: 11 | ||
row: 13 | ||
column: 17 | ||
fix: ~ | ||
fix: | ||
content: | ||
- "" | ||
location: | ||
row: 13 | ||
column: 15 | ||
end_location: | ||
row: 13 | ||
column: 17 | ||
parent: ~ | ||
- kind: | ||
UnnecessaryParenOnRaiseException: ~ | ||
location: | ||
row: 16 | ||
column: 16 | ||
end_location: | ||
row: 16 | ||
column: 18 | ||
fix: | ||
content: | ||
- "" | ||
location: | ||
row: 16 | ||
column: 15 | ||
end_location: | ||
row: 16 | ||
column: 18 | ||
parent: ~ | ||
- kind: | ||
UnnecessaryParenOnRaiseException: ~ | ||
location: | ||
row: 20 | ||
column: 4 | ||
end_location: | ||
row: 20 | ||
column: 6 | ||
fix: | ||
content: | ||
- "" | ||
location: | ||
row: 19 | ||
column: 15 | ||
end_location: | ||
row: 20 | ||
column: 6 | ||
parent: ~ | ||
- kind: | ||
UnnecessaryParenOnRaiseException: ~ | ||
location: | ||
row: 23 | ||
column: 15 | ||
end_location: | ||
row: 25 | ||
column: 1 | ||
fix: | ||
content: | ||
- "" | ||
location: | ||
row: 23 | ||
column: 15 | ||
end_location: | ||
row: 25 | ||
column: 1 | ||
parent: ~ | ||
- kind: | ||
UnnecessaryParenOnRaiseException: ~ | ||
location: | ||
row: 28 | ||
column: 15 | ||
end_location: | ||
row: 30 | ||
column: 1 | ||
fix: | ||
content: | ||
- "" | ||
location: | ||
row: 28 | ||
column: 15 | ||
end_location: | ||
row: 30 | ||
column: 1 | ||
parent: ~ | ||
|