garbage-code-hunter 0.2.2

A humorous Rust code quality detector that roasts your garbage code
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
//! Scan source files for "last words" — TODO, FIXME, HACK, TEMP comments.

use regex::Regex;
use std::path::{Path, PathBuf};
use walkdir::WalkDir;

/// A discovered "last word" comment in the codebase.
#[derive(Debug, Clone)]
pub struct LastWord {
    pub file: PathBuf,
    pub line: usize,
    pub kind: LastWordKind,
    pub text: String,
    pub age_days: Option<u64>,
}

/// The type of last-word comment.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum LastWordKind {
    Todo,
    Fixme,
    Hack,
    Temp,
    QuickFix,
    Wontfix,
    Workaround,
    Deprecated,
    Safety,
}

impl LastWordKind {
    pub fn label(&self) -> &'static str {
        match self {
            Self::Todo => "TODO",
            Self::Fixme => "FIXME",
            Self::Hack => "HACK",
            Self::Temp => "TEMP",
            Self::QuickFix => "quick fix",
            Self::Wontfix => "WONTFIX",
            Self::Workaround => "workaround",
            Self::Deprecated => "DEPRECATED",
            Self::Safety => "SAFETY",
        }
    }

    pub fn tombstone_quote(&self) -> &'static str {
        match self {
            Self::Todo => "I'll do it later",
            Self::Fixme => "This is fine... probably",
            Self::Hack => "Don't touch this",
            Self::Temp => "Temporary workaround",
            Self::QuickFix => "Quick fix for now",
            Self::Wontfix => "Won't fix, not my problem",
            Self::Workaround => "It works, don't ask how",
            Self::Deprecated => "Dead code walking",
            Self::Safety => "Unsafe but necessary",
        }
    }
}

/// Scan a directory for last-word comments.
pub fn scan(path: &Path) -> Vec<LastWord> {
    let patterns = build_patterns();
    let mut results = Vec::new();

    let entries: Vec<_> = if path.is_file() {
        vec![path.to_path_buf()]
    } else {
        WalkDir::new(path)
            .into_iter()
            .filter_map(|e| e.ok())
            .filter(|e| is_source_file(e.path()))
            .map(|e| e.path().to_path_buf())
            .collect()
    };

    for file_path in entries {
        let content = match std::fs::read_to_string(&file_path) {
            Ok(c) => c,
            Err(_) => continue,
        };

        for (line_num, line) in content.lines().enumerate() {
            let trimmed = line.trim();
            // Skip lines that are just code, not comments
            if !is_comment_line(trimmed) {
                continue;
            }

            for (kind, re) in &patterns {
                if re.is_match(trimmed) {
                    results.push(LastWord {
                        file: file_path.clone(),
                        line: line_num + 1,
                        kind: kind.clone(),
                        text: trimmed.to_string(),
                        age_days: None,
                    });
                }
            }
        }
    }

    results
}

/// Try to get the age of a TODO comment via git blame.
/// Returns age in days if successful.
pub fn try_get_age(file: &Path, line: usize) -> Option<u64> {
    let output = std::process::Command::new("git")
        .args([
            "blame",
            "-L",
            &format!("{},{}", line, line),
            "--porcelain",
            &file.to_string_lossy(),
        ])
        .output()
        .ok()?;

    if !output.status.success() {
        return None;
    }

    let stdout = String::from_utf8_lossy(&output.stdout);
    for l in stdout.lines() {
        if let Some(rest) = l.strip_prefix("committer-time ") {
            let timestamp: u64 = rest.trim().parse().ok()?;
            let now = std::time::SystemTime::now()
                .duration_since(std::time::UNIX_EPOCH)
                .ok()?
                .as_secs();
            return now.checked_sub(timestamp).map(|d| d / 86400);
        }
    }

    None
}

fn build_patterns() -> Vec<(LastWordKind, Regex)> {
    vec![
        (
            LastWordKind::Fixme,
            Regex::new(r"(?i)\bFIXME\b").expect("FIXME regex is a valid hardcoded literal"),
        ),
        (
            LastWordKind::Todo,
            Regex::new(r"(?i)\bTODO\b").expect("TODO regex is a valid hardcoded literal"),
        ),
        (
            LastWordKind::Hack,
            Regex::new(r"(?i)\bHACK\b").expect("HACK regex is a valid hardcoded literal"),
        ),
        (
            LastWordKind::Temp,
            Regex::new(r"(?i)\bTEMP(ORARY)?\b").expect("TEMP regex is a valid hardcoded literal"),
        ),
        (
            LastWordKind::QuickFix,
            Regex::new(r"(?i)\bquick\s*fix\b")
                .expect("quick-fix regex is a valid hardcoded literal"),
        ),
        (
            LastWordKind::Wontfix,
            Regex::new(r"(?i)\bWONT\s*FIX\b").expect("wontfix regex is a valid hardcoded literal"),
        ),
        (
            LastWordKind::Workaround,
            Regex::new(r"(?i)\bworkaround\b")
                .expect("workaround regex is a valid hardcoded literal"),
        ),
        (
            LastWordKind::Deprecated,
            Regex::new(r"(?i)\bDEPRECATED?\b")
                .expect("deprecated regex is a valid hardcoded literal"),
        ),
        (
            LastWordKind::Safety,
            Regex::new(r"(?i)\bSAFETY\b").expect("SAFETY regex is a valid hardcoded literal"),
        ),
    ]
}

fn is_comment_line(line: &str) -> bool {
    line.starts_with("//")
        || line.starts_with("/*")
        || line.starts_with("*")
        || line.starts_with("#")
        || line.starts_with("<!--")
}

fn is_source_file(path: &Path) -> bool {
    matches!(
        path.extension().and_then(|e| e.to_str()),
        Some(
            "rs" | "py"
                | "js"
                | "ts"
                | "go"
                | "java"
                | "c"
                | "cpp"
                | "h"
                | "hpp"
                | "rb"
                | "php"
                | "swift"
                | "kt"
                | "scala"
                | "sh"
                | "bash"
                | "zsh"
                | "toml"
                | "yaml"
                | "yml"
                | "json"
                | "md"
                | "html"
                | "css"
                | "sql"
        )
    )
}

#[cfg(test)]
mod tests {
    use super::*;

    // ── kind_label ─────────────────────────────────────────────────

    /// Objective: Verify all variants map to the expected label strings.
    /// Invariants: Every LastWordKind has a unique, non-empty label.
    #[test]
    fn test_kind_label_all_variants() {
        assert_eq!(LastWordKind::Todo.label(), "TODO");
        assert_eq!(LastWordKind::Fixme.label(), "FIXME");
        assert_eq!(LastWordKind::Hack.label(), "HACK");
        assert_eq!(LastWordKind::Temp.label(), "TEMP");
        assert_eq!(LastWordKind::QuickFix.label(), "quick fix");
        assert_eq!(LastWordKind::Wontfix.label(), "WONTFIX");
        assert_eq!(LastWordKind::Workaround.label(), "workaround");
        assert_eq!(LastWordKind::Deprecated.label(), "DEPRECATED");
        assert_eq!(LastWordKind::Safety.label(), "SAFETY");
    }

    // ── tombstone_quote ────────────────────────────────────────────

    /// Objective: Verify all variants return a non-empty tombstone quote.
    /// Invariants: Every quote is a unique, non-empty string (no panics).
    #[test]
    fn test_tombstone_quote_all_variants() {
        for kind in &[
            LastWordKind::Todo,
            LastWordKind::Fixme,
            LastWordKind::Hack,
            LastWordKind::Temp,
            LastWordKind::QuickFix,
            LastWordKind::Wontfix,
            LastWordKind::Workaround,
            LastWordKind::Deprecated,
            LastWordKind::Safety,
        ] {
            let quote = kind.tombstone_quote();
            assert!(
                !quote.is_empty(),
                "{:?}.tombstone_quote() should not be empty",
                kind
            );
        }
    }

    // ── is_comment_line ────────────────────────────────────────────

    /// Objective: Verify all comment line prefixes are recognized.
    /// Invariants: Lines starting with //, /*, *, #, <!-- are comments.
    #[test]
    fn test_is_comment_line_all_styles() {
        assert!(is_comment_line("// TODO: fix"), "// line");
        assert!(is_comment_line("/* FIXME */"), "/* line");
        assert!(is_comment_line("* multiline continuation"), "* line");
        assert!(is_comment_line("# HACK"), "# line");
        assert!(is_comment_line("<!-- HTML comment -->"), "<!-- line");
    }

    /// Objective: Verify known non-comment lines are rejected.
    #[test]
    fn test_is_comment_line_non_comment() {
        assert!(!is_comment_line("let x = 1;"), "code line");
        assert!(!is_comment_line(""), "empty line");
        assert!(!is_comment_line("   "), "whitespace only");
        assert!(
            !is_comment_line("x // inline comment is not a comment line"),
            "code with trailing //"
        );
    }

    // ── is_source_file ─────────────────────────────────────────────

    /// Objective: Verify is_source_file returns true for all supported extensions.
    /// Invariants: Any file with a supported extension is recognized as a source file.
    #[test]
    fn test_is_source_file_supported() {
        for ext in &[
            "rs", "py", "js", "ts", "go", "java", "c", "cpp", "h", "hpp", "rb", "php", "swift",
            "kt", "scala", "sh", "bash", "zsh", "toml", "yaml", "yml", "json", "md", "html", "css",
            "sql",
        ] {
            let path = PathBuf::from(format!("file.{}", ext));
            assert!(
                is_source_file(&path),
                "file.{ext} should be recognized as source"
            );
        }
    }

    /// Objective: Verify is_source_file returns false for unsupported extensions.
    #[test]
    fn test_is_source_file_unsupported() {
        assert!(
            !is_source_file(Path::new("file.txt")),
            ".txt should not be source"
        );
        assert!(
            !is_source_file(Path::new("file.pdf")),
            ".pdf should not be source"
        );
        assert!(
            !is_source_file(Path::new("Makefile")),
            "no ext should not be source"
        );
        assert!(
            !is_source_file(Path::new("file.")),
            "trailing dot should not be source"
        );
    }

    // ── build_patterns ─────────────────────────────────────────────

    /// Objective: Verify each pattern matches its expected keyword and rejects others.
    /// Invariants: Patterns are case-insensitive; they match keywords anywhere in a line.
    #[test]
    fn test_build_patterns_todo() {
        let patterns = build_patterns();
        let todo_re = patterns
            .iter()
            .find(|(k, _)| *k == LastWordKind::Todo)
            .map(|(_, r)| r)
            .unwrap();
        assert!(
            todo_re.is_match("// TODO: fix me"),
            "TODO pattern should match '// TODO: fix me'"
        );
        assert!(
            todo_re.is_match("// todo: lower case"),
            "TODO i flag: 'todo'"
        );
        assert!(
            !todo_re.is_match("// todolist"),
            "TODO should not match 'todolist' (\\b)"
        );
    }

    /// Objective: Verify FIXME pattern works correctly.
    #[test]
    fn test_build_patterns_fixme() {
        let patterns = build_patterns();
        let fixme_re = patterns
            .iter()
            .find(|(k, _)| *k == LastWordKind::Fixme)
            .map(|(_, r)| r)
            .unwrap();
        assert!(fixme_re.is_match("// FIXME: broken"), "FIXME should match");
        assert!(fixme_re.is_match("# fixme"), "fixme lowercase");
        assert!(
            !fixme_re.is_match("// fixable"),
            "fixme should not match 'fixable'"
        );
    }

    /// Objective: Verify HACK pattern matches with word boundary.
    #[test]
    fn test_build_patterns_hack() {
        let patterns = build_patterns();
        let hack_re = patterns
            .iter()
            .find(|(k, _)| *k == LastWordKind::Hack)
            .map(|(_, r)| r)
            .unwrap();
        assert!(
            hack_re.is_match("// HACK: ugly but works"),
            "HACK should match"
        );
        assert!(
            !hack_re.is_match("// hackneyed"),
            "HACK should not match 'hackneyed' (\\b)"
        );
    }

    /// Objective: Verify TEMP pattern matches both "TEMP" and "TEMPORARY".
    #[test]
    fn test_build_patterns_temp() {
        let patterns = build_patterns();
        let temp_re = patterns
            .iter()
            .find(|(k, _)| *k == LastWordKind::Temp)
            .map(|(_, r)| r)
            .unwrap();
        assert!(temp_re.is_match("// TEMP: quick fix"), "TEMP should match");
        assert!(
            temp_re.is_match("// TEMPORARY workaround"),
            "TEMPORARY should match"
        );
        assert!(
            !temp_re.is_match("// temperature"),
            "TEMPORARY? with \\b should not match 'temperature'"
        );
    }

    /// Objective: Verify quick-fix pattern matches with optional space between words.
    #[test]
    fn test_build_patterns_quickfix() {
        let patterns = build_patterns();
        let qfix_re = patterns
            .iter()
            .find(|(k, _)| *k == LastWordKind::QuickFix)
            .map(|(_, r)| r)
            .unwrap();
        assert!(qfix_re.is_match("// quick fix"), "quick fix should match");
        assert!(
            qfix_re.is_match("// quickfix"),
            "quickfix should match (\\s* allows zero)"
        );
        assert!(
            qfix_re.is_match("// Quick Fix"),
            "Quick Fix should match (i flag)"
        );
    }

    /// Objective: Verify WONTFIX pattern matches standard form.
    #[test]
    fn test_build_patterns_wontfix() {
        let patterns = build_patterns();
        let wontfix_re = patterns
            .iter()
            .find(|(k, _)| *k == LastWordKind::Wontfix)
            .map(|(_, r)| r)
            .unwrap();
        assert!(wontfix_re.is_match("// WONT FIX"), "WONT FIX should match");
        assert!(
            wontfix_re.is_match("// wontfix"),
            "wontfix collapsed form matches (\\s* zero)"
        );
        assert!(
            wontfix_re.is_match("// WONT   FIX"),
            "multiple spaces between WONT and FIX"
        );
        assert!(
            !wontfix_re.is_match("// won't fix"),
            "should not match apostrophe form"
        );
    }

    /// Objective: Verify WORKAROUND pattern matches.
    #[test]
    fn test_build_patterns_workaround() {
        let patterns = build_patterns();
        let work_re = patterns
            .iter()
            .find(|(k, _)| *k == LastWordKind::Workaround)
            .map(|(_, r)| r)
            .unwrap();
        assert!(
            work_re.is_match("// workaround: temp fix"),
            "workaround should match"
        );
        assert!(work_re.is_match("/* WORKAROUND */"), "WORKAROUND uppercase");
    }

    /// Objective: Verify DEPRECATED pattern matches both "DEPRECATED" and "DEPRECATE".
    #[test]
    fn test_build_patterns_deprecated() {
        let patterns = build_patterns();
        let dep_re = patterns
            .iter()
            .find(|(k, _)| *k == LastWordKind::Deprecated)
            .map(|(_, r)| r)
            .unwrap();
        assert!(
            dep_re.is_match("// DEPRECATED: old code"),
            "DEPRECATED should match"
        );
        assert!(
            dep_re.is_match("// Deprecate this in v2"),
            "DEPRECATE? should match 'Deprecate'"
        );
        assert!(
            !dep_re.is_match("// depreciation"),
            "DEPRECATE? with \\b pos should not match 'depreciation'"
        );
    }

    /// Objective: Verify SAFETY pattern matches.
    #[test]
    fn test_build_patterns_safety() {
        let patterns = build_patterns();
        let safety_re = patterns
            .iter()
            .find(|(k, _)| *k == LastWordKind::Safety)
            .map(|(_, r)| r)
            .unwrap();
        assert!(
            safety_re.is_match("// SAFETY: unsafe block"),
            "SAFETY should match"
        );
        assert!(safety_re.is_match("# safety: required"), "safety lowercase");
    }

    // ── scan ───────────────────────────────────────────────────────

    /// Objective: Verify scan finds TODO and FIXME in a Rust file.
    /// Invariants: Each matching comment creates one LastWord result.
    #[test]
    fn test_scan_finds_todos_and_fixmes() {
        let dir = std::env::temp_dir().join("gch_last_words_test_scan");
        let _ = std::fs::create_dir_all(&dir);
        let file = dir.join("test.rs");
        std::fs::write(&file, "// TODO: fix this\nfn main() {}\n// FIXME: broken\n").unwrap();

        let results = scan(&dir);
        assert!(
            results.iter().any(|r| r.kind == LastWordKind::Todo),
            "should find TODO"
        );
        assert!(
            results.iter().any(|r| r.kind == LastWordKind::Fixme),
            "should find FIXME"
        );
        assert_eq!(results.len(), 2, "exactly 2 last-words in the file");

        let _ = std::fs::remove_dir_all(&dir);
    }

    /// Objective: Verify scan finds all comment types: #, //, /* */ in different files.
    #[test]
    fn test_scan_finds_hack_in_python() {
        let dir = std::env::temp_dir().join("gch_last_words_test_hack");
        let _ = std::fs::create_dir_all(&dir);
        let py_file = dir.join("script.py");
        std::fs::write(&py_file, "# HACK: this is terrible\nx = 1\n").unwrap();

        let results = scan(&dir);
        // Only the .py file should be scanned
        let hacks: Vec<_> = results
            .iter()
            .filter(|r| r.kind == LastWordKind::Hack)
            .collect();
        assert_eq!(hacks.len(), 1, "should find 1 HACK in Python file");
        assert_eq!(hacks[0].line, 1, "HACK should be on line 1");

        let _ = std::fs::remove_dir_all(&dir);
    }

    /// Objective: Verify scan skips non-comment lines (code only).
    #[test]
    fn test_scan_skips_code_lines() {
        let dir = std::env::temp_dir().join("gch_last_words_test_skip");
        let _ = std::fs::create_dir_all(&dir);
        let file = dir.join("test.rs");
        std::fs::write(&file, "fn main() {\n    let todo = \"not a comment\";\n}\n").unwrap();

        let results = scan(&dir);
        assert!(
            results.is_empty(),
            "code with no comments should produce 0 results, got {}",
            results.len()
        );

        let _ = std::fs::remove_dir_all(&dir);
    }

    /// Objective: Verify scan handles empty files without panicking.
    #[test]
    fn test_scan_empty_file() {
        let dir = std::env::temp_dir().join("gch_last_words_test_empty");
        let _ = std::fs::create_dir_all(&dir);
        let file = dir.join("empty.rs");
        std::fs::write(&file, "").unwrap();

        let results = scan(&dir);
        assert!(results.is_empty(), "empty file should produce 0 results");

        let _ = std::fs::remove_dir_all(&dir);
    }

    /// Objective: Verify scan ignores non-source files.
    #[test]
    fn test_scan_ignores_unsupported_extensions() {
        let dir = std::env::temp_dir().join("gch_last_words_test_unsup");
        let _ = std::fs::create_dir_all(&dir);
        let file = dir.join("readme.txt");
        std::fs::write(&file, "// TODO: ignored").unwrap();

        let results = scan(&dir);
        assert!(
            results.is_empty(),
            ".txt file should be ignored, got {} results",
            results.len()
        );

        let _ = std::fs::remove_dir_all(&dir);
    }

    /// Objective: Verify scan works on a single file (not a directory).
    #[test]
    fn test_scan_single_file() {
        let dir = std::env::temp_dir().join("gch_last_words_test_single");
        let _ = std::fs::create_dir_all(&dir);
        let file = dir.join("single.rs");
        std::fs::write(&file, "// TODO: single file\n// HACK: also here\n").unwrap();

        let results = scan(&file);
        assert_eq!(results.len(), 2, "single file scan should find 2 results");
        assert!(results.iter().any(|r| r.kind == LastWordKind::Todo));
        assert!(results.iter().any(|r| r.kind == LastWordKind::Hack));

        let _ = std::fs::remove_dir_all(&dir);
    }
}