mirror of
https://github.com/astral-sh/ruff.git
synced 2025-09-26 20:10:09 +00:00
Move flake8-{errmsg,print} violations (#2536)
This commit is contained in:
parent
14c5000ad5
commit
924e264156
6 changed files with 78 additions and 84 deletions
|
@ -47,7 +47,6 @@ pub mod settings;
|
||||||
pub mod source_code;
|
pub mod source_code;
|
||||||
mod vendor;
|
mod vendor;
|
||||||
mod violation;
|
mod violation;
|
||||||
mod violations;
|
|
||||||
mod visibility;
|
mod visibility;
|
||||||
|
|
||||||
use cfg_if::cfg_if;
|
use cfg_if::cfg_if;
|
||||||
|
|
|
@ -7,8 +7,8 @@ use strum_macros::{AsRefStr, EnumIter};
|
||||||
|
|
||||||
use crate::ast::types::Range;
|
use crate::ast::types::Range;
|
||||||
use crate::fix::Fix;
|
use crate::fix::Fix;
|
||||||
|
use crate::rules;
|
||||||
use crate::violation::Violation;
|
use crate::violation::Violation;
|
||||||
use crate::{rules, violations};
|
|
||||||
|
|
||||||
ruff_macros::define_rule_mapping!(
|
ruff_macros::define_rule_mapping!(
|
||||||
// pycodestyle errors
|
// pycodestyle errors
|
||||||
|
@ -167,8 +167,8 @@ ruff_macros::define_rule_mapping!(
|
||||||
ISC002 => rules::flake8_implicit_str_concat::rules::MultiLineImplicitStringConcatenation,
|
ISC002 => rules::flake8_implicit_str_concat::rules::MultiLineImplicitStringConcatenation,
|
||||||
ISC003 => rules::flake8_implicit_str_concat::rules::ExplicitStringConcatenation,
|
ISC003 => rules::flake8_implicit_str_concat::rules::ExplicitStringConcatenation,
|
||||||
// flake8-print
|
// flake8-print
|
||||||
T201 => violations::PrintFound,
|
T201 => rules::flake8_print::rules::PrintFound,
|
||||||
T203 => violations::PPrintFound,
|
T203 => rules::flake8_print::rules::PPrintFound,
|
||||||
// flake8-quotes
|
// flake8-quotes
|
||||||
Q000 => rules::flake8_quotes::rules::BadQuotesInlineString,
|
Q000 => rules::flake8_quotes::rules::BadQuotesInlineString,
|
||||||
Q001 => rules::flake8_quotes::rules::BadQuotesMultilineString,
|
Q001 => rules::flake8_quotes::rules::BadQuotesMultilineString,
|
||||||
|
@ -385,9 +385,9 @@ ruff_macros::define_rule_mapping!(
|
||||||
PD015 => rules::pandas_vet::rules::UseOfPdMerge,
|
PD015 => rules::pandas_vet::rules::UseOfPdMerge,
|
||||||
PD901 => rules::pandas_vet::rules::DfIsABadVariableName,
|
PD901 => rules::pandas_vet::rules::DfIsABadVariableName,
|
||||||
// flake8-errmsg
|
// flake8-errmsg
|
||||||
EM101 => violations::RawStringInException,
|
EM101 => rules::flake8_errmsg::rules::RawStringInException,
|
||||||
EM102 => violations::FStringInException,
|
EM102 => rules::flake8_errmsg::rules::FStringInException,
|
||||||
EM103 => violations::DotFormatInException,
|
EM103 => rules::flake8_errmsg::rules::DotFormatInException,
|
||||||
// flake8-pytest-style
|
// flake8-pytest-style
|
||||||
PT001 => rules::flake8_pytest_style::rules::IncorrectFixtureParenthesesStyle,
|
PT001 => rules::flake8_pytest_style::rules::IncorrectFixtureParenthesesStyle,
|
||||||
PT002 => rules::flake8_pytest_style::rules::FixturePositionalArgs,
|
PT002 => rules::flake8_pytest_style::rules::FixturePositionalArgs,
|
||||||
|
|
|
@ -3,7 +3,40 @@ use rustpython_ast::{Constant, Expr, ExprKind};
|
||||||
use crate::ast::types::Range;
|
use crate::ast::types::Range;
|
||||||
use crate::checkers::ast::Checker;
|
use crate::checkers::ast::Checker;
|
||||||
use crate::registry::{Diagnostic, Rule};
|
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
|
/// EM101, EM102, EM103
|
||||||
pub fn string_in_exception(checker: &mut Checker, exc: &Expr) {
|
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 checker.settings.rules.enabled(&Rule::RawStringInException) {
|
||||||
if string.len() > checker.settings.flake8_errmsg.max_string_length {
|
if string.len() > checker.settings.flake8_errmsg.max_string_length {
|
||||||
checker.diagnostics.push(Diagnostic::new(
|
checker.diagnostics.push(Diagnostic::new(
|
||||||
violations::RawStringInException,
|
RawStringInException,
|
||||||
Range::from_located(first),
|
Range::from_located(first),
|
||||||
));
|
));
|
||||||
}
|
}
|
||||||
|
@ -28,7 +61,7 @@ pub fn string_in_exception(checker: &mut Checker, exc: &Expr) {
|
||||||
ExprKind::JoinedStr { .. } => {
|
ExprKind::JoinedStr { .. } => {
|
||||||
if checker.settings.rules.enabled(&Rule::FStringInException) {
|
if checker.settings.rules.enabled(&Rule::FStringInException) {
|
||||||
checker.diagnostics.push(Diagnostic::new(
|
checker.diagnostics.push(Diagnostic::new(
|
||||||
violations::FStringInException,
|
FStringInException,
|
||||||
Range::from_located(first),
|
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 let ExprKind::Attribute { value, attr, .. } = &func.node {
|
||||||
if attr == "format" && matches!(value.node, ExprKind::Constant { .. }) {
|
if attr == "format" && matches!(value.node, ExprKind::Constant { .. }) {
|
||||||
checker.diagnostics.push(Diagnostic::new(
|
checker.diagnostics.push(Diagnostic::new(
|
||||||
violations::DotFormatInException,
|
DotFormatInException,
|
||||||
Range::from_located(first),
|
Range::from_located(first),
|
||||||
));
|
));
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,3 +1,3 @@
|
||||||
pub use print_call::print_call;
|
pub use print_call::{print_call, PPrintFound, PrintFound};
|
||||||
|
|
||||||
mod print_call;
|
mod print_call;
|
||||||
|
|
|
@ -1,12 +1,43 @@
|
||||||
use log::error;
|
use log::error;
|
||||||
use rustpython_ast::{Expr, Keyword, Stmt, StmtKind};
|
use rustpython_ast::{Expr, Keyword, Stmt, StmtKind};
|
||||||
|
|
||||||
|
use ruff_macros::derive_message_formats;
|
||||||
|
|
||||||
use crate::ast::helpers::is_const_none;
|
use crate::ast::helpers::is_const_none;
|
||||||
use crate::ast::types::Range;
|
use crate::ast::types::Range;
|
||||||
use crate::autofix::helpers;
|
use crate::autofix::helpers;
|
||||||
use crate::checkers::ast::Checker;
|
use crate::checkers::ast::Checker;
|
||||||
|
use crate::define_violation;
|
||||||
use crate::registry::Diagnostic;
|
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
|
/// T201, T203
|
||||||
pub fn print_call(checker: &mut Checker, func: &Expr, keywords: &[Keyword]) {
|
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| {
|
} else if call_path.as_ref().map_or(false, |call_path| {
|
||||||
*call_path.as_slice() == ["pprint", "pprint"]
|
*call_path.as_slice() == ["pprint", "pprint"]
|
||||||
}) {
|
}) {
|
||||||
Diagnostic::new(violations::PPrintFound, Range::from_located(func))
|
Diagnostic::new(PPrintFound, Range::from_located(func))
|
||||||
} else {
|
} else {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
|
@ -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")
|
|
||||||
}
|
|
||||||
}
|
|
Loading…
Add table
Add a link
Reference in a new issue