diff --git a/xtask/src/test_lint.rs b/xtask/src/test_lint.rs index 1485cfc600..7927f48eaa 100644 --- a/xtask/src/test_lint.rs +++ b/xtask/src/test_lint.rs @@ -2264,7 +2264,10 @@ fn feeds_entry(b: &[u8], pattern: &str, from: usize, to: usize) -> bool { // Assignments in move closures leave outer bindings unchanged. Assignments in async // blocks that are not awaited, or in closures that nothing calls, do not run; // conditional assignments do not make aliases. - if in_move_closure(b, from, to, at, &name) || in_async_block(b, from, at) || !runs_with_assertion_within(b, from, to, at, &[], true) { + if in_move_closure(b, from, to, at, &name) + || in_async_block(b, from, at, &name) + || !runs_with_assertion_within(b, from, to, at, &[], true) + { continue; } if live(value_start(eq, end), end, &derived, &shadows) { @@ -2559,11 +2562,8 @@ fn runs_with_assertion_within(b: &[u8], from: usize, to: usize, start: usize, ac let prefix_at = skip_ws(b, head).min(open); let prefix = b[head..open].trim_ascii(); let nested_for = prefix.starts_with(b"for") && prefix.get(3).is_some_and(u8::is_ascii_whitespace); - // Where the `async` of an async block starts. - let async_kw = [b"async".as_slice(), b"async move"] - .iter() - .find(|kw| prefix.ends_with(kw) && !prefix[..prefix.len() - kw.len()].last().copied().is_some_and(is_ident)) - .map(|kw| prefix_at + prefix.len() - kw.len()); + // Where the `async` of an async block starts, with any whitespace before `move`. + let async_kw = async_block_keyword(b, prefix_at, open).map(|(kw, _)| kw); if prefix.ends_with(b"||") || prefix.ends_with(b"&&") { if !runs_with_assertion_within(b, from, to, open, &active, may_run) { return false; @@ -2675,11 +2675,12 @@ fn runs_with_assertion_within(b: &[u8], from: usize, to: usize, start: usize, ac if b[i] == b'=' && !matches!(b.get(i + 1), Some(b'=' | b'>')) && !matches!(b[i - 1], b'=' | b'!' | b'<' | b'>') { break; } - if !may_run - && b[i] == b'&' + if b[i] == b'&' && b[i - 1] == b'&' - && b[stmt..i - 1].trim_ascii_end().last().is_some_and(|&c| is_ident(c) || matches!(c, b')' | b']' | b'"' | b'\'' | b'?' | b'}')) - && !asserted_conjunction(b, from, to, i - 1) + && ((!may_run + && b[stmt..i - 1].trim_ascii_end().last().is_some_and(|&c| is_ident(c) || matches!(c, b')' | b']' | b'"' | b'\'' | b'?' | b'}')) + && !asserted_conjunction(b, from, to, i - 1)) + || (may_run && literal_operand(b, stmt, i - 1, b"false"))) { return false; } @@ -2692,7 +2693,8 @@ fn runs_with_assertion_within(b: &[u8], from: usize, to: usize, start: usize, ac && !asynchronous && prefix.last().is_some_and(|&c| is_ident(c) || c.is_ascii_digit() || matches!(c, b')' | b']' | b'"' | b'\'' | b'?' | b'}')) { - if may_run { + // A literal `true` always skips the right side; any other operand may let it run. + if may_run && !literal_operand(b, stmt, i - 1, b"true") { continue; } return false; @@ -2712,7 +2714,10 @@ fn runs_with_assertion_within(b: &[u8], from: usize, to: usize, start: usize, ac binding_head.trim_start().strip_suffix('=').and_then(|h| h.strip_prefix("let")).filter(|h| h.starts_with(char::is_whitespace)) else { // Other callees may invoke or retain a callback, but an unused constructor only stores it. - let may_call = may_run && !asynchronous && closure_body_end(b, end, to).is_some_and(|close| !stored_closure(b, bare, close, to)); + let may_call = may_run + && !asynchronous + && !discarding_call(bare) + && closure_body_end(b, end, to).is_some_and(|close| !stored_closure(b, bare, close, to)); // `(|| 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 may_call }; @@ -2882,10 +2887,51 @@ fn required_conjunction(b: &[u8], mut f: usize, mut t: usize, at: usize, depth: /// Whether `start` lies in the body of an `async move` block, which writes to its own copies of the /// captures, or of an `async` block that is not polled where it is written ([`polled_in_place`]). -fn in_async_block(b: &[u8], from: usize, start: usize) -> bool { - (from..start) - .filter(|&i| b[i] == b'{' && matching(b, i).is_some_and(|close| start < close)) - .any(|open| async_block_keyword(b, from, open).is_some_and(|(kw, moved)| moved || !polled_in_place(b, from, kw, open))) +/// A write to `name` that a `let` in the block before `start` declares is a write to the block's own +/// local, not to a capture. +fn in_async_block(b: &[u8], from: usize, start: usize, name: &str) -> bool { + (from..start).filter(|&i| b[i] == b'{' && matching(b, i).is_some_and(|close| start < close)).any(|open| { + async_block_keyword(b, from, open) + .is_some_and(|(kw, moved)| (moved && !declared_in(b, open + 1, start, name)) || !polled_in_place(b, from, kw, open)) + }) +} + +/// Whether a `let` in `b[body..start]` whose scope reaches `start` binds `name`. +fn declared_in(b: &[u8], body: usize, start: usize, name: &str) -> bool { + words(b, b"let", body, start).into_iter().any(|l| { + block_close(b, l) > start + && (l + 3..start) + .find(|&i| b[i] == b'=' && !matches!(b.get(i + 1), Some(b'=' | b'>')) && !matches!(b[i - 1], b'=' | b'!' | b'<' | b'>')) + .is_some_and(|eq| pattern_names(&String::from_utf8_lossy(&b[l + 3..eq])).any(|n| n == name)) + }) +} + +/// Whether the left operand of the `&&` or `||` at `op` in the statement starting at `stmt` is the +/// literal `lit` (`false && …`, `true || …`), which decides the operator without its right side. +fn literal_operand(b: &[u8], stmt: usize, op: usize, lit: &[u8]) -> bool { + let left = b[stmt..op].trim_ascii_end(); + let Some(rest) = left.strip_suffix(lit) else { return false }; + if rest.last().copied().is_some_and(is_ident) { + return false; + } + let rest = rest.trim_ascii_end(); + match rest.last() { + None | Some(b'(' | b'{' | b',' | b';') => true, + Some(b'=') => !matches!(rest.get(rest.len().wrapping_sub(2)), Some(b'=' | b'!' | b'<' | b'>')), + // `a && true || …` has `a && true` as its left operand, while `a && false && …` is still false. + Some(b'&') => lit == b"false" && rest.ends_with(b"&&"), + Some(b'|') => rest.ends_with(b"||"), + _ => false, + } +} + +/// Whether `head`, the text before a closure's parameters, ends with a call to `drop`, which +/// discards the closure without calling it. A bare `drop` is the prelude's, as in [`value_kept`]. +fn discarding_call(head: &str) -> bool { + head.strip_suffix('(').map(str::trim_end).is_some_and(|callee| { + let path_len = callee.bytes().rev().take_while(|&c| is_ident(c) || c == b':').count(); + matches!(&callee[callee.len() - path_len..], "drop" | "mem::drop" | "std::mem::drop" | "core::mem::drop") + }) } /// The position of `async` before the block at `open`, and whether it captures by move. @@ -10797,6 +10843,64 @@ mod tests { ); } + #[test] + fn census_skips_writes_that_cannot_run_and_async_block_locals() { + let loop_of = |body: &str| format!("#[test]\nfn t() {{\n let mut app = app();\n for c in COMMANDS {{\n {body}\n }}\n}}\n"); + let census = |body: &str| hits(&loop_of(body)).into_iter().filter(|h| h.1 == "census").collect::>(); + assert_eq!( + census("let _future = async /* deferred */ move { app.run(c); }; assert!(menu(c).is_some());"), + [(2, "census")], + "a comment between async and move still marks an unpolled async block" + ); + assert_eq!( + census("block_on(async move { let mut input = fallback(); input = c; app.run(input); }); assert!(menu(c).is_some());"), + [], + "a write to a local declared inside an async move block makes an alias" + ); + assert_eq!( + census( + "let mut input = c; let mut reset = || { input = fallback(); }; false && { reset(); true }; app.run(input); assert!(menu(c).is_some());" + ), + [], + "a literal false left of && never runs the reset" + ); + assert_eq!( + census( + "let mut input = c; let mut reset = || { input = fallback(); }; true || { reset(); true }; app.run(input); assert!(menu(c).is_some());" + ), + [], + "a literal true left of || never runs the reset" + ); + assert_eq!( + census( + "let mut input = c; let mut reset = || { input = fallback(); }; ready && { reset(); true }; app.run(input); assert!(menu(c).is_some());" + ), + [(2, "census")], + "an unknown left operand of && may run the reset" + ); + assert_eq!( + census( + "let mut input = c; let mut reset = || { input = fallback(); }; std::mem::drop(|| reset()); app.run(input); assert!(menu(c).is_some());" + ), + [], + "a closure passed to drop is never called" + ); + assert_eq!( + census( + "let mut input = c; let mut reset = || { input = fallback(); }; on_change(|| reset()); app.run(input); assert!(menu(c).is_some());" + ), + [(2, "census")], + "a closure passed to another callee may be called" + ); + assert_eq!( + census( + "let mut input = c; let mut reset = || { input = fallback(); }; ready && true || { reset(); true }; app.run(input); assert!(menu(c).is_some());" + ), + [(2, "census")], + "a literal true after && is not the whole left operand of ||" + ); + } + #[test] fn census_keeps_guards_around_nested_arguments() { let loop_of = |body: &str| format!("#[test]\nfn t() {{\n let mut app = app();\n for c in COMMANDS {{\n {body}\n }}\n}}\n");