Move pygrep-hooks violations (#2539)

This commit is contained in:
Aarni Koskela 2023-02-03 15:41:05 +02:00 committed by GitHub
parent 47e0b2521a
commit d985473f4f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 65 additions and 61 deletions

View File

@ -367,10 +367,10 @@ ruff_macros::define_rule_mapping!(
DTZ011 => rules::flake8_datetimez::rules::CallDateToday, DTZ011 => rules::flake8_datetimez::rules::CallDateToday,
DTZ012 => rules::flake8_datetimez::rules::CallDateFromtimestamp, DTZ012 => rules::flake8_datetimez::rules::CallDateFromtimestamp,
// pygrep-hooks // pygrep-hooks
PGH001 => violations::NoEval, PGH001 => rules::pygrep_hooks::rules::NoEval,
PGH002 => violations::DeprecatedLogWarn, PGH002 => rules::pygrep_hooks::rules::DeprecatedLogWarn,
PGH003 => violations::BlanketTypeIgnore, PGH003 => rules::pygrep_hooks::rules::BlanketTypeIgnore,
PGH004 => violations::BlanketNOQA, PGH004 => rules::pygrep_hooks::rules::BlanketNOQA,
// pandas-vet // pandas-vet
PD002 => rules::pandas_vet::rules::UseOfInplaceArgument, PD002 => rules::pandas_vet::rules::UseOfInplaceArgument,
PD003 => rules::pandas_vet::rules::UseOfDotIsNull, PD003 => rules::pandas_vet::rules::UseOfDotIsNull,

View File

@ -1,10 +1,22 @@
use crate::define_violation;
use crate::violation::Violation;
use once_cell::sync::Lazy; use once_cell::sync::Lazy;
use regex::Regex; use regex::Regex;
use ruff_macros::derive_message_formats;
use rustpython_ast::Location; use rustpython_ast::Location;
use crate::ast::types::Range; use crate::ast::types::Range;
use crate::registry::Diagnostic; use crate::registry::Diagnostic;
use crate::violations;
define_violation!(
pub struct BlanketNOQA;
);
impl Violation for BlanketNOQA {
#[derive_message_formats]
fn message(&self) -> String {
format!("Use specific rule codes when using `noqa`")
}
}
static BLANKET_NOQA_REGEX: Lazy<Regex> = static BLANKET_NOQA_REGEX: Lazy<Regex> =
Lazy::new(|| Regex::new(r"(?i)# noqa($|\s|:[^ ])").unwrap()); Lazy::new(|| Regex::new(r"(?i)# noqa($|\s|:[^ ])").unwrap());
@ -13,7 +25,7 @@ static BLANKET_NOQA_REGEX: Lazy<Regex> =
pub fn blanket_noqa(lineno: usize, line: &str) -> Option<Diagnostic> { pub fn blanket_noqa(lineno: usize, line: &str) -> Option<Diagnostic> {
BLANKET_NOQA_REGEX.find(line).map(|m| { BLANKET_NOQA_REGEX.find(line).map(|m| {
Diagnostic::new( Diagnostic::new(
violations::BlanketNOQA, BlanketNOQA,
Range::new( Range::new(
Location::new(lineno + 1, m.start()), Location::new(lineno + 1, m.start()),
Location::new(lineno + 1, m.end()), Location::new(lineno + 1, m.end()),

View File

@ -1,10 +1,22 @@
use crate::define_violation;
use crate::violation::Violation;
use once_cell::sync::Lazy; use once_cell::sync::Lazy;
use regex::Regex; use regex::Regex;
use ruff_macros::derive_message_formats;
use rustpython_ast::Location; use rustpython_ast::Location;
use crate::ast::types::Range; use crate::ast::types::Range;
use crate::registry::Diagnostic; use crate::registry::Diagnostic;
use crate::violations;
define_violation!(
pub struct BlanketTypeIgnore;
);
impl Violation for BlanketTypeIgnore {
#[derive_message_formats]
fn message(&self) -> String {
format!("Use specific rule codes when ignoring type issues")
}
}
static BLANKET_TYPE_IGNORE_REGEX: Lazy<Regex> = static BLANKET_TYPE_IGNORE_REGEX: Lazy<Regex> =
Lazy::new(|| Regex::new(r"# type:? *ignore($|\s)").unwrap()); Lazy::new(|| Regex::new(r"# type:? *ignore($|\s)").unwrap());
@ -13,7 +25,7 @@ static BLANKET_TYPE_IGNORE_REGEX: Lazy<Regex> =
pub fn blanket_type_ignore(lineno: usize, line: &str) -> Option<Diagnostic> { pub fn blanket_type_ignore(lineno: usize, line: &str) -> Option<Diagnostic> {
BLANKET_TYPE_IGNORE_REGEX.find(line).map(|m| { BLANKET_TYPE_IGNORE_REGEX.find(line).map(|m| {
Diagnostic::new( Diagnostic::new(
violations::BlanketTypeIgnore, BlanketTypeIgnore,
Range::new( Range::new(
Location::new(lineno + 1, m.start()), Location::new(lineno + 1, m.start()),
Location::new(lineno + 1, m.end()), Location::new(lineno + 1, m.end()),

View File

@ -1,9 +1,21 @@
use crate::define_violation;
use crate::violation::Violation;
use ruff_macros::derive_message_formats;
use rustpython_ast::Expr; use rustpython_ast::Expr;
use crate::ast::types::Range; use crate::ast::types::Range;
use crate::checkers::ast::Checker; use crate::checkers::ast::Checker;
use crate::registry::Diagnostic; use crate::registry::Diagnostic;
use crate::violations;
define_violation!(
pub struct DeprecatedLogWarn;
);
impl Violation for DeprecatedLogWarn {
#[derive_message_formats]
fn message(&self) -> String {
format!("`warn` is deprecated in favor of `warning`")
}
}
/// PGH002 - deprecated use of logging.warn /// PGH002 - deprecated use of logging.warn
pub fn deprecated_log_warn(checker: &mut Checker, func: &Expr) { pub fn deprecated_log_warn(checker: &mut Checker, func: &Expr) {
@ -11,7 +23,7 @@ pub fn deprecated_log_warn(checker: &mut Checker, func: &Expr) {
call_path.as_slice() == ["logging", "warn"] call_path.as_slice() == ["logging", "warn"]
}) { }) {
checker.diagnostics.push(Diagnostic::new( checker.diagnostics.push(Diagnostic::new(
violations::DeprecatedLogWarn, DeprecatedLogWarn,
Range::from_located(func), Range::from_located(func),
)); ));
} }

View File

@ -1,7 +1,7 @@
pub use blanket_noqa::blanket_noqa; pub use blanket_noqa::{blanket_noqa, BlanketNOQA};
pub use blanket_type_ignore::blanket_type_ignore; pub use blanket_type_ignore::{blanket_type_ignore, BlanketTypeIgnore};
pub use deprecated_log_warn::deprecated_log_warn; pub use deprecated_log_warn::{deprecated_log_warn, DeprecatedLogWarn};
pub use no_eval::no_eval; pub use no_eval::{no_eval, NoEval};
mod blanket_noqa; mod blanket_noqa;
mod blanket_type_ignore; mod blanket_type_ignore;

View File

@ -1,10 +1,21 @@
use crate::define_violation;
use crate::violation::Violation;
use ruff_macros::derive_message_formats;
use rustpython_ast::{Expr, ExprKind}; use rustpython_ast::{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; use crate::registry::Diagnostic;
use crate::violations;
define_violation!(
pub struct NoEval;
);
impl Violation for NoEval {
#[derive_message_formats]
fn message(&self) -> String {
format!("No builtin `eval()` allowed")
}
}
/// PGH001 - no eval /// PGH001 - no eval
pub fn no_eval(checker: &mut Checker, func: &Expr) { pub fn no_eval(checker: &mut Checker, func: &Expr) {
let ExprKind::Name { id, .. } = &func.node else { let ExprKind::Name { id, .. } = &func.node else {
@ -16,8 +27,7 @@ pub fn no_eval(checker: &mut Checker, func: &Expr) {
if !checker.is_builtin("eval") { if !checker.is_builtin("eval") {
return; return;
} }
checker.diagnostics.push(Diagnostic::new( checker
violations::NoEval, .diagnostics
Range::from_located(func), .push(Diagnostic::new(NoEval, Range::from_located(func)));
));
} }

View File

@ -52,48 +52,6 @@ impl AlwaysAutofixableViolation for PPrintFound {
} }
} }
// pygrep-hooks
define_violation!(
pub struct NoEval;
);
impl Violation for NoEval {
#[derive_message_formats]
fn message(&self) -> String {
format!("No builtin `eval()` allowed")
}
}
define_violation!(
pub struct DeprecatedLogWarn;
);
impl Violation for DeprecatedLogWarn {
#[derive_message_formats]
fn message(&self) -> String {
format!("`warn` is deprecated in favor of `warning`")
}
}
define_violation!(
pub struct BlanketTypeIgnore;
);
impl Violation for BlanketTypeIgnore {
#[derive_message_formats]
fn message(&self) -> String {
format!("Use specific rule codes when ignoring type issues")
}
}
define_violation!(
pub struct BlanketNOQA;
);
impl Violation for BlanketNOQA {
#[derive_message_formats]
fn message(&self) -> String {
format!("Use specific rule codes when using `noqa`")
}
}
// flake8-errmsg // flake8-errmsg
define_violation!( define_violation!(