The changes in this commit were generated by running: for f in $(find src -name '*.rs'); do sed -Ei 's/use crate::registry::.*;/\0use crate::violations;/g' $f; done for f in $(find src -name '*.rs'); do sed -Ei 's/CheckKind::([A-Z])/violations::\1/g' $f; done git checkout src/registry.rs src/lib.rs src/lib_wasm.rs src/violations.rs cargo +nightly fmt
55 lines
1.6 KiB
Rust
55 lines
1.6 KiB
Rust
use rustpython_ast::{ExprKind, Stmt, StmtKind};
|
|
|
|
use crate::ast::types::Range;
|
|
use crate::ast::visitor::Visitor;
|
|
use crate::checkers::ast::Checker;
|
|
use crate::python::string::is_lower;
|
|
use crate::registry::{Check, CheckKind};
|
|
use crate::violations;
|
|
|
|
struct RaiseVisitor {
|
|
checks: Vec<Check>,
|
|
}
|
|
|
|
impl<'a> Visitor<'a> for RaiseVisitor {
|
|
fn visit_stmt(&mut self, stmt: &'a Stmt) {
|
|
match &stmt.node {
|
|
StmtKind::Raise {
|
|
exc: Some(exc),
|
|
cause: None,
|
|
} => match &exc.node {
|
|
ExprKind::Name { id, .. } if is_lower(id) => {}
|
|
_ => {
|
|
self.checks.push(Check::new(
|
|
violations::RaiseWithoutFromInsideExcept,
|
|
Range::from_located(stmt),
|
|
));
|
|
}
|
|
},
|
|
StmtKind::ClassDef { .. }
|
|
| StmtKind::FunctionDef { .. }
|
|
| StmtKind::AsyncFunctionDef { .. }
|
|
| StmtKind::Try { .. } => {}
|
|
StmtKind::If { body, .. }
|
|
| StmtKind::While { body, .. }
|
|
| StmtKind::With { body, .. }
|
|
| StmtKind::AsyncWith { body, .. }
|
|
| StmtKind::For { body, .. }
|
|
| StmtKind::AsyncFor { body, .. } => {
|
|
for stmt in body {
|
|
self.visit_stmt(stmt);
|
|
}
|
|
}
|
|
_ => {}
|
|
}
|
|
}
|
|
}
|
|
|
|
pub fn raise_without_from_inside_except(checker: &mut Checker, body: &[Stmt]) {
|
|
let mut visitor = RaiseVisitor { checks: vec![] };
|
|
for stmt in body {
|
|
visitor.visit_stmt(stmt);
|
|
}
|
|
checker.checks.extend(visitor.checks);
|
|
}
|