From 8ce227047d8186ed8569cd693123dba7bca76c8f Mon Sep 17 00:00:00 2001 From: Charlie Marsh Date: Tue, 11 Apr 2023 23:35:15 -0400 Subject: [PATCH] Tidy up some `pygrep-hooks` rules (#3942) --- crates/ruff/src/checkers/physical_lines.rs | 4 +--- .../rules/pygrep_hooks/rules/blanket_noqa.rs | 18 ++++++++++-------- .../pygrep_hooks/rules/deprecated_log_warn.rs | 2 +- .../src/rules/pygrep_hooks/rules/no_eval.rs | 3 ++- 4 files changed, 14 insertions(+), 13 deletions(-) diff --git a/crates/ruff/src/checkers/physical_lines.rs b/crates/ruff/src/checkers/physical_lines.rs index a754f1b36773a..692e0bf8a1ed2 100644 --- a/crates/ruff/src/checkers/physical_lines.rs +++ b/crates/ruff/src/checkers/physical_lines.rs @@ -80,9 +80,7 @@ pub fn check_physical_lines( } if enforce_blanket_noqa { - if let Some(diagnostic) = blanket_noqa(index, line) { - diagnostics.push(diagnostic); - } + blanket_noqa(&mut diagnostics, index, line); } if enforce_shebang_missing diff --git a/crates/ruff/src/rules/pygrep_hooks/rules/blanket_noqa.rs b/crates/ruff/src/rules/pygrep_hooks/rules/blanket_noqa.rs index 8d3410959ef95..8dafd2439a131 100644 --- a/crates/ruff/src/rules/pygrep_hooks/rules/blanket_noqa.rs +++ b/crates/ruff/src/rules/pygrep_hooks/rules/blanket_noqa.rs @@ -19,15 +19,17 @@ impl Violation for BlanketNOQA { static BLANKET_NOQA_REGEX: Lazy = Lazy::new(|| Regex::new(r"(?i)# noqa($|\s|:[^ ])").unwrap()); -/// PGH004 - use of blanket noqa comments -pub fn blanket_noqa(lineno: usize, line: &str) -> Option { - BLANKET_NOQA_REGEX.find(line).map(|m| { - Diagnostic::new( +/// PGH004 +pub fn blanket_noqa(diagnostics: &mut Vec, lineno: usize, line: &str) { + if let Some(match_) = BLANKET_NOQA_REGEX.find(line) { + let start = line[..match_.start()].chars().count(); + let end = start + line[match_.start()..match_.end()].chars().count(); + diagnostics.push(Diagnostic::new( BlanketNOQA, Range::new( - Location::new(lineno + 1, m.start()), - Location::new(lineno + 1, m.end()), + Location::new(lineno + 1, start), + Location::new(lineno + 1, end), ), - ) - }) + )); + } } diff --git a/crates/ruff/src/rules/pygrep_hooks/rules/deprecated_log_warn.rs b/crates/ruff/src/rules/pygrep_hooks/rules/deprecated_log_warn.rs index 7b39ebec29312..98d9709ad9a7c 100644 --- a/crates/ruff/src/rules/pygrep_hooks/rules/deprecated_log_warn.rs +++ b/crates/ruff/src/rules/pygrep_hooks/rules/deprecated_log_warn.rs @@ -16,7 +16,7 @@ impl Violation for DeprecatedLogWarn { } } -/// PGH002 - deprecated use of logging.warn +/// PGH002 pub fn deprecated_log_warn(checker: &mut Checker, func: &Expr) { if checker .ctx diff --git a/crates/ruff/src/rules/pygrep_hooks/rules/no_eval.rs b/crates/ruff/src/rules/pygrep_hooks/rules/no_eval.rs index 0c68501b85f4f..6f767b7618589 100644 --- a/crates/ruff/src/rules/pygrep_hooks/rules/no_eval.rs +++ b/crates/ruff/src/rules/pygrep_hooks/rules/no_eval.rs @@ -15,7 +15,8 @@ impl Violation for Eval { format!("No builtin `eval()` allowed") } } -/// PGH001 - no eval + +/// PGH001 pub fn no_eval(checker: &mut Checker, func: &Expr) { let ExprKind::Name { id, .. } = &func.node else { return;