mirror of
https://github.com/astral-sh/ruff.git
synced 2025-09-26 20:10:09 +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
|
except: # SIM105
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
try:
|
||||||
|
foo()
|
||||||
|
except (a.Error, b.Error): # SIM105
|
||||||
|
pass
|
||||||
|
|
||||||
try:
|
try:
|
||||||
foo()
|
foo()
|
||||||
except ValueError:
|
except ValueError:
|
||||||
|
@ -29,3 +34,10 @@ except ValueError:
|
||||||
pass
|
pass
|
||||||
else:
|
else:
|
||||||
print('bar')
|
print('bar')
|
||||||
|
|
||||||
|
try:
|
||||||
|
foo()
|
||||||
|
except ValueError:
|
||||||
|
pass
|
||||||
|
finally:
|
||||||
|
print('bar')
|
||||||
|
|
|
@ -1248,7 +1248,10 @@ where
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
StmtKind::Try {
|
StmtKind::Try {
|
||||||
handlers, orelse, ..
|
handlers,
|
||||||
|
orelse,
|
||||||
|
finalbody,
|
||||||
|
..
|
||||||
} => {
|
} => {
|
||||||
if self.settings.enabled.contains(&CheckCode::F707) {
|
if self.settings.enabled.contains(&CheckCode::F707) {
|
||||||
if let Some(check) =
|
if let Some(check) =
|
||||||
|
@ -1275,7 +1278,9 @@ where
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
if self.settings.enabled.contains(&CheckCode::SIM105) {
|
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, .. } => {
|
StmtKind::Assign { targets, value, .. } => {
|
||||||
|
|
|
@ -11,8 +11,9 @@ pub fn use_contextlib_suppress(
|
||||||
stmt: &Stmt,
|
stmt: &Stmt,
|
||||||
handlers: &[Excepthandler],
|
handlers: &[Excepthandler],
|
||||||
orelse: &[Stmt],
|
orelse: &[Stmt],
|
||||||
|
finalbody: &[Stmt],
|
||||||
) {
|
) {
|
||||||
if handlers.len() != 1 || !orelse.is_empty() {
|
if handlers.len() != 1 || !orelse.is_empty() || !finalbody.is_empty() {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
let handler = &handlers[0];
|
let handler = &handlers[0];
|
||||||
|
@ -21,7 +22,7 @@ pub fn use_contextlib_suppress(
|
||||||
if matches!(body[0].node, StmtKind::Pass) {
|
if matches!(body[0].node, StmtKind::Pass) {
|
||||||
let handler_names: Vec<_> = helpers::extract_handler_names(handlers)
|
let handler_names: Vec<_> = helpers::extract_handler_names(handlers)
|
||||||
.into_iter()
|
.into_iter()
|
||||||
.flatten()
|
.map(|v| v.join("."))
|
||||||
.collect();
|
.collect();
|
||||||
let exception = if handler_names.is_empty() {
|
let exception = if handler_names.is_empty() {
|
||||||
"Exception".to_string()
|
"Exception".to_string()
|
||||||
|
|
|
@ -32,4 +32,14 @@ expression: checks
|
||||||
column: 8
|
column: 8
|
||||||
fix: ~
|
fix: ~
|
||||||
parent: ~
|
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