mirror of https://github.com/astral-sh/ruff
Extend SIM105 to match also 'Ellipsis only' bodies in exception handlers (#3925)
This commit is contained in:
parent
002caadf9e
commit
333f1bd9ce
|
|
@ -59,3 +59,15 @@ def bar():
|
||||||
return foo()
|
return foo()
|
||||||
except ValueError:
|
except ValueError:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
def with_ellipsis():
|
||||||
|
try:
|
||||||
|
foo()
|
||||||
|
except ValueError:
|
||||||
|
...
|
||||||
|
|
||||||
|
def with_ellipsis_and_return():
|
||||||
|
try:
|
||||||
|
return foo()
|
||||||
|
except ValueError:
|
||||||
|
...
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,6 @@
|
||||||
use rustpython_parser::ast::{Excepthandler, ExcepthandlerKind, Located, Location, Stmt, StmtKind};
|
use rustpython_parser::ast::{
|
||||||
|
Constant, Excepthandler, ExcepthandlerKind, ExprKind, Located, Location, Stmt, StmtKind,
|
||||||
|
};
|
||||||
|
|
||||||
use ruff_diagnostics::{AlwaysAutofixableViolation, Diagnostic, Edit, Fix};
|
use ruff_diagnostics::{AlwaysAutofixableViolation, Diagnostic, Edit, Fix};
|
||||||
use ruff_macros::{derive_message_formats, violation};
|
use ruff_macros::{derive_message_formats, violation};
|
||||||
|
|
@ -60,7 +62,17 @@ pub fn suppressible_exception(
|
||||||
let handler = &handlers[0];
|
let handler = &handlers[0];
|
||||||
let ExcepthandlerKind::ExceptHandler { body, .. } = &handler.node;
|
let ExcepthandlerKind::ExceptHandler { body, .. } = &handler.node;
|
||||||
if body.len() == 1 {
|
if body.len() == 1 {
|
||||||
if matches!(body[0].node, StmtKind::Pass) {
|
let node = &body[0].node;
|
||||||
|
if matches!(node, StmtKind::Pass)
|
||||||
|
|| (matches!(
|
||||||
|
node,
|
||||||
|
StmtKind::Expr {
|
||||||
|
value,
|
||||||
|
..
|
||||||
|
}
|
||||||
|
if matches!(**value, Located { node: ExprKind::Constant { value: Constant::Ellipsis, .. }, ..})
|
||||||
|
))
|
||||||
|
{
|
||||||
let handler_names: Vec<String> = helpers::extract_handled_exceptions(handlers)
|
let handler_names: Vec<String> = helpers::extract_handled_exceptions(handlers)
|
||||||
.into_iter()
|
.into_iter()
|
||||||
.filter_map(compose_call_path)
|
.filter_map(compose_call_path)
|
||||||
|
|
|
||||||
|
|
@ -142,4 +142,39 @@ expression: diagnostics
|
||||||
row: 22
|
row: 22
|
||||||
column: 8
|
column: 8
|
||||||
parent: ~
|
parent: ~
|
||||||
|
- kind:
|
||||||
|
name: SuppressibleException
|
||||||
|
body: "Use `contextlib.suppress(ValueError)` instead of `try`-`except`-`pass`"
|
||||||
|
suggestion: "Replace with `contextlib.suppress(ValueError)`"
|
||||||
|
fixable: true
|
||||||
|
location:
|
||||||
|
row: 64
|
||||||
|
column: 4
|
||||||
|
end_location:
|
||||||
|
row: 67
|
||||||
|
column: 11
|
||||||
|
fix:
|
||||||
|
edits:
|
||||||
|
- content: "import contextlib\n"
|
||||||
|
location:
|
||||||
|
row: 1
|
||||||
|
column: 0
|
||||||
|
end_location:
|
||||||
|
row: 1
|
||||||
|
column: 0
|
||||||
|
- content: with contextlib.suppress(ValueError)
|
||||||
|
location:
|
||||||
|
row: 64
|
||||||
|
column: 4
|
||||||
|
end_location:
|
||||||
|
row: 64
|
||||||
|
column: 7
|
||||||
|
- content: ""
|
||||||
|
location:
|
||||||
|
row: 66
|
||||||
|
column: 0
|
||||||
|
end_location:
|
||||||
|
row: 67
|
||||||
|
column: 11
|
||||||
|
parent: ~
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue