Skip to content

Commit f29c7b0

Browse files
authored
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.
1 parent 3a0d45c commit f29c7b0

3 files changed

Lines changed: 39 additions & 1 deletion

File tree

crates/ruff/tests/lint.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1021,6 +1021,22 @@ include = ["*.ipy"]
10211021
Ok(())
10221022
}
10231023

1024+
#[test]
1025+
fn warn_invalid_noqa_with_no_diagnostics() {
1026+
assert_cmd_snapshot!(Command::new(get_cargo_bin(BIN_NAME))
1027+
.args(STDIN_BASE_OPTIONS)
1028+
.args(["--isolated"])
1029+
.arg("--select")
1030+
.arg("F401")
1031+
.arg("-")
1032+
.pass_stdin(
1033+
r#"
1034+
# ruff: noqa: AAA101
1035+
print("Hello world!")
1036+
"#
1037+
));
1038+
}
1039+
10241040
#[test]
10251041
fn file_noqa_external() -> Result<()> {
10261042
let tempdir = TempDir::new()?;
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
---
2+
source: crates/ruff/tests/lint.rs
3+
info:
4+
program: ruff
5+
args:
6+
- check
7+
- "--no-cache"
8+
- "--output-format"
9+
- concise
10+
- "--isolated"
11+
- "--select"
12+
- F401
13+
- "-"
14+
stdin: "\n# ruff: noqa: AAA101\nprint(\"Hello world!\")\n"
15+
---
16+
success: true
17+
exit_code: 0
18+
----- stdout -----
19+
All checks passed!
20+
21+
----- stderr -----
22+
warning: Invalid rule code provided to `# ruff: noqa` at -:2: AAA101

crates/ruff_linter/src/linter.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,7 @@ pub fn check_path(
267267
}
268268

269269
// Enforce `noqa` directives.
270-
if (noqa.is_enabled() && !diagnostics.is_empty())
270+
if noqa.is_enabled()
271271
|| settings
272272
.rules
273273
.iter_enabled()

0 commit comments

Comments
 (0)