xtask: test-lint census skips stringified helpers and reads struct closure params

An asserting helper inside `stringify!` is not evaluated, so it no longer
counts as a check that runs for each entry. Closure parameter lists may now
hold struct patterns (`|Row { c }|`), so a parameter that shadows the entry
is seen. A path segment in a pattern (`entry::Row { .. }`) does not bind a
name.
This commit is contained in:
dotCooCoo committed 2026-12-03 07:10:52 -08:00
1 parent 6ec484819a
commit cb837fa47a
1 file changed
+99 -9
+99 -9
View File
@@ -2052,10 +2052,16 @@ fn census(v: &View, helpers: &HashSet<String>) -> Vec<Hit> {
// Failure messages run only after an assertion has failed, so a check there does not count.
let unmessaged = v.body_without_messages();
let helpers_in = |from: usize, to: usize| -> Vec<String> {
calls(&unmessaged, from - v.t.open, to - v.t.open)
let (f, t) = (from - v.t.open, to - v.t.open);
let unevaluated = stringify_args(&unmessaged, f, t);
calls_at(&unmessaged, f, t)
.into_iter()
.filter(|(n, method)| !(n.ends_with('!') && is_assert_macro(n)) && is_helper_call(n, *method, helpers))
.map(|(n, _)| n)
.filter(|(at, n, method)| {
!(n.ends_with('!') && is_assert_macro(n))
&& is_helper_call(n, *method, helpers)
&& !unevaluated.iter().any(|&(o, c)| o < *at && *at < c)
})
.map(|(_, n, _)| n)
.collect()
};
let weak_in = |from: usize, to: usize, var: &str| {
@@ -2129,7 +2135,9 @@ fn pattern_names(pattern: &str) -> impl Iterator<Item = String> + '_ {
// Inside braces, `name:` labels a field; outside them it starts a type (`let x: u32`).
let in_braces = b[..at].iter().filter(|&&c| c == b'{').count() > b[..at].iter().filter(|&&c| c == b'}').count();
let next = end + b[end..].iter().take_while(|c| c.is_ascii_whitespace()).count();
!in_braces || b.get(next) != Some(&b':') || b.get(next + 1) == Some(&b':')
// A path segment (`entry::Row { .. }`, `Kind::empty`) names a module or an item.
let path = b[next..].starts_with(b"::") || pattern[..at].trim_end().ends_with("::");
!path && (!in_braces || b.get(next) != Some(&b':') || b.get(next + 1) == Some(&b':'))
})
.map(move |(at, end)| &pattern[at..end])
.filter(|w| !matches!(*w, "" | "_" | "mut" | "ref") && w.starts_with(|c: char| c.is_ascii_lowercase() || c == '_'))
@@ -2180,7 +2188,7 @@ fn feeds_entry(b: &[u8], pattern: &str, from: usize, to: usize) -> bool {
}
// Each parameter binds the names before its type.
let params = String::from_utf8_lossy(&b[at + 1..end]).into_owned();
let names: Vec<String> = params.split(',').flat_map(|p| pattern_names(p.split(':').next().unwrap_or_default()).collect::<Vec<_>>()).collect();
let names: Vec<String> = closure_param_patterns(&params).into_iter().flat_map(|p| pattern_names(p).collect::<Vec<_>>()).collect();
shadows.extend(names.into_iter().map(|n| (n, at + 1, until)));
}
// The scopes of nested loop patterns and closure parameters, where an assignment to the name
@@ -2544,6 +2552,23 @@ fn runs_with_assertion(b: &[u8], from: usize, to: usize, start: usize) -> bool {
runs_with_assertion_within(b, from, to, start, &[], false)
}
/// Whether `at` lies in the arguments of a `stringify!` in `b[from..to]`, which are not evaluated.
fn in_stringify(b: &[u8], from: usize, to: usize, at: usize) -> bool {
stringify_args(b, from, to).iter().any(|&(open, close)| open < at && at < close)
}
/// The brackets around the arguments of each `stringify!` in `b[from..to]`.
fn stringify_args(b: &[u8], from: usize, to: usize) -> Vec<(usize, usize)> {
calls_at(b, from, to)
.into_iter()
.filter(|(_, name, _)| name == "stringify!")
.filter_map(|(p, name, _)| {
let open = skip_ws(b, p + name.len());
matching(b, open).map(|close| (open, close))
})
.collect()
}
/// Checks whether `start` runs whenever assertions pass, or may run at all when `may_run` is true.
/// `active` holds positions being checked so recursive closure calls terminate.
fn runs_with_assertion_within(b: &[u8], from: usize, to: usize, start: usize, active: &[usize], may_run: bool) -> bool {
@@ -2552,7 +2577,7 @@ fn runs_with_assertion_within(b: &[u8], from: usize, to: usize, start: usize, ac
}
let mut active = active.to_vec();
active.push(start);
if in_uncalled_closure(b, from, to, start, &active, may_run) {
if in_stringify(b, from, to, start) || in_uncalled_closure(b, from, to, start, &active, may_run) {
return false;
}
// Check enclosing blocks independently of the call's statement: an earlier semicolon inside
@@ -2812,10 +2837,54 @@ fn parenthesized_closure_binding_head<'a>(b: &[u8], stmt: usize, start: usize, m
/// The `|` that closes the closure parameter list opened by the `|` at `pipe`, before `to`, when the
/// text between them can be a parameter list. Parameters may carry types (`|c: Command|`,
/// `|p: &[u8; 4]|`, `|f: fn() -> bool|`), but not a block, a string, a method call or an assignment.
/// `|p: &[u8; 4]|`, `|f: fn() -> bool|`) and struct patterns (`|Row { c, .. }|`), but not a block,
/// a string, a method call or an assignment.
fn closure_params_end(b: &[u8], pipe: usize, to: usize) -> Option<usize> {
let end = (pipe + 1..to).find(|&i| b[i] == b'|')?;
(!b[pipe + 1..end].iter().any(|c| matches!(c, b'{' | b'}' | b'"' | b'.' | b'='))).then_some(end)
let mut i = pipe + 1;
loop {
match b[..to].get(i)? {
b'|' => break Some(i),
b'"' | b'.' | b'=' | b'}' => break None,
b'{' => {
// A struct pattern follows its path (`Row {`); its braces hold names, labels and `..`.
let named = b[pipe + 1..i].trim_ascii_end().last().copied().is_some_and(is_ident);
let close = matching(b, i).filter(|&c| c < to && named)?;
if b[i + 1..close].iter().any(|c| matches!(c, b'"' | b'=' | b';' | b'{' | b'}')) {
break None;
}
i = close + 1;
}
_ => i += 1,
}
}
}
/// The patterns of a closure parameter list `params` without their types: the text of each
/// top-level parameter before its `:` (`c` in `c: Command`, `Row { c: other }` in `Row { c: other }: Row`).
fn closure_param_patterns(params: &str) -> Vec<&str> {
let b = params.as_bytes();
let (mut out, mut depth, mut start, mut typed) = (vec![], 0usize, 0, false);
for (i, &c) in b.iter().enumerate() {
match c {
b'(' | b'[' | b'{' | b'<' => depth += 1,
b')' | b']' | b'}' | b'>' => depth = depth.saturating_sub(1),
b':' if depth == 0 && !typed && b.get(i + 1) != Some(&b':') && (i == 0 || b[i - 1] != b':') => {
out.push(&params[start..i]);
typed = true;
}
b',' if depth == 0 => {
if !typed {
out.push(&params[start..i]);
}
(start, typed) = (i + 1, false);
}
_ => {}
}
}
if !typed {
out.push(&params[start..]);
}
out
}
/// The head before a closure's parameters with a trailing `move` and `async` removed, and whether
@@ -11199,6 +11268,27 @@ mod tests {
[(2, "census")],
"a call inside stringify! does not run, so it does not feed the entry"
);
assert_eq!(
census("let _ = stringify!(verify_entry(c)); assert!(menu(c).is_some());"),
[(2, "census")],
"an asserting helper inside stringify! does not run"
);
assert_eq!(census("verify_entry(c); assert!(menu(c).is_some());"), [], "an asserting helper that runs checks each entry");
assert_eq!(
census("let run = |Row { c }| { render(c, offset); }; run(fallback()); assert!(menu(c).is_some());"),
[(2, "census")],
"a struct pattern in closure parameters shadows the entry"
);
assert_eq!(
census("let run = |Row { c: other }| { render(c, other); }; run(fallback()); assert!(menu(c).is_some());"),
[],
"a field label in a closure's struct pattern binds the name after it, not the entry"
);
assert_eq!(
census("let run = |c::Row { offset }| { render(c, offset); }; run(fallback()); assert!(menu(c).is_some());"),
[],
"a path segment in a closure's struct pattern does not bind the name"
);
assert_eq!(
census(
"let mut input = c; let mut reset = || { input = fallback(); }; false && ready || { reset(); true }; app.run(input); assert!(menu(c).is_some());"