From e6f8e72fbc9762e2f0066cf9cd681fa8fadd823c Mon Sep 17 00:00:00 2001 From: augustelalande Date: Tue, 23 Apr 2024 12:59:13 -0400 Subject: [PATCH] fix bug --- .../resources/test/fixtures/pygrep_hooks/PGH004_1.py | 1 + crates/ruff_linter/src/rules/pygrep_hooks/mod.rs | 1 + .../src/rules/pygrep_hooks/rules/blanket_noqa.rs | 11 ++++++++--- ...ules__pygrep_hooks__tests__PGH004_PGH004_1.py.snap | 8 ++++++++ 4 files changed, 18 insertions(+), 3 deletions(-) create mode 100644 crates/ruff_linter/resources/test/fixtures/pygrep_hooks/PGH004_1.py create mode 100644 crates/ruff_linter/src/rules/pygrep_hooks/snapshots/ruff_linter__rules__pygrep_hooks__tests__PGH004_PGH004_1.py.snap diff --git a/crates/ruff_linter/resources/test/fixtures/pygrep_hooks/PGH004_1.py b/crates/ruff_linter/resources/test/fixtures/pygrep_hooks/PGH004_1.py new file mode 100644 index 0000000000000..ea4cde0074264 --- /dev/null +++ b/crates/ruff_linter/resources/test/fixtures/pygrep_hooks/PGH004_1.py @@ -0,0 +1 @@ +#noqa \ No newline at end of file diff --git a/crates/ruff_linter/src/rules/pygrep_hooks/mod.rs b/crates/ruff_linter/src/rules/pygrep_hooks/mod.rs index 8cce061b151c3..f06e8eda13780 100644 --- a/crates/ruff_linter/src/rules/pygrep_hooks/mod.rs +++ b/crates/ruff_linter/src/rules/pygrep_hooks/mod.rs @@ -16,6 +16,7 @@ mod tests { #[test_case(Rule::BlanketTypeIgnore, Path::new("PGH003_0.py"))] #[test_case(Rule::BlanketTypeIgnore, Path::new("PGH003_1.py"))] #[test_case(Rule::BlanketNOQA, Path::new("PGH004_0.py"))] + #[test_case(Rule::BlanketNOQA, Path::new("PGH004_1.py"))] #[test_case(Rule::InvalidMockAccess, Path::new("PGH005_0.py"))] fn rules(rule_code: Rule, path: &Path) -> Result<()> { let snapshot = format!("{}_{}", rule_code.noqa_code(), path.to_string_lossy()); diff --git a/crates/ruff_linter/src/rules/pygrep_hooks/rules/blanket_noqa.rs b/crates/ruff_linter/src/rules/pygrep_hooks/rules/blanket_noqa.rs index e8cbf7da62b1c..0213d4d6a920c 100644 --- a/crates/ruff_linter/src/rules/pygrep_hooks/rules/blanket_noqa.rs +++ b/crates/ruff_linter/src/rules/pygrep_hooks/rules/blanket_noqa.rs @@ -2,7 +2,7 @@ use ruff_diagnostics::{Diagnostic, Edit, Fix, FixAvailability, Violation}; use ruff_macros::{derive_message_formats, violation}; use ruff_python_trivia::Cursor; use ruff_source_file::Locator; -use ruff_text_size::{Ranged, TextRange}; +use ruff_text_size::{Ranged, TextLen, TextRange}; use crate::noqa::{Directive, NoqaDirectives}; @@ -88,8 +88,13 @@ pub(crate) fn blanket_noqa( ) { for directive_line in noqa_directives.lines() { if let Directive::All(all) = &directive_line.directive { - let line = locator.slice(directive_line.range); - let offset = directive_line.range.start(); + let line = if directive_line.end() > locator.contents().text_len() { + let range = TextRange::new(directive_line.start(), locator.contents().text_len()); + locator.slice(range) + } else { + locator.slice(directive_line) + }; + let offset = directive_line.start(); let noqa_end = all.end() - offset; // Skip the `# noqa`, plus any trailing whitespace. diff --git a/crates/ruff_linter/src/rules/pygrep_hooks/snapshots/ruff_linter__rules__pygrep_hooks__tests__PGH004_PGH004_1.py.snap b/crates/ruff_linter/src/rules/pygrep_hooks/snapshots/ruff_linter__rules__pygrep_hooks__tests__PGH004_PGH004_1.py.snap new file mode 100644 index 0000000000000..34c02426953b0 --- /dev/null +++ b/crates/ruff_linter/src/rules/pygrep_hooks/snapshots/ruff_linter__rules__pygrep_hooks__tests__PGH004_PGH004_1.py.snap @@ -0,0 +1,8 @@ +--- +source: crates/ruff_linter/src/rules/pygrep_hooks/mod.rs +--- +PGH004_1.py:1:1: PGH004 Use specific rule codes when using `noqa` + | +1 | #noqa + | ^^^^^ PGH004 + |