xtask: test-lint census entry reads skip path segments, comment gaps and block statements

- An entry name after `::` (`defaults::c`) is a path segment, not a read of the loop binding.
- A `let` initializer scans to the end of its statement, so a block initializer binds its tail
  (`{ prepare(); c }`) and not the earlier statements in the block; `if let` and `while let`
  end at their block.
- A call keeps its name when masked comment whitespace sits before `(`, except after a keyword
  (`if (a)`).
- The argument-guard and alias tests now depend on the logic they cover.
This commit is contained in:
dotCooCoo committed 2026-12-02 03:01:11 -08:00
1 parent 35afebf9dd
commit 1eb22f6134
1 file changed
+58 -8
+58 -8
View File
@@ -1764,9 +1764,10 @@ fn feeds_entry(b: &[u8], pattern: &str, from: usize, to: usize) -> bool {
let names: Vec<String> = params.split(',').flat_map(|p| pattern_names(p.split(':').next().unwrap_or_default()).collect::<Vec<_>>()).collect();
shadows.extend(names.into_iter().map(|n| (n, at + 1, until)));
}
// A field access (`options.c`) or a field label (`Row { c: x }`) names a field, not the binding.
// A field access (`options.c`), a path segment (`defaults::c`) or a field label (`Row { c: x }`)
// names a field or an item, not the binding.
let is_field = |at: usize, len: usize| {
b[..at].trim_ascii_end().last() == Some(&b'.') || {
b[..at].trim_ascii_end().last() == Some(&b'.') || b[..at].trim_ascii_end().ends_with(b"::") || {
let next = skip_ws(b, at + len);
b.get(next) == Some(&b':') && b.get(next + 1) != Some(&b':')
}
@@ -1785,9 +1786,27 @@ fn feeds_entry(b: &[u8], pattern: &str, from: usize, to: usize) -> bool {
else {
continue;
};
let end = (eq..to).find(|&i| b[i] == b';').unwrap_or(to);
// `if let` and `while let` end their initializer at the conditional's block.
let conditional = [b"if".as_slice(), b"while"].iter().any(|kw| b[from..at].trim_ascii_end().ends_with(kw));
let end = if conditional { (eq..to).find(|&i| b[i] == b'{').unwrap_or(to) } else { statement_end(&b[..to], eq) };
// A block initializer binds the value of its tail (`{ prepare(); c }`).
let mut value = eq + 1;
let init = skip_ws(b, value);
if !conditional && b.get(init) == Some(&b'{') && matching(b, init).is_some_and(|close| b[close + 1..end].trim_ascii().is_empty()) {
let close = matching(b, init).unwrap_or(end);
let mut i = init + 1;
value = i;
while i < close {
match b[i] {
b'(' | b'[' | b'{' => i = matching(b, i).unwrap_or(close),
b';' => value = i + 1,
_ => {}
}
i += 1;
}
}
let names: Vec<String> = pattern_names(&String::from_utf8_lossy(&b[at + 3..eq])).collect();
if live(eq + 1, end, &derived, &shadows) {
if live(value, end, &derived, &shadows) {
// Rebinding from the entry suspends earlier shadows until this block ends.
let until = block_close(b, at).min(to);
let mut resumed = vec![];
@@ -1937,9 +1956,18 @@ fn numeric_literal(s: &str) -> bool {
/// Where the name of a call whose `(` is at `open` ends: right before the `(`, or before the
/// `::<…>` of a turbofish (`app.run::<Mode>(c)`). `None` when no name precedes the `(`.
fn call_name_end(b: &[u8], from: usize, open: usize) -> Option<usize> {
let before = open;
// Masked comments leave spaces between the name and the `(` (`app.run /* note */ (c)`).
let before = from + b[from..open].trim_ascii_end().len();
if before > from && is_ident(b[before - 1]) {
return Some(before);
// A keyword before a parenthesized expression (`if (a)`, `return (x)`) is not a call.
let mut start = before;
while start > from && is_ident(b[start - 1]) {
start -= 1;
}
const KEYWORDS: [&[u8]; 15] =
[b"if", b"else", b"match", b"while", b"for", b"in", b"return", b"break", b"let", b"mut", b"move", b"async", b"await", b"as", b"loop"];
let keyword = KEYWORDS.contains(&&b[start..before]);
return (before == open || !keyword).then_some(before);
}
if before == from || b[before - 1] != b'>' {
return None;
@@ -8592,7 +8620,11 @@ mod tests {
[(2, "census")],
"a nested sibling keeps the enclosing guard"
);
assert_eq!(census("assert!(ready && accept(0, app.run(c)));"), [], "an asserted enclosing conjunction requires the argument");
assert_eq!(
census("assert!(menu(c).is_some() && accept(0, app.run(c)).is_some());"),
[],
"an asserted enclosing conjunction requires the argument"
);
assert_eq!(census("let _ = accept(0, { app.run(c) }); assert!(menu(c).is_some());"), [], "an argument block runs");
assert_eq!(
census("let _run = |_: ()| app.run(c); assert!(menu(c).is_some());"),
@@ -8616,6 +8648,24 @@ mod tests {
[(2, "census")],
"a typed closure parameter shadows the entry"
);
assert_eq!(
census("app.run(defaults::c); assert!(menu(c).is_some());"),
[(2, "census")],
"a path segment named like the entry does not read it"
);
assert_eq!(census("app.run(c); assert!(menu(c).is_some());"), [], "a plain argument reads the entry");
assert_eq!(census("let input = { prepare(); c }; assert!(render(input, offset).is_ok());"), [], "a block initializer's tail binds the entry");
assert_eq!(census("assert!(app.run /* note */ (c).is_ok());"), [], "a comment before the arguments keeps the call");
assert_eq!(
census("let input = { prepare(); let _ = c; fixed }; assert!(render(input, offset).is_ok()); assert!(menu(c).is_some());"),
[(2, "census")],
"a block initializer binds its tail, not earlier statements"
);
assert_eq!(
census("if let Some(input) = fallback() { prepare(); } menu(c); assert!(render(input, offset).is_ok()); assert!(menu(c).is_some());"),
[(2, "census")],
"an if let initializer ends at its block"
);
}
#[test]
@@ -8627,7 +8677,7 @@ mod tests {
assert_eq!(census(split), [], "a chain split over lines keeps its receiver");
let rendered = "#[test]\nfn t() {\n let d = doc();\n for level in Level::ALL {\n let ps = export(&d, &Options { level, ..opts() });\n assert!(ps.contains(\"op\"));\n }\n}\n";
assert_eq!(census(rendered), [], "a file written for each entry");
let bound = "#[test]\nfn t() {\n for (_, kind) in KINDS {\n let params = json!({\"k\": kind});\n assert!(check_params(SCHEMA, &params).is_ok());\n }\n}\n";
let bound = "#[test]\nfn t() {\n for (_, kind) in KINDS {\n let params = json!({\"k\": kind});\n assert!(validate_params(SCHEMA, &params).is_ok());\n }\n}\n";
assert_eq!(census(bound), [], "a value bound from the entry, checked against a schema");
let lookup = "#[test]\nfn t() {\n let names = names();\n for c in COMMANDS {\n assert!(names.contains(c));\n }\n}\n";
assert_eq!(census(lookup), [(2, "census")], "a lookup in other state is still a census");