diff --git a/crates/ruff_linter/src/linter.rs b/crates/ruff_linter/src/linter.rs index 4f5de73ae9686..1e69755ab48c3 100644 --- a/crates/ruff_linter/src/linter.rs +++ b/crates/ruff_linter/src/linter.rs @@ -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() diff --git a/crates/ruff_linter/src/rules/ruff/mod.rs b/crates/ruff_linter/src/rules/ruff/mod.rs index b26cfa6afb5b4..4727ee0f2ba29 100644 --- a/crates/ruff_linter/src/rules/ruff/mod.rs +++ b/crates/ruff_linter/src/rules/ruff/mod.rs @@ -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( diff --git a/crates/ruff_linter/src/rules/ruff/snapshots/ruff_linter__rules__ruff__tests__ruff_per_file_ignores_empty.snap b/crates/ruff_linter/src/rules/ruff/snapshots/ruff_linter__rules__ruff__tests__ruff_per_file_ignores_empty.snap new file mode 100644 index 0000000000000..7f58cfd7246a3 --- /dev/null +++ b/crates/ruff_linter/src/rules/ruff/snapshots/ruff_linter__rules__ruff__tests__ruff_per_file_ignores_empty.snap @@ -0,0 +1,4 @@ +--- +source: crates/ruff_linter/src/rules/ruff/mod.rs +--- +