diff --git a/xtask/src/test_lint.rs b/xtask/src/test_lint.rs index f6b8a6e2b1..ec88e985fd 100644 --- a/xtask/src/test_lint.rs +++ b/xtask/src/test_lint.rs @@ -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 { 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::(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::(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), []);