From 924e2641567feee13e13e9825e8e90b134f241e9 Mon Sep 17 00:00:00 2001 From: Aarni Koskela Date: Fri, 3 Feb 2023 16:03:49 +0200 Subject: [PATCH] Move flake8-{errmsg,print} violations (#2536) --- src/lib.rs | 1 - src/registry.rs | 12 ++-- src/rules/flake8_errmsg/rules.rs | 41 +++++++++++-- src/rules/flake8_print/rules/mod.rs | 2 +- src/rules/flake8_print/rules/print_call.rs | 37 +++++++++++- src/violations.rs | 69 ---------------------- 6 files changed, 78 insertions(+), 84 deletions(-) delete mode 100644 src/violations.rs diff --git a/src/lib.rs b/src/lib.rs index 004dcc8d79..a5c69597b2 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -47,7 +47,6 @@ pub mod settings; pub mod source_code; mod vendor; mod violation; -mod violations; mod visibility; use cfg_if::cfg_if; diff --git a/src/registry.rs b/src/registry.rs index 85122e0413..fcb3d9b57a 100644 --- a/src/registry.rs +++ b/src/registry.rs @@ -7,8 +7,8 @@ use strum_macros::{AsRefStr, EnumIter}; use crate::ast::types::Range; use crate::fix::Fix; +use crate::rules; use crate::violation::Violation; -use crate::{rules, violations}; ruff_macros::define_rule_mapping!( // pycodestyle errors @@ -167,8 +167,8 @@ ruff_macros::define_rule_mapping!( ISC002 => rules::flake8_implicit_str_concat::rules::MultiLineImplicitStringConcatenation, ISC003 => rules::flake8_implicit_str_concat::rules::ExplicitStringConcatenation, // flake8-print - T201 => violations::PrintFound, - T203 => violations::PPrintFound, + T201 => rules::flake8_print::rules::PrintFound, + T203 => rules::flake8_print::rules::PPrintFound, // flake8-quotes Q000 => rules::flake8_quotes::rules::BadQuotesInlineString, Q001 => rules::flake8_quotes::rules::BadQuotesMultilineString, @@ -385,9 +385,9 @@ ruff_macros::define_rule_mapping!( PD015 => rules::pandas_vet::rules::UseOfPdMerge, PD901 => rules::pandas_vet::rules::DfIsABadVariableName, // flake8-errmsg - EM101 => violations::RawStringInException, - EM102 => violations::FStringInException, - EM103 => violations::DotFormatInException, + EM101 => rules::flake8_errmsg::rules::RawStringInException, + EM102 => rules::flake8_errmsg::rules::FStringInException, + EM103 => rules::flake8_errmsg::rules::DotFormatInException, // flake8-pytest-style PT001 => rules::flake8_pytest_style::rules::IncorrectFixtureParenthesesStyle, PT002 => rules::flake8_pytest_style::rules::FixturePositionalArgs, diff --git a/src/rules/flake8_errmsg/rules.rs b/src/rules/flake8_errmsg/rules.rs index 0ce41444a4..ce2273995e 100644 --- a/src/rules/flake8_errmsg/rules.rs +++ b/src/rules/flake8_errmsg/rules.rs @@ -3,7 +3,40 @@ use rustpython_ast::{Constant, Expr, ExprKind}; use crate::ast::types::Range; use crate::checkers::ast::Checker; use crate::registry::{Diagnostic, Rule}; -use crate::violations; +use crate::violation::Violation; + +use crate::define_violation; +use ruff_macros::derive_message_formats; + +define_violation!( + pub struct RawStringInException; +); +impl Violation for RawStringInException { + #[derive_message_formats] + fn message(&self) -> String { + format!("Exception must not use a string literal, assign to variable first") + } +} + +define_violation!( + pub struct FStringInException; +); +impl Violation for FStringInException { + #[derive_message_formats] + fn message(&self) -> String { + format!("Exception must not use an f-string literal, assign to variable first") + } +} + +define_violation!( + pub struct DotFormatInException; +); +impl Violation for DotFormatInException { + #[derive_message_formats] + fn message(&self) -> String { + format!("Exception must not use a `.format()` string directly, assign to variable first") + } +} /// EM101, EM102, EM103 pub fn string_in_exception(checker: &mut Checker, exc: &Expr) { @@ -18,7 +51,7 @@ pub fn string_in_exception(checker: &mut Checker, exc: &Expr) { if checker.settings.rules.enabled(&Rule::RawStringInException) { if string.len() > checker.settings.flake8_errmsg.max_string_length { checker.diagnostics.push(Diagnostic::new( - violations::RawStringInException, + RawStringInException, Range::from_located(first), )); } @@ -28,7 +61,7 @@ pub fn string_in_exception(checker: &mut Checker, exc: &Expr) { ExprKind::JoinedStr { .. } => { if checker.settings.rules.enabled(&Rule::FStringInException) { checker.diagnostics.push(Diagnostic::new( - violations::FStringInException, + FStringInException, Range::from_located(first), )); } @@ -39,7 +72,7 @@ pub fn string_in_exception(checker: &mut Checker, exc: &Expr) { if let ExprKind::Attribute { value, attr, .. } = &func.node { if attr == "format" && matches!(value.node, ExprKind::Constant { .. }) { checker.diagnostics.push(Diagnostic::new( - violations::DotFormatInException, + DotFormatInException, Range::from_located(first), )); } diff --git a/src/rules/flake8_print/rules/mod.rs b/src/rules/flake8_print/rules/mod.rs index feda2e53f4..879b765b5a 100644 --- a/src/rules/flake8_print/rules/mod.rs +++ b/src/rules/flake8_print/rules/mod.rs @@ -1,3 +1,3 @@ -pub use print_call::print_call; +pub use print_call::{print_call, PPrintFound, PrintFound}; mod print_call; diff --git a/src/rules/flake8_print/rules/print_call.rs b/src/rules/flake8_print/rules/print_call.rs index 8bc0e4ca73..00251fa6ca 100644 --- a/src/rules/flake8_print/rules/print_call.rs +++ b/src/rules/flake8_print/rules/print_call.rs @@ -1,12 +1,43 @@ use log::error; use rustpython_ast::{Expr, Keyword, Stmt, StmtKind}; +use ruff_macros::derive_message_formats; + use crate::ast::helpers::is_const_none; use crate::ast::types::Range; use crate::autofix::helpers; use crate::checkers::ast::Checker; +use crate::define_violation; use crate::registry::Diagnostic; -use crate::violations; +use crate::violation::AlwaysAutofixableViolation; + +define_violation!( + pub struct PrintFound; +); +impl AlwaysAutofixableViolation for PrintFound { + #[derive_message_formats] + fn message(&self) -> String { + format!("`print` found") + } + + fn autofix_title(&self) -> String { + "Remove `print`".to_string() + } +} + +define_violation!( + pub struct PPrintFound; +); +impl AlwaysAutofixableViolation for PPrintFound { + #[derive_message_formats] + fn message(&self) -> String { + format!("`pprint` found") + } + + fn autofix_title(&self) -> String { + "Remove `pprint`".to_string() + } +} /// T201, T203 pub fn print_call(checker: &mut Checker, func: &Expr, keywords: &[Keyword]) { @@ -34,11 +65,11 @@ pub fn print_call(checker: &mut Checker, func: &Expr, keywords: &[Keyword]) { } } } - Diagnostic::new(violations::PrintFound, Range::from_located(func)) + Diagnostic::new(PrintFound, Range::from_located(func)) } else if call_path.as_ref().map_or(false, |call_path| { *call_path.as_slice() == ["pprint", "pprint"] }) { - Diagnostic::new(violations::PPrintFound, Range::from_located(func)) + Diagnostic::new(PPrintFound, Range::from_located(func)) } else { return; } diff --git a/src/violations.rs b/src/violations.rs deleted file mode 100644 index 6cbca2d09f..0000000000 --- a/src/violations.rs +++ /dev/null @@ -1,69 +0,0 @@ -#![allow(clippy::useless_format)] - -use ruff_macros::derive_message_formats; - -use crate::define_violation; - -use crate::violation::{AlwaysAutofixableViolation, Violation}; - -// flake8-print - -define_violation!( - pub struct PrintFound; -); -impl AlwaysAutofixableViolation for PrintFound { - #[derive_message_formats] - fn message(&self) -> String { - format!("`print` found") - } - - fn autofix_title(&self) -> String { - "Remove `print`".to_string() - } -} - -define_violation!( - pub struct PPrintFound; -); -impl AlwaysAutofixableViolation for PPrintFound { - #[derive_message_formats] - fn message(&self) -> String { - format!("`pprint` found") - } - - fn autofix_title(&self) -> String { - "Remove `pprint`".to_string() - } -} - -// flake8-errmsg - -define_violation!( - pub struct RawStringInException; -); -impl Violation for RawStringInException { - #[derive_message_formats] - fn message(&self) -> String { - format!("Exception must not use a string literal, assign to variable first") - } -} - -define_violation!( - pub struct FStringInException; -); -impl Violation for FStringInException { - #[derive_message_formats] - fn message(&self) -> String { - format!("Exception must not use an f-string literal, assign to variable first") - } -} - -define_violation!( - pub struct DotFormatInException; -); -impl Violation for DotFormatInException { - #[derive_message_formats] - fn message(&self) -> String { - format!("Exception must not use a `.format()` string directly, assign to variable first") - } -}