diff --git a/src/checkers/ast.rs b/src/checkers/ast.rs index bda2295c44afc..0f29e1854ff1f 100644 --- a/src/checkers/ast.rs +++ b/src/checkers/ast.rs @@ -1067,17 +1067,11 @@ where } } if self.settings.enabled.contains(&CheckCode::EM101) - | self.settings.enabled.contains(&CheckCode::EM102) - | self.settings.enabled.contains(&CheckCode::EM103) + || self.settings.enabled.contains(&CheckCode::EM102) + || self.settings.enabled.contains(&CheckCode::EM103) { if let Some(exc) = exc { - self.add_checks( - flake8_errmsg::checks::check_string_in_exception( - exc, - self.settings.flake8_errmsg.max_string_length, - ) - .into_iter(), - ); + flake8_errmsg::plugins::string_in_exception(self, exc); } } } diff --git a/src/flake8_errmsg/checks.rs b/src/flake8_errmsg/checks.rs deleted file mode 100644 index f7551cc6a7b73..0000000000000 --- a/src/flake8_errmsg/checks.rs +++ /dev/null @@ -1,46 +0,0 @@ -use rustpython_ast::{Constant, Expr, ExprKind}; - -use crate::ast::types::Range; -use crate::checks::{Check, CheckKind}; - -pub fn check_string_in_exception(exc: &Expr, max_string_length: usize) -> Vec { - let mut checks = vec![]; - - if let ExprKind::Call { args, .. } = &exc.node { - if let Some(first) = args.first() { - match &first.node { - // Check for string literals - ExprKind::Constant { - value: Constant::Str(string), - .. - } => { - if string.len() > max_string_length { - checks.push(Check::new( - CheckKind::RawStringInException, - Range::from_located(first), - )); - } - } - // Check for f-strings - ExprKind::JoinedStr { .. } => checks.push(Check::new( - CheckKind::FStringInException, - Range::from_located(first), - )), - // Check for .format() calls - ExprKind::Call { func, .. } => { - if let ExprKind::Attribute { value, attr, .. } = &func.node { - if attr == "format" && matches!(value.node, ExprKind::Constant { .. }) { - checks.push(Check::new( - CheckKind::DotFormatInException, - Range::from_located(first), - )); - } - } - } - _ => {} - } - } - } - - checks -} diff --git a/src/flake8_errmsg/mod.rs b/src/flake8_errmsg/mod.rs index cb0ae88fe2986..b7da432baab3c 100644 --- a/src/flake8_errmsg/mod.rs +++ b/src/flake8_errmsg/mod.rs @@ -1,4 +1,4 @@ -pub mod checks; +pub mod plugins; pub mod settings; #[cfg(test)] diff --git a/src/flake8_errmsg/plugins.rs b/src/flake8_errmsg/plugins.rs new file mode 100644 index 0000000000000..af93679d794c8 --- /dev/null +++ b/src/flake8_errmsg/plugins.rs @@ -0,0 +1,52 @@ +use rustpython_ast::{Constant, Expr, ExprKind}; + +use crate::ast::types::Range; +use crate::checkers::ast::Checker; +use crate::checks::{Check, CheckCode, CheckKind}; + +/// EM101, EM102, EM103 +pub fn string_in_exception(checker: &mut Checker, exc: &Expr) { + if let ExprKind::Call { args, .. } = &exc.node { + if let Some(first) = args.first() { + match &first.node { + // Check for string literals + ExprKind::Constant { + value: Constant::Str(string), + .. + } => { + if checker.settings.enabled.contains(&CheckCode::EM101) { + if string.len() > checker.settings.flake8_errmsg.max_string_length { + checker.add_check(Check::new( + CheckKind::RawStringInException, + Range::from_located(first), + )); + } + } + } + // Check for f-strings + ExprKind::JoinedStr { .. } => { + if checker.settings.enabled.contains(&CheckCode::EM102) { + checker.add_check(Check::new( + CheckKind::FStringInException, + Range::from_located(first), + )); + } + } + // Check for .format() calls + ExprKind::Call { func, .. } => { + if checker.settings.enabled.contains(&CheckCode::EM103) { + if let ExprKind::Attribute { value, attr, .. } = &func.node { + if attr == "format" && matches!(value.node, ExprKind::Constant { .. }) { + checker.add_check(Check::new( + CheckKind::DotFormatInException, + Range::from_located(first), + )); + } + } + } + } + _ => {} + } + } + } +}