Skip to content

Commit 563daa8

Browse files
carljmAlexWaygood
andauthored
Fix docs and add overlap test for negated per-file-ignores (#10863)
Refs #3172 ## Summary Fix a typo in the docs example, and add a test for the case where a negative pattern and a positive pattern overlap. The behavior here is simple: patterns (positive or negative) are always additive if they hit (i.e. match for a positive pattern, don't match for a negated pattern). We never "un-ignore" previously-ignored rules based on a pattern (positive or negative) failing to hit. It's simple enough that I don't really see other cases we need to add tests for (the tests we have cover all branches in the ignores_from_path function that implements the core logic), but open to reviewer feedback. I also didn't end up changing the docs to explain this more, because I think they are accurate as written and don't wrongly imply any more complex behavior. Open to reviewer feedback on this as well! After some discussion, I think allowing negative patterns to un-ignore rules is too confusing and easy to get wrong; if we need that, we should add `per-file-selects` instead. ## Test Plan Test/docs only change; tests pass, docs render and look right. --------- Co-authored-by: Alex Waygood <[email protected]>
1 parent 7ae15c6 commit 563daa8

File tree

2 files changed

+37
-1
lines changed

2 files changed

+37
-1
lines changed

crates/ruff/tests/lint.rs

+36
Original file line numberDiff line numberDiff line change
@@ -1248,3 +1248,39 @@ fn negated_per_file_ignores_absolute() -> Result<()> {
12481248
});
12491249
Ok(())
12501250
}
1251+
1252+
/// patterns are additive, can't use negative patterns to "un-ignore"
1253+
#[test]
1254+
fn negated_per_file_ignores_overlap() -> Result<()> {
1255+
let tempdir = TempDir::new()?;
1256+
let ruff_toml = tempdir.path().join("ruff.toml");
1257+
fs::write(
1258+
&ruff_toml,
1259+
r#"
1260+
[lint.per-file-ignores]
1261+
"*.py" = ["RUF"]
1262+
"!foo.py" = ["RUF"]
1263+
"#,
1264+
)?;
1265+
let foo_file = tempdir.path().join("foo.py");
1266+
fs::write(foo_file, "")?;
1267+
let bar_file = tempdir.path().join("bar.py");
1268+
fs::write(bar_file, "")?;
1269+
1270+
assert_cmd_snapshot!(Command::new(get_cargo_bin(BIN_NAME))
1271+
.args(STDIN_BASE_OPTIONS)
1272+
.arg("--config")
1273+
.arg(&ruff_toml)
1274+
.arg("--select")
1275+
.arg("RUF901")
1276+
.current_dir(&tempdir)
1277+
, @r###"
1278+
success: true
1279+
exit_code: 0
1280+
----- stdout -----
1281+
All checks passed!
1282+
1283+
----- stderr -----
1284+
"###);
1285+
Ok(())
1286+
}

crates/ruff_workspace/src/options.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -916,7 +916,7 @@ pub struct LintCommonOptions {
916916
"__init__.py" = ["E402"]
917917
"path/to/file.py" = ["E402"]
918918
# Ignore `D` rules everywhere except for the `src/` directory.
919-
"!src/**.py" = ["F401"]
919+
"!src/**.py" = ["D"]
920920
"#
921921
)]
922922
pub per_file_ignores: Option<FxHashMap<String, Vec<RuleSelector>>>,

0 commit comments

Comments
 (0)