From f29c7b03ec9b2d4cc9d967aad2be466a3acce64e Mon Sep 17 00:00:00 2001 From: Dylan <53534755+dylwil3@users.noreply.github.com> Date: Sun, 16 Feb 2025 13:58:18 -0600 Subject: [PATCH] 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. --- crates/ruff/tests/lint.rs | 16 ++++++++++++++ ...warn_invalid_noqa_with_no_diagnostics.snap | 22 +++++++++++++++++++ crates/ruff_linter/src/linter.rs | 2 +- 3 files changed, 39 insertions(+), 1 deletion(-) create mode 100644 crates/ruff/tests/snapshots/lint__warn_invalid_noqa_with_no_diagnostics.snap diff --git a/crates/ruff/tests/lint.rs b/crates/ruff/tests/lint.rs index f9c819a5c4..c13ba49136 100644 --- a/crates/ruff/tests/lint.rs +++ b/crates/ruff/tests/lint.rs @@ -1021,6 +1021,22 @@ include = ["*.ipy"] 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] fn file_noqa_external() -> Result<()> { let tempdir = TempDir::new()?; diff --git a/crates/ruff/tests/snapshots/lint__warn_invalid_noqa_with_no_diagnostics.snap b/crates/ruff/tests/snapshots/lint__warn_invalid_noqa_with_no_diagnostics.snap new file mode 100644 index 0000000000..09772b73fc --- /dev/null +++ b/crates/ruff/tests/snapshots/lint__warn_invalid_noqa_with_no_diagnostics.snap @@ -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 diff --git a/crates/ruff_linter/src/linter.rs b/crates/ruff_linter/src/linter.rs index 9d08f30135..ed27a6fdfd 100644 --- a/crates/ruff_linter/src/linter.rs +++ b/crates/ruff_linter/src/linter.rs @@ -267,7 +267,7 @@ pub fn check_path( } // Enforce `noqa` directives. - if (noqa.is_enabled() && !diagnostics.is_empty()) + if noqa.is_enabled() || settings .rules .iter_enabled()