mirror of
https://github.com/astral-sh/ruff.git
synced 2025-08-31 07:38:00 +00:00
Minor fixes for SIM105
(#1633)
This commit is contained in:
parent
9ede902328
commit
12166584c4
4 changed files with 32 additions and 4 deletions
|
@ -16,6 +16,11 @@ try:
|
|||
except: # SIM105
|
||||
pass
|
||||
|
||||
try:
|
||||
foo()
|
||||
except (a.Error, b.Error): # SIM105
|
||||
pass
|
||||
|
||||
try:
|
||||
foo()
|
||||
except ValueError:
|
||||
|
@ -29,3 +34,10 @@ except ValueError:
|
|||
pass
|
||||
else:
|
||||
print('bar')
|
||||
|
||||
try:
|
||||
foo()
|
||||
except ValueError:
|
||||
pass
|
||||
finally:
|
||||
print('bar')
|
||||
|
|
|
@ -1248,7 +1248,10 @@ where
|
|||
}
|
||||
}
|
||||
StmtKind::Try {
|
||||
handlers, orelse, ..
|
||||
handlers,
|
||||
orelse,
|
||||
finalbody,
|
||||
..
|
||||
} => {
|
||||
if self.settings.enabled.contains(&CheckCode::F707) {
|
||||
if let Some(check) =
|
||||
|
@ -1275,7 +1278,9 @@ where
|
|||
);
|
||||
}
|
||||
if self.settings.enabled.contains(&CheckCode::SIM105) {
|
||||
flake8_simplify::plugins::use_contextlib_suppress(self, stmt, handlers, orelse);
|
||||
flake8_simplify::plugins::use_contextlib_suppress(
|
||||
self, stmt, handlers, orelse, finalbody,
|
||||
);
|
||||
}
|
||||
}
|
||||
StmtKind::Assign { targets, value, .. } => {
|
||||
|
|
|
@ -11,8 +11,9 @@ pub fn use_contextlib_suppress(
|
|||
stmt: &Stmt,
|
||||
handlers: &[Excepthandler],
|
||||
orelse: &[Stmt],
|
||||
finalbody: &[Stmt],
|
||||
) {
|
||||
if handlers.len() != 1 || !orelse.is_empty() {
|
||||
if handlers.len() != 1 || !orelse.is_empty() || !finalbody.is_empty() {
|
||||
return;
|
||||
}
|
||||
let handler = &handlers[0];
|
||||
|
@ -21,7 +22,7 @@ pub fn use_contextlib_suppress(
|
|||
if matches!(body[0].node, StmtKind::Pass) {
|
||||
let handler_names: Vec<_> = helpers::extract_handler_names(handlers)
|
||||
.into_iter()
|
||||
.flatten()
|
||||
.map(|v| v.join("."))
|
||||
.collect();
|
||||
let exception = if handler_names.is_empty() {
|
||||
"Exception".to_string()
|
||||
|
|
|
@ -32,4 +32,14 @@ expression: checks
|
|||
column: 8
|
||||
fix: ~
|
||||
parent: ~
|
||||
- kind:
|
||||
UseContextlibSuppress: "a.Error, b.Error"
|
||||
location:
|
||||
row: 19
|
||||
column: 0
|
||||
end_location:
|
||||
row: 22
|
||||
column: 8
|
||||
fix: ~
|
||||
parent: ~
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue