Move flake8-{errmsg,print} violations (#2536)

This commit is contained in:
Aarni Koskela 2023-02-03 16:03:49 +02:00 committed by GitHub
parent 14c5000ad5
commit 924e264156
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 78 additions and 84 deletions

View file

@ -47,7 +47,6 @@ pub mod settings;
pub mod source_code;
mod vendor;
mod violation;
mod violations;
mod visibility;
use cfg_if::cfg_if;

View file

@ -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,

View file

@ -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),
));
}

View file

@ -1,3 +1,3 @@
pub use print_call::print_call;
pub use print_call::{print_call, PPrintFound, PrintFound};
mod print_call;

View file

@ -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;
}

View file

@ -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")
}
}