mirror of https://github.com/astral-sh/ruff
Enable autofix for unnecessary-paren-on-raise-exception (#2596)
This commit is contained in:
parent
291ef9856a
commit
f6864a96f6
|
|
@ -1380,7 +1380,7 @@ For more, see [flake8-raise](https://pypi.org/project/flake8-raise/) on PyPI.
|
||||||
|
|
||||||
| Code | Name | Message | Fix |
|
| Code | Name | Message | Fix |
|
||||||
| ---- | ---- | ------- | --- |
|
| ---- | ---- | ------- | --- |
|
||||||
| RSE102 | unnecessary-paren-on-raise-exception | Unnecessary parentheses on raised exception | |
|
| RSE102 | unnecessary-paren-on-raise-exception | Unnecessary parentheses on raised exception | 🛠 |
|
||||||
|
|
||||||
### flake8-self (SLF)
|
### flake8-self (SLF)
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,14 +1,33 @@
|
||||||
try:
|
try:
|
||||||
y = 6 + "7"
|
y = 6 + "7"
|
||||||
except TypeError:
|
except TypeError:
|
||||||
raise ValueError() # RSE102
|
# RSE102
|
||||||
|
raise ValueError()
|
||||||
|
|
||||||
try:
|
try:
|
||||||
x = 1 / 0
|
x = 1 / 0
|
||||||
except ZeroDivisionError:
|
except ZeroDivisionError:
|
||||||
raise
|
raise
|
||||||
|
|
||||||
raise TypeError() # RSE102
|
# RSE102
|
||||||
|
raise TypeError()
|
||||||
|
|
||||||
|
# RSE102
|
||||||
|
raise TypeError ()
|
||||||
|
|
||||||
|
# RSE102
|
||||||
|
raise TypeError \
|
||||||
|
()
|
||||||
|
|
||||||
|
# RSE102
|
||||||
|
raise TypeError(
|
||||||
|
|
||||||
|
)
|
||||||
|
|
||||||
|
# RSE102
|
||||||
|
raise TypeError(
|
||||||
|
# Hello, world!
|
||||||
|
)
|
||||||
|
|
||||||
raise AssertionError
|
raise AssertionError
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -755,6 +755,33 @@ pub fn count_trailing_lines(stmt: &Stmt, locator: &Locator) -> usize {
|
||||||
.count()
|
.count()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Return the range of the first parenthesis pair after a given [`Location`].
|
||||||
|
pub fn match_parens(start: Location, locator: &Locator) -> Option<Range> {
|
||||||
|
let contents = locator.slice_source_code_at(start);
|
||||||
|
let mut fix_start = None;
|
||||||
|
let mut fix_end = None;
|
||||||
|
let mut count: usize = 0;
|
||||||
|
for (start, tok, end) in lexer::make_tokenizer_located(contents, start).flatten() {
|
||||||
|
if matches!(tok, Tok::Lpar) {
|
||||||
|
if count == 0 {
|
||||||
|
fix_start = Some(start);
|
||||||
|
}
|
||||||
|
count += 1;
|
||||||
|
}
|
||||||
|
if matches!(tok, Tok::Rpar) {
|
||||||
|
count -= 1;
|
||||||
|
if count == 0 {
|
||||||
|
fix_end = Some(end);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
match (fix_start, fix_end) {
|
||||||
|
(Some(start), Some(end)) => Some(Range::new(start, end)),
|
||||||
|
_ => None,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// Return the appropriate visual `Range` for any message that spans a `Stmt`.
|
/// Return the appropriate visual `Range` for any message that spans a `Stmt`.
|
||||||
/// Specifically, this method returns the range of a function or class name,
|
/// Specifically, this method returns the range of a function or class name,
|
||||||
/// rather than that of the entire function or class body.
|
/// rather than that of the entire function or class body.
|
||||||
|
|
|
||||||
|
|
@ -1,31 +1,47 @@
|
||||||
|
use rustpython_ast::{Expr, ExprKind};
|
||||||
|
|
||||||
use ruff_macros::derive_message_formats;
|
use ruff_macros::derive_message_formats;
|
||||||
|
|
||||||
use crate::ast::types::Range;
|
use crate::ast::helpers::match_parens;
|
||||||
use crate::checkers::ast::Checker;
|
use crate::checkers::ast::Checker;
|
||||||
use crate::define_violation;
|
use crate::define_violation;
|
||||||
|
use crate::fix::Fix;
|
||||||
use crate::registry::Diagnostic;
|
use crate::registry::Diagnostic;
|
||||||
use crate::violation::Violation;
|
use crate::violation::AlwaysAutofixableViolation;
|
||||||
use rustpython_ast::{Expr, ExprKind};
|
|
||||||
|
|
||||||
define_violation!(
|
define_violation!(
|
||||||
pub struct UnnecessaryParenOnRaiseException;
|
pub struct UnnecessaryParenOnRaiseException;
|
||||||
);
|
);
|
||||||
impl Violation for UnnecessaryParenOnRaiseException {
|
impl AlwaysAutofixableViolation for UnnecessaryParenOnRaiseException {
|
||||||
#[derive_message_formats]
|
#[derive_message_formats]
|
||||||
fn message(&self) -> String {
|
fn message(&self) -> String {
|
||||||
format!("Unnecessary parentheses on raised exception")
|
format!("Unnecessary parentheses on raised exception")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn autofix_title(&self) -> String {
|
||||||
|
format!("Remove unnecessary parentheses")
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// RSE102
|
/// RSE102
|
||||||
pub fn unnecessary_paren_on_raise_exception(checker: &mut Checker, expr: &Expr) {
|
pub fn unnecessary_paren_on_raise_exception(checker: &mut Checker, expr: &Expr) {
|
||||||
match &expr.node {
|
if let ExprKind::Call {
|
||||||
ExprKind::Call { args, keywords, .. } if args.is_empty() && keywords.is_empty() => {
|
func,
|
||||||
checker.diagnostics.push(Diagnostic::new(
|
args,
|
||||||
UnnecessaryParenOnRaiseException,
|
keywords,
|
||||||
Range::from_located(expr),
|
} = &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);
|
||||||
}
|
}
|
||||||
_ => (),
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,25 +1,113 @@
|
||||||
---
|
---
|
||||||
source: src/rules/flake8_raise/mod.rs
|
source: crates/ruff/src/rules/flake8_raise/mod.rs
|
||||||
expression: diagnostics
|
expression: diagnostics
|
||||||
---
|
---
|
||||||
- kind:
|
- kind:
|
||||||
UnnecessaryParenOnRaiseException: ~
|
UnnecessaryParenOnRaiseException: ~
|
||||||
location:
|
location:
|
||||||
row: 4
|
row: 5
|
||||||
column: 10
|
column: 20
|
||||||
end_location:
|
end_location:
|
||||||
row: 4
|
row: 5
|
||||||
column: 22
|
column: 22
|
||||||
fix: ~
|
fix:
|
||||||
|
content:
|
||||||
|
- ""
|
||||||
|
location:
|
||||||
|
row: 5
|
||||||
|
column: 20
|
||||||
|
end_location:
|
||||||
|
row: 5
|
||||||
|
column: 22
|
||||||
parent: ~
|
parent: ~
|
||||||
- kind:
|
- kind:
|
||||||
UnnecessaryParenOnRaiseException: ~
|
UnnecessaryParenOnRaiseException: ~
|
||||||
location:
|
location:
|
||||||
row: 11
|
row: 13
|
||||||
column: 6
|
column: 15
|
||||||
end_location:
|
end_location:
|
||||||
row: 11
|
row: 13
|
||||||
column: 17
|
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: ~
|
parent: ~
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue