Respect file-level # ruff: noqa suppressions for unused-noqa rule (#6405)

## Summary

We weren't respecting `# ruff: noqa: RUF100`, i.e., file-level
suppressions for the `unused-noqa` rule itself.

Closes https://github.com/astral-sh/ruff/issues/6385.
This commit is contained in:
Charlie Marsh 2023-08-07 16:33:01 -04:00 committed by GitHub
parent 3d06fe743d
commit 927cfc9564
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 28 additions and 2 deletions

View file

@ -0,0 +1,5 @@
# ruff: noqa: RUF100
import os # noqa: F401
print(os.sep)

View file

@ -94,8 +94,15 @@ pub(crate) fn check_noqa(
}
}
// Enforce that the noqa directive was actually used (RUF100).
if analyze_directives && settings.rules.enabled(Rule::UnusedNOQA) {
// Enforce that the noqa directive was actually used (RUF100), unless RUF100 was itself
// suppressed.
if settings.rules.enabled(Rule::UnusedNOQA)
&& analyze_directives
&& !exemption.is_some_and(|exemption| match exemption {
FileExemption::All => true,
FileExemption::Codes(codes) => codes.contains(&Rule::UnusedNOQA.noqa_code()),
})
{
for line in noqa_directives.lines() {
match &line.directive {
Directive::All(directive) => {

View file

@ -156,6 +156,16 @@ mod tests {
Ok(())
}
#[test]
fn ruf100_4() -> Result<()> {
let diagnostics = test_path(
Path::new("ruff/RUF100_4.py"),
&settings::Settings::for_rules(vec![Rule::UnusedNOQA, Rule::UnusedImport]),
)?;
assert_messages!(diagnostics);
Ok(())
}
#[test]
fn flake8_noqa() -> Result<()> {
let diagnostics = test_path(

View file

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