use std::path::Path;
pub const FORBIDDEN: &[&str] = &[
"thread::spawn",
"thread::scope",
"thread::Builder",
"thread::sleep",
"thread::park",
"thread::yield_now",
"std::thread",
"thread::",
"thread as",
"spawn(",
"sleep(",
"park(",
"park_timeout(",
"yield_now(",
"scope(",
"spin_loop",
"Condvar",
"Barrier",
"mpsc",
"rayon",
".wait(",
".wait_timeout",
".recv(",
];
const ALLOWED_THREAD: &str = "thread::available_parallelism";
pub const CELL_FILLS: &[&str] = &[
"get_or_init",
"get_or_try_init",
"get_mut_or_init",
"LazyLock",
"LazyCell",
"call_once",
"OnceCell",
];
const ONCE_FILE: &str = "once.rs";
#[must_use]
pub fn waits(src: &Path) -> Vec<String> {
scan_tree(src, |_| true, FORBIDDEN)
}
#[must_use]
pub fn cell_fills(src: &Path) -> Vec<String> {
scan_tree(src, |rel| rel != Path::new(ONCE_FILE), CELL_FILLS)
}
fn scan_tree(src: &Path, keep: impl Fn(&Path) -> bool, tokens: &[&str]) -> Vec<String> {
let mut files = Vec::new();
collect(src, &mut files);
files.sort();
let mut found = Vec::new();
for (name, text) in &files {
let path = Path::new(name);
if keep(path.strip_prefix(src).unwrap_or(path)) {
scan(name, text, tokens, &mut found);
}
}
found
}
fn collect(dir: &Path, out: &mut Vec<(String, String)>) {
for entry in std::fs::read_dir(dir).unwrap_or_else(|e| panic!("{}: {e}", dir.display())) {
let path = entry
.unwrap_or_else(|e| panic!("{}: {e}", dir.display()))
.path();
if path.is_dir() {
collect(&path, out);
} else if path.extension().is_some_and(|e| e == "rs") {
let text = std::fs::read_to_string(&path)
.unwrap_or_else(|e| panic!("{}: {e}", path.display()));
out.push((path.display().to_string(), text));
}
}
}
fn scan(name: &str, text: &str, tokens: &[&str], found: &mut Vec<String>) {
let code = live_text(text);
for (i, line) in code.lines().enumerate() {
let line = line.replace(ALLOWED_THREAD, "");
if let Some(token) = tokens.iter().find(|t| names(&line, t)) {
found.push(format!("{name}:{}: {token}", i + 1));
}
}
}
fn names(line: &str, token: &str) -> bool {
let ident = |c: u8| c.is_ascii_alphanumeric() || c == b'_';
let word = token.bytes().next().is_some_and(ident);
let (stem, call) = match token.strip_suffix('(') {
Some(stem) => (stem, true),
None => (token, false),
};
line.match_indices(stem).any(|(at, _)| {
let starts = !word || at == 0 || !ident(line.as_bytes()[at - 1]);
starts && (!call || opens_call(&line[at + stem.len()..]))
})
}
fn opens_call(rest: &str) -> bool {
let mut rest = rest.trim_start();
if let Some(generics) = rest.strip_prefix("::") {
let generics = generics.trim_start();
if !generics.starts_with('<') {
return false;
}
let mut depth = 0usize;
let mut end = None;
for (i, c) in generics.char_indices() {
match c {
'<' => depth += 1,
'>' => {
depth -= 1;
if depth == 0 {
end = Some(i + 1);
break;
}
}
_ => {}
}
}
let Some(end) = end else {
return true;
};
rest = generics[end..].trim_start();
}
rest.starts_with('(')
}
#[derive(Clone, Copy, PartialEq, Eq)]
enum Class {
Code,
Literal,
Comment,
}
#[must_use]
pub fn live_code(text: &str) -> String {
let live = live_text(text);
let class = classify(live.as_bytes());
let kept: Vec<u8> = live
.bytes()
.zip(class)
.map(|(b, c)| {
if c == Class::Literal && b != b'\n' {
b' '
} else {
b
}
})
.collect();
String::from_utf8_lossy(&kept).into_owned()
}
fn live_text(text: &str) -> String {
let bytes = text.as_bytes();
let class = classify(bytes);
let mut dead: Vec<bool> = class.iter().map(|c| *c == Class::Comment).collect();
let attr = b"#[cfg(test)]";
let mut i = 0;
while i < bytes.len() {
if class[i] == Class::Code && bytes[i..].starts_with(attr) {
let end = test_item_end(bytes, &class, i + attr.len());
for d in &mut dead[i..end] {
*d = true;
}
i = end;
} else {
i += 1;
}
}
let kept: Vec<u8> = bytes
.iter()
.zip(&dead)
.map(|(b, d)| if *d && *b != b'\n' { b' ' } else { *b })
.collect();
String::from_utf8_lossy(&kept).into_owned()
}
fn classify(b: &[u8]) -> Vec<Class> {
let mut class = vec![Class::Code; b.len()];
let mark = |class: &mut Vec<Class>, from: usize, to: usize, c: Class| {
for x in &mut class[from..to.min(b.len())] {
*x = c;
}
};
let mut i = 0;
while i < b.len() {
let rest = &b[i..];
let ident_before = i > 0 && (b[i - 1].is_ascii_alphanumeric() || b[i - 1] == b'_');
if rest.starts_with(b"//") {
let end = rest
.iter()
.position(|c| *c == b'\n')
.map_or(b.len(), |n| i + n);
mark(&mut class, i, end, Class::Comment);
i = end;
} else if rest.starts_with(b"/*") {
let end = block_comment_end(b, i);
mark(&mut class, i, end, Class::Comment);
i = end;
} else if let Some((open, hashes)) = raw_string_start(rest).filter(|_| !ident_before) {
let mut close = vec![b'"'];
close.extend(std::iter::repeat_n(b'#', hashes));
let body = i + open;
let end = b[body..]
.windows(close.len())
.position(|w| w == close.as_slice())
.map_or(b.len(), |n| body + n + close.len());
mark(&mut class, i, end, Class::Literal);
i = end;
} else if b[i] == b'"' {
let mut j = i + 1;
while j < b.len() && b[j] != b'"' {
j += if b[j] == b'\\' { 2 } else { 1 };
}
let end = (j + 1).min(b.len());
mark(&mut class, i, end, Class::Literal);
i = end;
} else if b[i] == b'\'' {
if let Some(n) = char_literal_len(rest) {
mark(&mut class, i, i + n, Class::Literal);
i += n;
} else {
i += 1; }
} else {
i += 1;
}
}
class
}
fn block_comment_end(b: &[u8], from: usize) -> usize {
let mut depth = 0;
let mut j = from;
while j < b.len() {
if b[j..].starts_with(b"/*") {
depth += 1;
j += 2;
} else if b[j..].starts_with(b"*/") {
depth -= 1;
j += 2;
if depth == 0 {
return j;
}
} else {
j += 1;
}
}
b.len()
}
fn raw_string_start(rest: &[u8]) -> Option<(usize, usize)> {
let after_b = usize::from(matches!(rest.first(), Some(b'b' | b'c')));
if rest.get(after_b) != Some(&b'r') {
return None;
}
let hashes = rest[after_b + 1..]
.iter()
.take_while(|c| **c == b'#')
.count();
let quote = after_b + 1 + hashes;
(rest.get(quote) == Some(&b'"')).then_some((quote + 1, hashes))
}
fn char_literal_len(rest: &[u8]) -> Option<usize> {
if rest.get(1) == Some(&b'\\') {
return rest
.iter()
.skip(3)
.take(10)
.position(|c| *c == b'\'')
.map(|n| n + 4);
}
let lead = *rest.get(1)?;
let width = match lead {
0x00..=0x7f => 1,
0xc0..=0xdf => 2,
0xe0..=0xef => 3,
_ => 4,
};
(rest.get(1 + width) == Some(&b'\'')).then_some(width + 2)
}
const ITEM_WORDS: &[&str] = &[
"fn",
"mod",
"impl",
"use",
"struct",
"enum",
"union",
"trait",
"type",
"const",
"static",
"extern",
"unsafe",
"async",
"macro_rules!",
"thread_local!",
];
fn test_item_end(b: &[u8], class: &[Class], from: usize) -> usize {
let item = starts_item(b, class, from);
let mut depth: i32 = 0;
for i in from..b.len() {
if class[i] != Class::Code {
continue;
}
match b[i] {
b'(' | b'[' | b'{' => depth += 1,
b')' | b']' | b'}' => {
depth -= 1;
if depth < 0 {
return i;
}
if depth == 0 && b[i] == b'}' {
return i + 1;
}
}
b';' if depth == 0 => return i + 1,
b',' if depth == 0 && !item => return i + 1,
_ => {}
}
}
b.len()
}
fn starts_item(b: &[u8], class: &[Class], from: usize) -> bool {
let mut i = from;
loop {
while i < b.len() && (b[i].is_ascii_whitespace() || class[i] == Class::Comment) {
i += 1;
}
if b[i..].starts_with(b"#[") {
let mut depth = 0;
while i < b.len() {
if class[i] == Class::Code {
if b[i] == b'[' {
depth += 1;
} else if b[i] == b']' {
depth -= 1;
if depth == 0 {
i += 1;
break;
}
}
}
i += 1;
}
continue;
}
if b[i..].starts_with(b"pub") {
i += 3;
while i < b.len() && b[i].is_ascii_whitespace() {
i += 1;
}
if b.get(i) == Some(&b'(') {
i += b[i..].iter().position(|c| *c == b')').map_or(0, |n| n + 1);
}
continue;
}
break;
}
let end = b[i..]
.iter()
.position(|c| !(c.is_ascii_alphanumeric() || *c == b'_' || *c == b'!'))
.map_or(b.len(), |n| i + n);
let word = &b[i..end];
ITEM_WORDS.iter().any(|w| w.as_bytes() == word)
}
#[cfg(test)]
mod tests {
use super::{CELL_FILLS, FORBIDDEN, scan};
fn run(text: &str) -> Vec<String> {
let mut found = Vec::new();
scan("f.rs", text, FORBIDDEN, &mut found);
found
}
#[test]
fn code_outside_tests_is_flagged() {
assert_eq!(
run("fn go() {\n std::thread::spawn(|| {});\n cv.wait(g);\n}\n"),
["f.rs:2: thread::spawn", "f.rs:3: .wait("]
);
}
#[test]
fn test_items_and_comments_are_not() {
let text = "\
// std::thread::sleep in a comment\n\
#[cfg(test)]\n\
thread_local! {\n static X: u8 = 0;\n}\n\
#[cfg(test)]\n\
use std::sync::Barrier;\n\
fn real() {\n #[cfg(test)]\n COUNT.with(|n| n.set(1));\n let x = rx.recv();\n}\n\
#[cfg(test)]\n\
mod tests {\n fn t() {\n std::thread::spawn(|| {});\n }\n}\n\
fn after() { std::thread::sleep(d); }\n";
assert_eq!(run(text), ["f.rs:11: .recv(", "f.rs:19: thread::sleep"]);
}
#[test]
fn a_test_field_arm_or_literal_field_skips_only_itself() {
let arm = "fn go(k: Kind) -> u8 {\n match k {\n #[cfg(test)]\n Kind::Probe => 0,\n Kind::Real => {\n std::thread::sleep(D);\n 1\n }\n }\n}\n";
assert_eq!(run(arm), ["f.rs:6: thread::sleep"]);
let literal = "fn make() -> Pool {\n Pool {\n #[cfg(test)]\n probes: 0,\n worker: std::thread::spawn(move || run()),\n }\n}\n";
assert_eq!(run(literal), ["f.rs:5: thread::spawn"]);
let last_field =
"struct S {\n #[cfg(test)]\n probe: u8\n}\nfn go() { std::thread::park(); }\n";
assert_eq!(run(last_field), ["f.rs:5: thread::park"]);
let block_arm = "fn go(k: Kind) {\n match k {\n #[cfg(test)]\n Kind::Probe => { let s = \"}\"; },\n Kind::Real => std::thread::yield_now(),\n }\n}\n";
assert_eq!(run(block_arm), ["f.rs:5: thread::yield_now"]);
let where_fn = "#[cfg(test)]\nfn t<A, B>() where A: X, B: Y {\n std::thread::spawn(|| {});\n}\nfn go() { rx.recv(); }\n";
assert_eq!(run(where_fn), ["f.rs:5: .recv("]);
}
#[test]
fn grouped_and_aliased_thread_imports_and_spins_are_flagged() {
assert_eq!(
run(
"use std::thread::{sleep, spawn};\nfn go() {\n spawn(|| {});\n sleep(D);\n}\n"
),
["f.rs:1: std::thread", "f.rs:3: spawn(", "f.rs:4: sleep("]
);
assert_eq!(
run("use std::thread as t;\nfn go() {\n t::spawn(|| {});\n}\n"),
["f.rs:1: std::thread", "f.rs:3: spawn("]
);
assert_eq!(
run("use std::{sync::Arc, thread as t};\n"),
["f.rs:1: thread as"]
);
assert_eq!(
run(
"fn go(f: &AtomicBool) {\n while !f.load(Acquire) {\n std::thread::yield_now();\n }\n}\n"
),
["f.rs:3: thread::yield_now"]
);
let none: [&str; 0] = [];
assert_eq!(
run(
"fn n() -> usize { std::thread::available_parallelism().map_or(1, usize::from) }\n"
),
none
);
assert_eq!(
run("fn go() { registry_scope(x); spark(y); my_spawn(z); }\n"),
none
);
}
#[test]
fn calls_with_a_turbofish_or_spaces_and_spin_hints_are_flagged() {
assert_eq!(
run(
"use std::hint;\nfn go() {\n spawn::<F, T>(f);\n sleep (D);\n hint::spin_loop();\n rx.recv::<>();\n x.wait :: < Vec<u8> > (g);\n}\n"
),
[
"f.rs:3: spawn(",
"f.rs:4: sleep(",
"f.rs:5: spin_loop",
"f.rs:6: .recv(",
"f.rs:7: .wait(",
]
);
assert_eq!(
run("fn go() {\n core::sync::atomic::spin_loop_hint();\n}\n"),
["f.rs:2: spin_loop"]
);
let none: [&str; 0] = [];
assert_eq!(
run("fn go() { spawned(x); let s = sleep_ms; my_sleep::<u8>(1); }\n"),
none
);
}
#[test]
fn a_cell_filled_by_turbofish_path_or_force_is_flagged() {
let mut found = Vec::new();
let text = "fn a(&self) -> &u8 {\n self.cell.get_or_init::<_>(|| 1)\n}\nfn b(&self) -> &u8 {\n self.cell.get_or_try_init::<_, E>(|| Ok(1)).unwrap()\n}\nfn c(o: &Once) {\n o.call_once_force(|_| ());\n}\nfn d(c: &OnceLock<u8>) -> &u8 {\n let f = OnceLock::get_or_init;\n f(c, || 1)\n}\n";
scan("f.rs", text, CELL_FILLS, &mut found);
assert_eq!(
found,
[
"f.rs:2: get_or_init",
"f.rs:5: get_or_try_init",
"f.rs:8: call_once",
"f.rs:11: get_or_init",
]
);
}
#[test]
fn only_the_crates_own_once_rs_is_exempt_from_the_cell_scan() {
let scratch = crate::scratch::Scratch::new("waits");
let dir = scratch.to_path_buf();
let fill = "pub fn f(c: &std::sync::OnceLock<u8>) -> &u8 {\n c.get_or_init(|| 1)\n}\n";
std::fs::create_dir_all(dir.join("env")).unwrap();
std::fs::create_dir_all(dir.join("files")).unwrap();
for f in ["once.rs", "env/once.rs", "files/bonce.rs", "lib.rs"] {
std::fs::write(dir.join(f), fill).unwrap();
}
let found = super::cell_fills(&dir);
let rel: Vec<String> = found
.iter()
.map(|f| {
let f = f.strip_prefix(&dir.display().to_string()).unwrap();
f.trim_start_matches(std::path::MAIN_SEPARATOR).to_owned()
})
.collect();
let sep = std::path::MAIN_SEPARATOR;
assert_eq!(
rel,
[
format!("env{sep}once.rs:2: get_or_init"),
format!("files{sep}bonce.rs:2: get_or_init"),
"lib.rs:2: get_or_init".to_owned(),
]
);
}
#[test]
fn a_cell_filled_with_its_value_inside_is_flagged_and_its_helper_is_not() {
let mut found = Vec::new();
let text = "fn entry(&self) -> &File {\n self.cell.get_or_init(|| parse(self))\n}\nstatic T: LazyLock<u8> = LazyLock::new(|| 1);\n#[cfg(test)]\nmod tests { fn t() { c.get_or_init(|| 1); } }\n";
scan("f.rs", text, CELL_FILLS, &mut found);
assert_eq!(found, ["f.rs:2: get_or_init", "f.rs:4: LazyLock"]);
}
#[test]
fn live_code_blanks_each_literal_and_keeps_the_code_after_it() {
let text = concat!(
"let a = \"{\\\"}\";\n",
"let b = r#\"{\"}\"#;\n",
"let c = cr#\"a\"b{\"#;\n",
"let d = c\"{\\\"}\";\n",
"let e = br\"}\";\n",
"let f = '\\'';\n",
"let g = b'{';\n",
"let h = b'\\'';\n",
"let i = '\\u{7B}';\n",
"/* /* nested } */ { */\n",
"fn live<'a>(m: &'a Meter) { m.work_used_up(); }\n",
);
let code = super::live_code(text);
let lines: Vec<&str> = code.lines().collect();
assert_eq!(lines.len(), 11, "{code}");
for (i, l) in lines.iter().take(9).enumerate() {
assert!(!l.contains(['{', '}', '"', '\'', '\\']), "line {i}: {l:?}");
assert!(l.trim_end().ends_with(';'), "line {i}: {l:?}");
}
assert_eq!(lines[9].trim(), "");
assert_eq!(lines[10], "fn live<'a>(m: &'a Meter) { m.work_used_up(); }");
}
#[test]
fn live_code_blanks_test_items_and_keeps_what_follows() {
let text = "fn a() { x(); }\n#[cfg(test)]\nfn t() { y(); }\nfn b() { z(); }\n\
#[cfg(test)] impl S { fn u() { w(); } }\n#[allow(dead_code)] impl T { fn v() {} }\n\
let r#fn = r#work_used_up();\n";
let code = super::live_code(text);
let lines: Vec<&str> = code.lines().map(str::trim).collect();
assert_eq!(
lines,
[
"fn a() { x(); }",
"",
"",
"fn b() { z(); }",
"",
"#[allow(dead_code)] impl T { fn v() {} }",
"let r#fn = r#work_used_up();",
]
);
}
#[test]
fn literals_and_lifetimes_do_not_move_the_depth() {
let text = "#[cfg(test)]\nfn t<'a>(x: &'a str) {\n let _ = ('{', \"{{\", r#\"}\"#, b'(');\n}\nfn go() { rx.recv(); }\n";
assert_eq!(run(text), ["f.rs:5: .recv("]);
}
}