forked from astral-sh/ruff
/
use_contextlib_suppress.rs
38 lines (36 loc) · 1.13 KB
/
use_contextlib_suppress.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
use rustpython_ast::{Excepthandler, ExcepthandlerKind, Stmt, StmtKind};
use crate::ast::helpers;
use crate::ast::types::Range;
use crate::checkers::ast::Checker;
use crate::registry::{Check, CheckKind};
/// SIM105
pub fn use_contextlib_suppress(
checker: &mut Checker,
stmt: &Stmt,
handlers: &[Excepthandler],
orelse: &[Stmt],
) {
if handlers.len() != 1 || !orelse.is_empty() {
return;
}
let handler = &handlers[0];
let ExcepthandlerKind::ExceptHandler { body, .. } = &handler.node;
if body.len() == 1 {
if matches!(body[0].node, StmtKind::Pass) {
let handler_names: Vec<_> = helpers::extract_handler_names(handlers)
.into_iter()
.flatten()
.collect();
let exception = if handler_names.is_empty() {
"Exception".to_string()
} else {
handler_names.join(", ")
};
let check = Check::new(
CheckKind::UseContextlibSuppress(exception),
Range::from_located(stmt),
);
checker.add_check(check);
}
}
}