Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[pygrep_hooks] Fix blanket-noqa panic when last line has noqa with no newline (PGH004) #11108

Merged
merged 3 commits into from
Apr 25, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
#noqa
45 changes: 29 additions & 16 deletions crates/ruff_linter/src/noqa.rs
Original file line number Diff line number Diff line change
Expand Up @@ -664,6 +664,7 @@ impl Ranged for NoqaDirectiveLine<'_> {
#[derive(Debug, Default)]
pub(crate) struct NoqaDirectives<'a> {
inner: Vec<NoqaDirectiveLine<'a>>,
last_directive_includes_eof: bool,
}

impl<'a> NoqaDirectives<'a> {
Expand Down Expand Up @@ -694,14 +695,17 @@ impl<'a> NoqaDirectives<'a> {
}
}

// Extend a mapping at the end of the file to also include the EOF token.
if let Some(last) = directives.last_mut() {
if last.range.end() == locator.contents().text_len() {
last.range = last.range.add_end(TextSize::from(1));
}
}
// Record whether last directive should include EOF token.
let last_directive_includes_eof = if let Some(last) = directives.last() {
last.range.end() == locator.contents().text_len()
} else {
false
};

Self { inner: directives }
Self {
inner: directives,
last_directive_includes_eof,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: I think I would rename this to includes_end to make it clear that the comparison should include the end range.

}
}

pub(crate) fn find_line_with_directive(&self, offset: TextSize) -> Option<&NoqaDirectiveLine> {
Expand All @@ -720,17 +724,26 @@ impl<'a> NoqaDirectives<'a> {
}

fn find_line_index(&self, offset: TextSize) -> Option<usize> {
self.inner
.binary_search_by(|directive| {
if directive.range.end() < offset {
std::cmp::Ordering::Less
} else if directive.range.contains(offset) {
std::cmp::Ordering::Equal
let index = self.inner.binary_search_by(|directive| {
if directive.range.end() < offset {
std::cmp::Ordering::Less
} else if directive.range.contains(offset) {
std::cmp::Ordering::Equal
} else {
std::cmp::Ordering::Greater
}
});

match index {
Ok(index) => Some(index),
Err(index) => {
if self.last_directive_includes_eof && index == self.inner.len() - 1 {
Some(index)
} else {
std::cmp::Ordering::Greater
None
}
})
.ok()
}
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The search then becomes

self inner.binary_search_by(|directive| {
	if directive.range.end() < offset {
		std::cmp::Ordering::Less
	} else if directive.range.start() >  offset {
		std::cmp::Ordering::Greater
	} 
	// At this point, end >= offset, start <= offset
	else if !directive.includes_end && directive.range.end() == offset  {
		std::cmp::Ordering::Less // I think or should it be greater?
	} else {
		std::cmp::Ordering::Equal
	}

}

pub(crate) fn lines(&self) -> &[NoqaDirectiveLine] {
Expand Down
1 change: 1 addition & 0 deletions crates/ruff_linter/src/rules/pygrep_hooks/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,9 +88,8 @@ 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 noqa_end = all.end() - offset;
let line = locator.slice(directive_line);
let noqa_end = all.end() - directive_line.start();

// Skip the `# noqa`, plus any trailing whitespace.
let mut cursor = Cursor::new(&line[noqa_end.to_usize()..]);
Expand Down
Original file line number Diff line number Diff line change
@@ -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
|
Loading