xtask: test-lint census rule reads let( bindings, non-ASCII labels and grouped block_on futures

- A `let` followed directly by `(`, `[` or `&` (`let(_run) = || { ... };`)
  now binds the closure, and Unicode whitespace after `let` separates the
  pattern.
- A loop label with non-ASCII letters (`'été: for _ in 0..0`) is stripped,
  so an empty range still skips the body.
- `block_on((run()))` counts the future as run: grouping parentheses
  around it are skipped. A callee whose name only ends in `block_on`
  (`déblock_on`) still does not count, and Unicode whitespace before
  `block_on` does not join it to the name before.
This commit is contained in:
dotCooCoo committed 2026-12-02 12:55:32 -08:00
1 parent 8cea13d8a4
commit 38f198bb22
1 file changed
+65 -16
+65 -16
View File
@@ -2459,17 +2459,31 @@ fn integer_literal(s: &str) -> Option<i128> {
i128::from_str_radix(&body.replace('_', ""), radix).ok().map(|n| sign * n)
}
/// The text after the last `let` keyword in `head` that a binding follows (`let x`, not the `let`
/// inside a name such as `inlet_fn`).
/// The text after the last `let` keyword in `head` that a binding follows (`let x`, `let(x)`,
/// `let[x]`, `let&x`, not the `let` inside a name such as `inlet_fn`).
fn after_let(head: &str) -> Option<&str> {
let b = head.as_bytes();
head.match_indices("let")
.map(|(at, _)| at)
.filter(|&at| (at == 0 || !is_ident(b[at - 1])) && b.get(at + 3).is_some_and(u8::is_ascii_whitespace))
.filter(|&at| (at == 0 || !is_ident(b[at - 1])) && opens_let_pattern(&head[at + 3..]))
.last()
.map(|at| &head[at + 3..])
}
/// Whether `rest`, the text right after a `let` keyword, starts its pattern: whitespace, or a
/// tuple, slice or reference pattern written without a space (`let(x)`, `let[x]`, `let&x`).
fn opens_let_pattern(rest: &str) -> bool {
rest.starts_with(|c: char| c.is_whitespace() || matches!(c, '(' | '[' | '&'))
}
/// Whether `head`, the text before a call's opening parenthesis, ends with the name `block_on`
/// itself, not a longer name that ends with it (`discard_block_on`, `déblock_on`).
fn ends_with_block_on(head: &[u8]) -> bool {
head.trim_ascii_end().strip_suffix(b"block_on").is_some_and(|h| {
!String::from_utf8_lossy(h).chars().next_back().is_some_and(|c| c.is_alphanumeric() || c == '_' || !(c.is_ascii() || c.is_whitespace()))
})
}
/// Whether the source of the `for … in <source>` head `head` yields at least one item, for an
/// integer range with literal bounds (`0..3` and `0..=0` do, `0..0` and `5..=4` do not) or an array
/// literal (`[a, b]` and `[x; 2]` do, `[]` and `[x; 0]` do not). `None` for any other source.
@@ -2578,10 +2592,10 @@ fn runs_with_assertion_within(b: &[u8], from: usize, to: usize, start: usize, ac
}
}
let prefix = b[head..open].trim_ascii();
// A loop label (`'outer: for …`) comes before the keyword.
// A loop label (`'outer: for …`, `'été: for …`) comes before the keyword.
let prefix = prefix
.strip_prefix(b"'")
.map(|rest| rest.split_at(rest.iter().take_while(|&&c| is_ident(c)).count()))
.map(|rest| rest.split_at(rest.iter().take_while(|&&c| is_ident(c) || !c.is_ascii()).count()))
.and_then(|(label, rest)| rest.trim_ascii_start().strip_prefix(b":").filter(|r| !label.is_empty() && r.first() != Some(&b':')))
.map_or(prefix, <[u8]>::trim_ascii_start);
let nested_for = prefix.starts_with(b"for") && prefix.get(3).is_some_and(u8::is_ascii_whitespace);
@@ -2655,7 +2669,7 @@ fn runs_with_assertion_within(b: &[u8], from: usize, to: usize, start: usize, ac
}
let head = String::from_utf8_lossy(b[stmt..pipe].trim_ascii());
let head = strip_closure_keywords(&head).0.as_bytes();
if !head.ends_with(b"=") || !head.starts_with(b"let") || !head.get(3).is_some_and(u8::is_ascii_whitespace) {
if !head.ends_with(b"=") || !head.starts_with(b"let") || !std::str::from_utf8(&head[3..]).is_ok_and(opens_let_pattern) {
return None;
}
(pipe + 1..start).find(|&end| b[end] == b'|').map(|end| (pipe, end))
@@ -2725,9 +2739,7 @@ fn runs_with_assertion_within(b: &[u8], from: usize, to: usize, start: usize, ac
inner_close.filter(|&close| b.get(skip_ws(b, close + 1)) == Some(&b';')).map(|_| head)
})();
let binding_head = unwrapped.unwrap_or(bare);
let Some(binding) =
binding_head.trim_start().strip_suffix('=').and_then(|h| h.strip_prefix("let")).filter(|h| h.starts_with(char::is_whitespace))
else {
let Some(binding) = binding_head.trim_start().strip_suffix('=').and_then(|h| h.strip_prefix("let")).filter(|h| opens_let_pattern(h)) else {
// `(|| app.run(c))()` calls the closure where it is written.
let open = stmt + bare.len();
let Some(close) = open.checked_sub(1).filter(|&o| b[o] == b'(').and_then(|o| matching(b, o)) else { return false };
@@ -2743,7 +2755,7 @@ fn runs_with_assertion_within(b: &[u8], from: usize, to: usize, start: usize, ac
outer.is_some_and(|o| {
b[o] == b'('
&& !b[stmt..o].contains(&b'|')
&& b[..o].trim_ascii_end().strip_suffix(b"block_on").is_some_and(|h| !h.last().copied().is_some_and(is_ident))
&& ends_with_block_on(&b[..o])
&& matching(b, o).is_some_and(|c| matches!(b[call_close + 1..c].trim_ascii(), b"" | b","))
})
});
@@ -2803,11 +2815,22 @@ fn closure_calls(b: &[u8], name: &str, bound: usize, after: usize, to: usize, as
let awaited = b[next..].starts_with(b".await") && !b.get(next + 6).copied().is_some_and(is_ident);
// The future is the whole argument of `block_on` itself, not of a name that ends with it
// (`discard_block_on`), and not part of a longer expression that may never poll it.
let paren = b[..at].trim_ascii_end().len().checked_sub(1).filter(|&p| b[p] == b'(');
let blocked = paren.is_some_and(|p| {
b[..p].trim_ascii_end().strip_suffix(b"block_on").is_some_and(|h| !h.last().copied().is_some_and(is_ident))
&& matching(b, p).is_some_and(|c| matches!(b[close + 1..c].trim_ascii(), b"" | b","))
});
// Grouping parentheses around the future (`block_on((run()))`) are skipped.
let opening = |end: usize| b[..end].trim_ascii_end().len().checked_sub(1).filter(|&p| b[p] == b'(');
let mut paren = opening(at);
let mut inner = close;
let blocked = loop {
let Some((p, c)) = paren.and_then(|p| matching(b, p).map(|c| (p, c))) else { break false };
let rest = b[inner + 1..c].trim_ascii();
if ends_with_block_on(&b[..p]) {
break matches!(rest, b"" | b",");
}
if !rest.is_empty() {
break false;
}
paren = opening(p);
inner = c;
};
awaited || blocked
})
.collect()
@@ -2942,7 +2965,7 @@ fn stored_closure(b: &[u8], head: &str, close: usize, to: usize) -> bool {
let paren = if b.get(after) == Some(&b',') { skip_ws(b, after + 1) } else { after };
let semi = skip_ws(b, paren + 1);
constructor
&& binding.starts_with(char::is_whitespace)
&& opens_let_pattern(binding)
&& b.get(paren) == Some(&b')')
&& b.get(semi) == Some(&b';')
&& pattern_names(binding).all(|n| words(b, n.as_bytes(), semi, to).is_empty())
@@ -10365,6 +10388,32 @@ mod tests {
[(2, "census")],
"a label with a space before its colon"
);
assert_eq!(census("'été: for _ in 0..0 { app.run(c); } assert!(menu(c).is_some());"), [(2, "census")], "a label with non-ASCII letters");
assert_eq!(
census("let(_run) = || { app.run(c); }; _run(); assert!(menu(c).is_some());"),
[],
"a let with no space before its pattern binds a closure that is called"
);
assert_eq!(
census("let run = async || app.run(c); block_on((run())); assert!(menu(c).is_some());"),
[],
"a future in grouping parentheses passed to block_on runs"
);
assert_eq!(
census("let run = async || app.run(c); déblock_on((run())); discard‿block_on((run())); assert!(menu(c).is_some());"),
[(2, "census")],
"a callee whose non-ASCII name ends in block_on does not run the future"
);
assert_eq!(
census("let\u{85}run = || app.run(c); run(); assert!(menu(c).is_some());"),
[],
"Unicode whitespace after let separates the pattern"
);
assert_eq!(
census("let run = async || app.run(c);\u{85}block_on(run()); assert!(menu(c).is_some());"),
[],
"Unicode whitespace before block_on"
);
assert_eq!(
census("let run = || app.run(c); other::run(); assert!(menu(c).is_some());"),
[(2, "census")],