xtask: test-lint reads a closure body past turbofish commas

An expression closure such as `|body: fn()| forward::<u8, u16>(body)`
ended at the comma inside the turbofish, so a parameter named like an
asserting helper stopped shadowing it at that comma. The closure body
now skips a turbofish whole.

A count or emptiness getter on a pinned snapshot (`let _ = got.len();`)
keeps the pin; only a read of the receiver itself that is bound or
passed to a call may hand out an alias.
This commit is contained in:
dotCooCoo committed 2026-12-02 01:11:04 -08:00
1 parent cbd1936ae6
commit df12f897d4
1 file changed
+32 -14
+32 -14
View File
@@ -2410,8 +2410,10 @@ fn touched_after(body: &[u8], name: &str, from: usize, receiver: &str) -> bool {
|| tail.starts_with([')', ']', '}', ','])
|| [".iter()", ".len()", ".is_empty()", ".first()", ".last()", ".get(", ".contains("].iter().any(|method| tail.starts_with(method))
});
// A read of the receiver bound to another local or passed to a call may hand out an alias;
// a count or emptiness getter returns a plain value.
let read_only = assertion || binds_receiver || body_level_statement(body, at);
!((read || getter) && read_only || shared)
!(getter || (read && read_only) || shared)
})
}
@@ -4126,20 +4128,31 @@ fn closure_binding(body: &[u8], name: &str, before: usize) -> Option<usize> {
let end = match body.get(from) {
Some(b'{') => matching(body, from),
_ => {
let mut depth = 0usize;
(from..body.len()).find(|&k| match body[k] {
b'(' | b'[' | b'{' => {
depth += 1;
false
// The expression body ends at the first `,`, `;` or closing bracket outside its own
// brackets; a turbofish (`forward::<u8, u16>(body)`) is skipped whole.
let (mut depth, mut k, mut end) = (0usize, from, None);
while k < body.len() {
let next = skip_turbofish(body, k);
if next != k {
k = next;
continue;
}
b')' | b']' | b'}' if depth == 0 => true,
b')' | b']' | b'}' => {
depth -= 1;
false
match body[k] {
b'(' | b'[' | b'{' => depth += 1,
b')' | b']' | b'}' if depth == 0 => {
end = Some(k);
break;
}
b')' | b']' | b'}' => depth -= 1,
b',' | b';' if depth == 0 => {
end = Some(k);
break;
}
_ => {}
}
b',' | b';' => depth == 0,
_ => false,
})
k += 1;
}
end
}
};
if !words(params, name.as_bytes(), 0, params.len()).is_empty() && from <= before && end.is_some_and(|end| before < end) {
@@ -7902,6 +7915,11 @@ mod tests {
assert_eq!(hits_in("crates/x/src/tests.rs", src, &ctx), [(3, "no-assertion")], "a match-arm pattern shadows the helper");
let after = src.replace("None => () }", "None => () }\n keeping(body);");
assert_eq!(hits_in("crates/x/src/tests.rs", &after, &ctx), [], "the helper after the match");
let turbofish = src.replace(
"match Some(|| {}) { Some(body) => keeping(body), None => () }",
"let cb = |body: fn()| forward::<u8, u16>(body);\n cb(|| {});",
);
assert_eq!(hits_in("crates/x/src/tests.rs", &turbofish, &ctx), [(3, "no-assertion")], "a turbofish comma does not end the closure");
}
#[test]
@@ -8002,7 +8020,7 @@ mod tests {
assert_eq!(hits(&reassigned), [(6, "all-unguarded")], "the snapshot was replaced before the assertion");
let pushed = pinned.replace(" assert_eq!(got", " got.push(1);\n assert_eq!(got");
assert_eq!(hits(&pushed), [(6, "all-unguarded")], "the snapshot changed before the assertion");
let read_first = pinned.replace(" assert_eq!(got", " assert!(got.len() < 9);\n assert_eq!(got");
let read_first = pinned.replace(" assert_eq!(got", " let _ = got.len();\n assert_eq!(got");
assert_eq!(hits(&read_first), [], "a read of the snapshot before the assertion");
let word = "#[test]\nfn t() {\n let plain = word();\n assert_eq!(gaps(&plain), [0.0, 0.0]);\n let gaps = gaps(&plain);\n assert!(gaps.iter().all(|g| g.abs() < 1e-6));\n}\n";
assert_eq!(hits(word), []);