Warn on invalid noqa even when there are no diagnostics (#16178)

On `main` we warn the user if there is an invalid noqa comment[^1] and
at least one of the following holds:

- There is at least one diagnostic
- A lint rule related to `noqa`s is enabled (e.g. `RUF100`)

This is probably strange behavior from the point of view of the user, so
we now show invalid `noqa`s even when there are no diagnostics.

Closes #12831

[^1]: For the current definition of "invalid noqa comment", which may be
expanded in #12811 . This PR is independent of loc. cit. in the sense
that the CLI warnings should be consistent, regardless of which `noqa`
comments are considered invalid.
This commit is contained in:
Dylan 2025-02-16 13:58:18 -06:00 committed by GitHub
parent 3a0d45c85b
commit f29c7b03ec
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 39 additions and 1 deletions

View file

@ -1021,6 +1021,22 @@ include = ["*.ipy"]
Ok(()) Ok(())
} }
#[test]
fn warn_invalid_noqa_with_no_diagnostics() {
assert_cmd_snapshot!(Command::new(get_cargo_bin(BIN_NAME))
.args(STDIN_BASE_OPTIONS)
.args(["--isolated"])
.arg("--select")
.arg("F401")
.arg("-")
.pass_stdin(
r#"
# ruff: noqa: AAA101
print("Hello world!")
"#
));
}
#[test] #[test]
fn file_noqa_external() -> Result<()> { fn file_noqa_external() -> Result<()> {
let tempdir = TempDir::new()?; let tempdir = TempDir::new()?;

View file

@ -0,0 +1,22 @@
---
source: crates/ruff/tests/lint.rs
info:
program: ruff
args:
- check
- "--no-cache"
- "--output-format"
- concise
- "--isolated"
- "--select"
- F401
- "-"
stdin: "\n# ruff: noqa: AAA101\nprint(\"Hello world!\")\n"
---
success: true
exit_code: 0
----- stdout -----
All checks passed!
----- stderr -----
warning: Invalid rule code provided to `# ruff: noqa` at -:2: AAA101

View file

@ -267,7 +267,7 @@ pub fn check_path(
} }
// Enforce `noqa` directives. // Enforce `noqa` directives.
if (noqa.is_enabled() && !diagnostics.is_empty()) if noqa.is_enabled()
|| settings || settings
.rules .rules
.iter_enabled() .iter_enabled()