Skip to content

Commit

Permalink
Respect per-file-ignores for RUF100 with no other diagnostics (#11058)
Browse files Browse the repository at this point in the history
## Summary

The existing test didn't cover the case in which there are _no_ other
diagnostics in the file.

Closes #10906.
  • Loading branch information
charliermarsh authored Apr 20, 2024
1 parent c80b9a4 commit d544199
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 1 deletion.
8 changes: 7 additions & 1 deletion crates/ruff_linter/src/linter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,13 @@ pub fn check_path(
}

// Ignore diagnostics based on per-file-ignores.
let per_file_ignores = if !diagnostics.is_empty() && !settings.per_file_ignores.is_empty() {
let per_file_ignores = if (!diagnostics.is_empty()
|| settings
.rules
.iter_enabled()
.any(|rule_code| rule_code.lint_source().is_noqa()))
&& !settings.per_file_ignores.is_empty()
{
fs::ignores_from_path(path, &settings.per_file_ignores)
} else {
RuleSet::empty()
Expand Down
18 changes: 18 additions & 0 deletions crates/ruff_linter/src/rules/ruff/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,24 @@ mod tests {
Ok(())
}

#[test]
fn ruff_per_file_ignores_empty() -> Result<()> {
let diagnostics = test_path(
Path::new("ruff/ruff_per_file_ignores.py"),
&settings::LinterSettings {
per_file_ignores: CompiledPerFileIgnoreList::resolve(vec![PerFileIgnore::new(
"ruff_per_file_ignores.py".to_string(),
&["RUF100".parse().unwrap()],
None,
)])
.unwrap(),
..settings::LinterSettings::for_rules(vec![Rule::UnusedNOQA])
},
)?;
assert_messages!(diagnostics);
Ok(())
}

#[test]
fn flake8_noqa() -> Result<()> {
let diagnostics = test_path(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
---
source: crates/ruff_linter/src/rules/ruff/mod.rs
---

0 comments on commit d544199

Please sign in to comment.