repopilot 0.11.0

Local-first CLI for repository audit, architecture risk detection, baseline tracking, and CI-friendly code review.
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
use crate::audits::code_quality::sanitize::sanitize_c_style;
use crate::audits::context::{FileRole, LanguageKind, RuntimeKind, classify_file};
use crate::audits::traits::FileAudit;
use crate::findings::types::{Confidence, Evidence, Finding, FindingCategory, Severity};
use crate::knowledge::decision::decide_for_audit_context;
use crate::scan::config::ScanConfig;
use crate::scan::facts::FileFacts;
use crate::scan::path_classification::is_low_signal_audit_path;

const RULE_ID: &str = "language.rust.panic-risk";

pub struct RustPanicRiskAudit;

impl FileAudit for RustPanicRiskAudit {
    fn audit(&self, file: &FileFacts, _config: &ScanConfig) -> Vec<Finding> {
        if is_low_signal_audit_path(&file.path) {
            return vec![];
        }

        let context = classify_file(file);

        if context.language != LanguageKind::Rust {
            return vec![];
        }

        let Some(content) = file.content.as_deref() else {
            return vec![];
        };

        let mut findings = Vec::new();
        let mut in_block_comment = false;
        let mut pending_render_write = false;

        for (index, line) in content.lines().enumerate() {
            let line_number = index + 1;
            let trimmed = line.trim();

            if trimmed.is_empty() {
                continue;
            }

            let sanitized = sanitize_c_style(line, &mut in_block_comment);
            let sanitized = sanitized.trim();
            if is_infallible_render_write_start(&file.path, sanitized) {
                pending_render_write = true;
            }

            let Some(pattern) = detect_pattern(sanitized) else {
                if sanitized.ends_with(';') {
                    pending_render_write = false;
                }
                continue;
            };

            if pending_render_write && is_infallible_render_write_result_unwrap(pattern, sanitized)
            {
                if sanitized.ends_with(';') {
                    pending_render_write = false;
                }
                continue;
            }

            let decision = decide_for_audit_context(
                RULE_ID,
                &context,
                pattern.base_severity(),
                Some(pattern.signal()),
            );

            if decision.is_suppressed() {
                continue;
            }

            findings.push(build_finding(
                file,
                line_number,
                trimmed,
                pattern,
                &context,
                decision.severity,
            ));

            if sanitized.ends_with(';') {
                pending_render_write = false;
            }
        }

        findings
    }
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum RustPanicPattern {
    Unwrap,
    Expect,
    Panic,
    Todo,
    Unimplemented,
}

impl RustPanicPattern {
    fn label(self) -> &'static str {
        match self {
            RustPanicPattern::Unwrap => "unwrap()",
            RustPanicPattern::Expect => "expect()",
            RustPanicPattern::Panic => "panic!",
            RustPanicPattern::Todo => "todo!",
            RustPanicPattern::Unimplemented => "unimplemented!",
        }
    }

    fn signal(self) -> &'static str {
        match self {
            RustPanicPattern::Unwrap => "rust.unwrap",
            RustPanicPattern::Expect => "rust.expect",
            RustPanicPattern::Panic => "rust.panic",
            RustPanicPattern::Todo => "rust.todo",
            RustPanicPattern::Unimplemented => "rust.unimplemented",
        }
    }

    fn base_severity(self) -> Severity {
        match self {
            RustPanicPattern::Todo | RustPanicPattern::Unimplemented => Severity::High,
            RustPanicPattern::Unwrap | RustPanicPattern::Expect | RustPanicPattern::Panic => {
                Severity::Medium
            }
        }
    }
}

fn detect_pattern(trimmed: &str) -> Option<RustPanicPattern> {
    if trimmed.contains("todo!(") {
        return Some(RustPanicPattern::Todo);
    }

    if trimmed.contains("unimplemented!(") {
        return Some(RustPanicPattern::Unimplemented);
    }

    if trimmed.contains("panic!(") {
        return Some(RustPanicPattern::Panic);
    }

    if trimmed.contains(".unwrap()") {
        return Some(RustPanicPattern::Unwrap);
    }

    if trimmed.contains(".expect(") {
        return Some(RustPanicPattern::Expect);
    }

    None
}

fn is_infallible_render_write_start(path: &std::path::Path, trimmed: &str) -> bool {
    is_report_renderer_path(path)
        && (trimmed.starts_with("writeln!(") || trimmed.starts_with("write!("))
}

fn is_report_renderer_path(path: &std::path::Path) -> bool {
    let mut previous = None;
    for component in path
        .components()
        .filter_map(|component| component.as_os_str().to_str())
    {
        if previous == Some("src") && component == "output" {
            return true;
        }
        previous = Some(component);
    }
    false
}

fn is_infallible_render_write_result_unwrap(pattern: RustPanicPattern, trimmed: &str) -> bool {
    match pattern {
        RustPanicPattern::Unwrap => {
            trimmed == ".unwrap();"
                || (is_infallible_render_write_line(trimmed) && trimmed.ends_with(".unwrap();"))
        }
        RustPanicPattern::Expect => {
            trimmed.starts_with(".expect(")
                || (is_infallible_render_write_line(trimmed) && trimmed.contains(").expect("))
        }
        RustPanicPattern::Panic | RustPanicPattern::Todo | RustPanicPattern::Unimplemented => false,
    }
}

fn is_infallible_render_write_line(trimmed: &str) -> bool {
    trimmed.starts_with("writeln!(") || trimmed.starts_with("write!(")
}

fn build_finding(
    file: &FileFacts,
    line_number: usize,
    snippet: &str,
    pattern: RustPanicPattern,
    context: &crate::audits::context::AuditContext,
    severity: Severity,
) -> Finding {
    let context_label = context_label(context);
    let recommendation = recommendation_for(context, pattern);
    let confidence = confidence_for(context, pattern, severity);
    let confidence_reason = confidence_reason_for(context, pattern);

    Finding {
        id: String::new(),
        rule_id: RULE_ID.to_string(),
        recommendation: recommendation.to_string(),
        title: format!("Risky Rust {} usage in {}", pattern.label(), context_label),
        description: format!(
            "Rust `{}` was found in {}; confidence is {} because {}. Unhandled panic paths can terminate execution or hide recoverable errors in production code.",
            pattern.label(),
            context_label,
            confidence.label(),
            confidence_reason,
        ),
        category: FindingCategory::CodeQuality,
        severity,
        confidence,
        evidence: vec![Evidence {
            path: file.path.clone(),
            line_start: line_number,
            line_end: None,
            snippet: snippet.to_string(),
        }],
        workspace_package: None,
        docs_url: None,
        risk: Default::default(),
    }
}

fn context_label(context: &crate::audits::context::AuditContext) -> &'static str {
    if context.is_test {
        return "Rust test code";
    }

    if context.has_runtime(RuntimeKind::RustCli) {
        return "Rust CLI boundary code";
    }

    if context.has_role(FileRole::Domain) {
        return "Rust domain code";
    }

    if context.has_runtime(RuntimeKind::RustLibrary) {
        return "Rust library code";
    }

    "Rust production code"
}

fn confidence_for(
    context: &crate::audits::context::AuditContext,
    pattern: RustPanicPattern,
    severity: Severity,
) -> Confidence {
    if context.is_test {
        return Confidence::Low;
    }

    if matches!(
        pattern,
        RustPanicPattern::Todo | RustPanicPattern::Unimplemented
    ) {
        return Confidence::High;
    }

    if context.has_runtime(RuntimeKind::RustCli) {
        return match pattern {
            RustPanicPattern::Unwrap | RustPanicPattern::Expect => Confidence::Low,
            RustPanicPattern::Panic => Confidence::Medium,
            RustPanicPattern::Todo | RustPanicPattern::Unimplemented => Confidence::High,
        };
    }

    if context.has_role(FileRole::Domain) || context.has_runtime(RuntimeKind::RustLibrary) {
        return Confidence::High;
    }

    match severity {
        Severity::High | Severity::Critical => Confidence::High,
        Severity::Info | Severity::Low => Confidence::Low,
        Severity::Medium => Confidence::Medium,
    }
}

fn confidence_reason_for(
    context: &crate::audits::context::AuditContext,
    pattern: RustPanicPattern,
) -> &'static str {
    if context.is_test {
        return "test code often uses panic-style macros for assertion setup or unfinished test scaffolding";
    }

    if matches!(
        pattern,
        RustPanicPattern::Todo | RustPanicPattern::Unimplemented
    ) {
        return "placeholder macros always panic if this path is reached";
    }

    if context.has_runtime(RuntimeKind::RustCli) {
        return match pattern {
            RustPanicPattern::Unwrap | RustPanicPattern::Expect => {
                "CLI boundary code may intentionally fail fast, but user-facing errors are usually better"
            }
            RustPanicPattern::Panic => {
                "CLI boundary code can terminate the process intentionally, but panic output is rarely a good user-facing error"
            }
            RustPanicPattern::Todo | RustPanicPattern::Unimplemented => {
                "placeholder macros always panic if this path is reached"
            }
        };
    }

    if context.has_role(FileRole::Domain) {
        return "domain code is usually reusable production logic, so callers cannot recover from this panic path";
    }

    if context.has_runtime(RuntimeKind::RustLibrary) {
        return "library code is reused by callers, so panics become part of its public failure behavior";
    }

    "this is production Rust code and the panic path is not locally handled"
}

fn recommendation_for(
    context: &crate::audits::context::AuditContext,
    pattern: RustPanicPattern,
) -> &'static str {
    if context.is_test {
        return "Panic-style helpers in tests can be acceptable, but keep them out of reusable test utilities when possible.";
    }

    match pattern {
        RustPanicPattern::Unwrap => {
            if context.has_runtime(RuntimeKind::RustCli) {
                "At CLI boundaries this may be acceptable for prototype code, but prefer returning a user-friendly error with context."
            } else {
                "Return `Result` or propagate with `?`; convert to `expect()` only when failure is impossible and the message documents the invariant."
            }
        }
        RustPanicPattern::Expect => {
            if context.has_runtime(RuntimeKind::RustCli) {
                "At CLI boundaries this may be acceptable for prototype code, but prefer returning a user-friendly error with context."
            } else {
                "Prefer `Result`/`?` for recoverable errors. Keep `expect()` only for impossible states, with a message that names the invariant."
            }
        }
        RustPanicPattern::Panic => {
            "Avoid panics in reusable production code. Prefer typed errors, validation, or explicit fallback behavior."
        }
        RustPanicPattern::Todo | RustPanicPattern::Unimplemented => {
            "Replace placeholder macros before release or guard them behind test-only code paths."
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::scan::facts::FileFacts;
    use std::path::PathBuf;

    #[test]
    fn ignores_unwrap_in_rust_tests() {
        let file = facts(
            "tests/parser_test.rs",
            "let value = parse().unwrap();",
            true,
        );

        let findings = RustPanicRiskAudit.audit(&file, &ScanConfig::default());

        assert!(findings.is_empty());
    }

    #[test]
    fn reports_unwrap_in_rust_library_code() {
        let file = facts(
            "src/domain/parser.rs",
            "let value = parse().unwrap();",
            false,
        );

        let findings = RustPanicRiskAudit.audit(&file, &ScanConfig::default());

        assert_eq!(findings.len(), 1);
        assert_eq!(findings[0].rule_id, RULE_ID);
        assert_eq!(findings[0].severity, Severity::Medium);
        assert_eq!(findings[0].confidence, Confidence::High);
        assert!(findings[0].title.contains("unwrap()"));
        assert!(
            findings[0]
                .description
                .contains("confidence is HIGH because")
        );
        assert!(findings[0].recommendation.contains("Return `Result`"));
    }

    #[test]
    fn reports_panic_in_domain_code_as_high() {
        let file = facts(
            "src/domain/user.rs",
            "panic!(\"invalid user state\");",
            false,
        );

        let findings = RustPanicRiskAudit.audit(&file, &ScanConfig::default());

        assert_eq!(findings.len(), 1);
        assert_eq!(findings[0].severity, Severity::High);
        assert!(findings[0].title.contains("panic!"));
    }

    #[test]
    fn reports_todo_in_production_code_as_high() {
        let file = facts(
            "src/service.rs",
            "todo!(\"implement payment flow\");",
            false,
        );

        let findings = RustPanicRiskAudit.audit(&file, &ScanConfig::default());

        assert_eq!(findings.len(), 1);
        assert_eq!(findings[0].severity, Severity::High);
        assert!(findings[0].title.contains("todo!"));
    }

    #[test]
    fn lowers_unwrap_severity_in_rust_cli_boundary() {
        let file = facts("src/main.rs", "let config = load_config().unwrap();", false);

        let findings = RustPanicRiskAudit.audit(&file, &ScanConfig::default());

        assert_eq!(findings.len(), 1);
        assert_eq!(findings[0].severity, Severity::Low);
        assert_eq!(findings[0].confidence, Confidence::Low);
        assert!(findings[0].description.contains("CLI boundary"));
    }

    #[test]
    fn ignores_commented_panic_patterns() {
        let file = facts(
            "src/lib.rs",
            "// let value = parse().unwrap();\n/// panic!(\"example\");\n/*\n * value.unwrap()\n */\n// panic!(\"old code\");",
            false,
        );

        let findings = RustPanicRiskAudit.audit(&file, &ScanConfig::default());

        assert!(findings.is_empty());
    }

    #[test]
    fn ignores_string_literal_panic_patterns() {
        let file = facts(
            "src/lib.rs",
            "let text = \"value.unwrap() and panic!(\\\"example\\\")\";",
            false,
        );

        let findings = RustPanicRiskAudit.audit(&file, &ScanConfig::default());

        assert!(findings.is_empty());
    }

    #[test]
    fn ignores_string_write_unwraps_in_report_renderers() {
        let file = facts(
            "src/output/markdown.rs",
            "pub fn render(output: &mut String) {\n    writeln!(output, \"# Report\").unwrap();\n}\n",
            false,
        );

        let findings = RustPanicRiskAudit.audit(&file, &ScanConfig::default());

        assert!(findings.is_empty());
    }

    #[test]
    fn ignores_multiline_string_write_unwraps_in_report_renderers() {
        let file = facts(
            "src/output/console.rs",
            "pub fn render(output: &mut String) {\n    writeln!(\n        output,\n        \"Findings: {}\",\n        3\n    )\n    .unwrap();\n}\n",
            false,
        );

        let findings = RustPanicRiskAudit.audit(&file, &ScanConfig::default());

        assert!(findings.is_empty());
    }

    #[test]
    fn still_reports_non_renderer_unwraps_in_output_modules() {
        let file = facts(
            "src/output/markdown.rs",
            "pub fn render(value: Option<&str>) -> &str {\n    value.unwrap()\n}\n",
            false,
        );

        let findings = RustPanicRiskAudit.audit(&file, &ScanConfig::default());

        assert_eq!(findings.len(), 1);
        assert_eq!(findings[0].rule_id, RULE_ID);
    }

    #[test]
    fn still_reports_unwraps_inside_renderer_format_arguments() {
        let file = facts(
            "src/output/console.rs",
            "pub fn render(output: &mut String, value: Option<&str>) {\n    writeln!(output, \"{}\", value.unwrap());\n}\n",
            false,
        );

        let findings = RustPanicRiskAudit.audit(&file, &ScanConfig::default());

        assert_eq!(findings.len(), 1);
        assert_eq!(findings[0].rule_id, RULE_ID);
    }

    #[test]
    fn does_not_report_functional_iterator_pipeline_without_panic_risk() {
        let file = facts(
            "src/domain/users.rs",
            "let names = users\n    .iter()\n    .filter(|user| user.is_active)\n    .map(|user| user.name.clone())\n    .collect::<Vec<_>>();\n",
            false,
        );

        let findings = RustPanicRiskAudit.audit(&file, &ScanConfig::default());

        assert!(findings.is_empty());
    }

    #[test]
    fn reports_expect_in_domain_code() {
        let file = facts(
            "src/domain/parser.rs",
            "let value = parse().expect(\"valid domain input\");",
            false,
        );

        let findings = RustPanicRiskAudit.audit(&file, &ScanConfig::default());

        assert_eq!(findings.len(), 1);
        assert_eq!(findings[0].severity, Severity::Medium);
        assert_eq!(findings[0].confidence, Confidence::High);
        assert!(findings[0].title.contains("expect()"));
        assert!(findings[0].description.contains("Rust domain code"));
    }

    #[test]
    fn reports_unimplemented_in_production_code() {
        let file = facts("src/lib.rs", "unimplemented!(\"missing adapter\");", false);

        let findings = RustPanicRiskAudit.audit(&file, &ScanConfig::default());

        assert_eq!(findings.len(), 1);
        assert_eq!(findings[0].severity, Severity::High);
        assert!(findings[0].title.contains("unimplemented!"));
    }

    #[test]
    fn does_not_run_on_non_rust_files() {
        let mut file = facts("src/app.ts", "panic!(\"not rust\");", false);
        file.language = Some("TypeScript".to_string());

        let findings = RustPanicRiskAudit.audit(&file, &ScanConfig::default());

        assert!(findings.is_empty());
    }

    #[test]
    fn does_not_run_without_file_content() {
        let mut file = facts("src/lib.rs", "panic!(\"missing content\");", false);
        file.content = None;

        let findings = RustPanicRiskAudit.audit(&file, &ScanConfig::default());

        assert!(findings.is_empty());
    }

    fn facts(path: &str, content: &str, has_inline_tests: bool) -> FileFacts {
        FileFacts {
            path: PathBuf::from(path),
            language: Some("Rust".to_string()),
            lines_of_code: content.lines().count(),
            branch_count: 0,
            imports: Vec::new(),
            content: Some(content.to_string()),
            has_inline_tests,
        }
    }
}