Skip to content

Commit 9362166

Browse files
committed
Add TODO checker
1 parent bf69dfa commit 9362166

4 files changed

Lines changed: 111 additions & 2 deletions

File tree

.github/workflows/main.yml

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,16 @@ env:
1919
RUSTFLAGS: "-Dwarnings"
2020

2121
jobs:
22+
todo_check:
23+
runs-on: ubuntu-latest
24+
timeout-minutes: 10
25+
26+
steps:
27+
- uses: actions/checkout@v4
28+
29+
- name: Check todo
30+
run: ./y.sh check-todo
31+
2232
rustfmt:
2333
runs-on: ubuntu-latest
2434
timeout-minutes: 10
@@ -232,7 +242,7 @@ jobs:
232242
runs-on: ubuntu-latest
233243
timeout-minutes: 10
234244
if: ${{ github.ref == 'refs/heads/main' }}
235-
needs: [rustfmt, test, bench, dist]
245+
needs: [todo_check, rustfmt, test, bench, dist]
236246

237247
permissions:
238248
contents: write # for creating the dev tag and release

build_system/main.rs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ mod prepare;
1717
mod rustc_info;
1818
mod shared_utils;
1919
mod tests;
20+
mod todo;
2021
mod utils;
2122

2223
fn usage() {
@@ -38,6 +39,7 @@ enum Command {
3839
Test,
3940
AbiCafe,
4041
Bench,
42+
CheckTodo,
4143
}
4244

4345
#[derive(Copy, Clone, Debug)]
@@ -66,6 +68,7 @@ fn main() {
6668
Some("test") => Command::Test,
6769
Some("abi-cafe") => Command::AbiCafe,
6870
Some("bench") => Command::Bench,
71+
Some("check-todo") => Command::CheckTodo,
6972
Some(flag) if flag.starts_with('-') => arg_error!("Expected command found flag {}", flag),
7073
Some(command) => arg_error!("Unknown command {}", command),
7174
None => {
@@ -139,6 +142,14 @@ fn main() {
139142
process::exit(0);
140143
}
141144

145+
if command == Command::CheckTodo {
146+
if let Err(err) = todo::run() {
147+
eprintln!("{err}");
148+
process::exit(1);
149+
}
150+
process::exit(0);
151+
}
152+
142153
let rustup_toolchain_name = match (env::var("CARGO"), env::var("RUSTC"), env::var("RUSTDOC")) {
143154
(Ok(_), Ok(_), Ok(_)) => None,
144155
(_, Err(_), Err(_)) => Some(rustc_info::get_toolchain_name()),
@@ -202,7 +213,7 @@ fn main() {
202213
))
203214
};
204215
match command {
205-
Command::Prepare => {
216+
Command::Prepare | Command::CheckTodo => {
206217
// Handled above
207218
}
208219
Command::Test => {

build_system/todo.rs

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
use std::ffi::OsStr;
2+
use std::fs;
3+
use std::path::{Path, PathBuf};
4+
use std::process::Command;
5+
6+
const EXTENSIONS: &[&str] =
7+
&["rs", "py", "js", "sh", "c", "cpp", "h", "md", "css", "ftl", "toml", "yml", "yaml"];
8+
const SKIP_FILES: &[&str] = &["build_system/todo.rs", ".github/workflows/main.yml"];
9+
10+
fn has_supported_extension(path: &Path) -> bool {
11+
path.extension().is_some_and(|ext| EXTENSIONS.iter().any(|e| ext == OsStr::new(e)))
12+
}
13+
14+
fn is_editor_temp(path: &Path) -> bool {
15+
path.file_name().is_some_and(|name| name.to_string_lossy().starts_with(".#"))
16+
}
17+
18+
fn list_tracked_files() -> Result<Vec<PathBuf>, String> {
19+
let output = Command::new("git")
20+
.args(["ls-files", "-z"])
21+
.output()
22+
.map_err(|e| format!("Failed to run `git ls-files`: {e}"))?;
23+
24+
if !output.status.success() {
25+
let stderr = String::from_utf8_lossy(&output.stderr);
26+
return Err(format!("`git ls-files` failed: {stderr}"));
27+
}
28+
29+
let mut files = Vec::new();
30+
for entry in output.stdout.split(|b| *b == 0) {
31+
if entry.is_empty() {
32+
continue;
33+
}
34+
let path = std::str::from_utf8(entry)
35+
.map_err(|_| "Non-utf8 path returned by `git ls-files`".to_string())?;
36+
files.push(PathBuf::from(path));
37+
}
38+
39+
Ok(files)
40+
}
41+
42+
pub(crate) fn run() -> Result<(), String> {
43+
let files = list_tracked_files()?;
44+
let mut errors = Vec::new();
45+
// Avoid embedding the task marker in source so greps only find real occurrences.
46+
let todo_marker = "todo".to_ascii_uppercase();
47+
48+
for file in files {
49+
if is_editor_temp(&file) {
50+
continue;
51+
}
52+
if SKIP_FILES.iter().any(|skip| file.ends_with(Path::new(skip))) {
53+
continue;
54+
}
55+
if !has_supported_extension(&file) {
56+
continue;
57+
}
58+
59+
let bytes =
60+
fs::read(&file).map_err(|e| format!("Failed to read `{}`: {e}", file.display()))?;
61+
let Ok(contents) = std::str::from_utf8(&bytes) else {
62+
continue;
63+
};
64+
65+
for (i, line) in contents.split('\n').enumerate() {
66+
let trimmed = line.trim();
67+
if trimmed.contains(&todo_marker) {
68+
errors.push(format!(
69+
"{}:{}: {} is used for tasks that should be done before merging a PR; if you want to leave a message in the codebase use FIXME",
70+
file.display(),
71+
i + 1,
72+
todo_marker
73+
));
74+
}
75+
}
76+
}
77+
78+
if errors.is_empty() {
79+
return Ok(());
80+
}
81+
82+
for err in &errors {
83+
eprintln!("{err}");
84+
}
85+
86+
Err(format!("found {} {}(s)", errors.len(), todo_marker))
87+
}

build_system/usage.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ USAGE:
66
./y.sh test [--sysroot none|clif|llvm] [--out-dir DIR] [--download-dir DIR] [--no-unstable-features] [--frozen] [--skip-test TESTNAME]
77
./y.sh abi-cafe [--sysroot none|clif|llvm] [--out-dir DIR] [--download-dir DIR] [--no-unstable-features] [--frozen]
88
./y.sh bench [--sysroot none|clif|llvm] [--out-dir DIR] [--download-dir DIR] [--no-unstable-features] [--frozen]
9+
./y.sh check-todo
910

1011
OPTIONS:
1112
--sysroot none|clif|llvm

0 commit comments

Comments
 (0)