Respect per-file-ignores for RUF100 with no other diagnostics (#11058)

## Summary

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

Closes https://github.com/astral-sh/ruff/issues/10906.
This commit is contained in:
Charlie Marsh 2024-04-20 11:33:22 -04:00 committed by GitHub
parent c80b9a4a90
commit d544199272
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 29 additions and 1 deletions

View file

@ -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()

View file

@ -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(

View file

@ -0,0 +1,4 @@
---
source: crates/ruff_linter/src/rules/ruff/mod.rs
---