candor-sentinel 1.0.0

Deterministic guardrails: 6 rules + 10 doctrine principles for agent safety
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
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
/// AST-level slop detection using line-based source analysis (no syn dependency).
///
/// Goes beyond simple regex matching by tracking function definitions,
/// reference counts, block structure, comment patterns, and control-flow
/// shapes across a Rust source file. All analysis is purely structural —
/// no semantic type resolution, no external parser.
use std::collections::HashMap;

use crate::rules::{RuleViolation, RulesCheck, ViolationSeverity};

// ── AI narration comment patterns ───────────────────────────────────────────

/// Patterns that indicate AI-generated narration comments.
///
/// These are comments written in a "tutorial" voice that narrate what the
/// code is doing. They add noise without value in production code.
const NARRATION_PATTERNS: &[&str] = &[
    "now we",
    "here we",
    "let us",
    "let's",
    "first we",
    "next we",
    "this function",
    "this method",
    "this helper",
    "this utility",
    "we create",
    "we define",
    "we implement",
    "we set up",
    "we build",
    "we add",
    "we write",
    "we need",
    "we want",
    "we can",
    "we will",
    "we use",
    "we call",
    "we check",
    "we handle",
    "we return",
    "we parse",
    "we convert",
    "we transform",
    "we generate",
    "we process",
    "we take",
    "we start",
    "we begin",
    "essentially",
    "basically just",
    "simply a",
    "in other words",
    "that is to say",
    "as mentioned",
    "as noted",
    "as we saw",
    "the idea is",
    "the concept is",
    "basically,",
    "simply put",
    "in essence",
    "// now ",
    "// next, ",
];

/// Lines that look like function definition headers.
fn is_fn_def(line: &str) -> bool {
    let trimmed = line.trim();
    // Skip lines that are comments or contain "#["
    if trimmed.starts_with("//") || trimmed.starts_with("/*") || trimmed.starts_with("#[") {
        return false;
    }
    // Match `fn name(...` at start (possibly with pub/async/unsafe/extern modifiers)
    let stripped = strip_visibility_and_modifiers(trimmed);
    stripped.starts_with("fn ") && stripped[3..].trim_start().chars().next().is_some_and(|c| c.is_alphabetic() || c == '_')
}

fn strip_visibility_and_modifiers(s: &str) -> &str {
    let mut s = s.trim_start();
    loop {
        let before = s;
        for prefix in &["pub ", "pub(crate) ", "pub(super) ", "pub(self) ",
                          "async ", "unsafe ", "extern ", "const ", "default ",
                          "override "] {
            if let Some(rest) = s.strip_prefix(prefix) {
                s = rest;
                break;
            }
        }
        // Also handle pub(in path) etc — simplified: just skip "pub(" ... ")"
        if s.starts_with("pub(")
            && let Some(close) = s.find(')') {
                s = s[close + 1..].trim_start();
            }
        if s == before {
            break;
        }
    }
    s
}

/// Extract function name from a `fn name(...` line.
fn extract_fn_name(line: &str) -> Option<String> {
    let trimmed = line.trim();
    let stripped = strip_visibility_and_modifiers(trimmed);
    if let Some(name_start) = stripped.strip_prefix("fn ") {
        let after_fn = name_start.trim_start();
        // name is up to '(' or '<' (generics) or ' ' (whitespace before parens in some edge cases)
        if let Some(paren) = after_fn.find('(') {
            let name_candidate = after_fn[..paren].trim();
            // Strip generics like 'name<T>'
            let name = name_candidate.split('<').next().unwrap_or(name_candidate).trim();
            if !name.is_empty() && name.chars().all(|c| c.is_alphanumeric() || c == '_') {
                return Some(name.to_string());
            }
        }
    }
    None
}

/// Extract function call references from a line.
/// Ignores the definition site itself.
fn extract_fn_calls(line: &str, defined_fns: &[String]) -> Vec<String> {
    let mut calls = Vec::new();
    for fn_name in defined_fns {
        // Look for `fn_name(` pattern, but not `fn fn_name(` (definition)
        let trimmed = line.trim();
        if trimmed.starts_with(&format!("fn {}", fn_name)) {
            continue;
        }
        // Simple detection: function_name followed by '('
        let search = format!("{}(", fn_name);
        if line.contains(&search) {
            calls.push(fn_name.clone());
        }
    }
    calls
}

/// Check if a line is unreachable (comes after return/panic!/todo!/unreachable!)
fn has_unreachable_after(lines: &[&str], idx: usize) -> Option<String> {
    if idx == 0 {
        return None;
    }
    let prev_line = lines[idx - 1].trim();

    // Check if previous line ends a block: `return expr;`, `panic!(...);`, etc.
    let terminating_patterns = [
        "return ",
        "return;",
        "panic!(",
        "unreachable!(",
        "todo!(",
        "unimplemented!(",
        "std::process::exit(",
        "process::exit(",
        "std::mem::forget(",
        "mem::forget(",
        "loop { break",
    ];

    let is_terminator = terminating_patterns.iter().any(|p| {
        if p.ends_with('(') {
            prev_line.contains(p) && prev_line.ends_with(';')
        } else if p.ends_with(';') || p.ends_with("break") {
            prev_line.starts_with(p)
        } else {
            prev_line.starts_with(p) && prev_line.ends_with(';')
        }
    });

    if !is_terminator {
        return None;
    }

    // Don't flag empty lines, closing braces, or comments after a return
    let current = lines[idx].trim();
    if current.is_empty() || current == "}" || current.starts_with("//") || current.starts_with("/*") || current.starts_with('*') {
        return None;
    }

    Some(format!(
        "Code after `{}` at line {} may be unreachable",
        prev_line,
        idx + 1 // 1-indexed for user display
    ))
}

/// Check if a function is a single-use helper (definition appears once
/// as a fn, and is referenced exactly once elsewhere).
fn check_single_use_helpers(
    fn_defs: &HashMap<String, Vec<usize>>,
    fn_calls: &HashMap<String, Vec<usize>>,
    lines: &[&str],
) -> Vec<RuleViolation> {
    let mut violations = Vec::new();
    for (name, def_lines) in fn_defs {
        let call_count = fn_calls.get(name).map(|c| c.len()).unwrap_or(0);
        let def_count = def_lines.len();

        // Only flag functions defined within this file that are called once
        // or zero times (private helpers no one calls)
        if def_count == 1 && call_count == 1 {
            let line_no = def_lines[0] + 1; // 1-indexed
            violations.push(RuleViolation {
                rule: "ast:single-use-helper".into(),
                description: format!(
                    "Function `{}` (defined at line {}) is only called once — consider inlining",
                    name, line_no
                ),
                severity: ViolationSeverity::Warning,
            });
        }

        // Zero-call private functions (not pub) are dead code
        if def_count == 1 && call_count == 0 {
            let line_no = def_lines[0] + 1;
            // Check if it's a public function — we can only approximate
            let def_line = lines[def_lines[0]].trim();
            // Skip `main` (called by the runtime) and pub functions
            if name != "main"
                && !def_line.trim_start().starts_with("pub ")
                && !def_line.trim_start().starts_with("pub(")
            {
                violations.push(RuleViolation {
                    rule: "ast:unused-function".into(),
                    description: format!(
                        "Function `{}` (defined at line {}) is defined but never called — dead code",
                        name, line_no
                    ),
                    severity: ViolationSeverity::Warning,
                });
            }
        }
    }
    violations
}

/// Check for over-abstraction: function whose body is just a single
/// call to another function (thin wrapper with no added logic).
fn check_over_abstraction(
    fn_defs: &HashMap<String, Vec<usize>>,
    lines: &[&str],
) -> Vec<RuleViolation> {
    let mut violations = Vec::new();

    for (name, def_lines) in fn_defs {
        for &def_line in def_lines {
            // Collect the body lines of this function
            let body = collect_fn_body(lines, def_line);
            if body.is_empty() {
                continue;
            }

            // A thin wrapper: body has exactly one statement that's a function call
            // Count actual statements (non-empty, non-brace, non-comment, non-fn-def lines)
            let stmts: Vec<&str> = body
                .iter()
                .map(|l| l.trim())
                .filter(|l| {
                    !l.is_empty()
                        && *l != "{"
                        && *l != "}"
                        && !l.starts_with("//")
                        && !l.starts_with("/*")
                        && !l.starts_with('*')
                        && !l.starts_with("fn ")
                })
                .collect();

            if stmts.len() == 1 {
                let stmt = stmts[0];
                // Check if it's just calling another function (possibly with semicolon)
                let clean_stmt = stmt.trim_end_matches(';').trim();
                if clean_stmt.contains('(') && clean_stmt.ends_with(')') {
                    // Extract the called function name
                    if let Some(called_name) = clean_stmt.split('(').next() {
                        let called_name = called_name.trim();
                        // Skip if calling self recursively
                        if called_name != name && !called_name.is_empty() && !called_name.starts_with('&') && !called_name.starts_with('*') {
                            violations.push(RuleViolation {
                                rule: "ast:over-abstraction".into(),
                                description: format!(
                                    "Function `{}` (line {}) is a thin wrapper around `{}` — inline it",
                                    name, def_line + 1, called_name
                                ),
                                severity: ViolationSeverity::Warning,
                            });
                        }
                    }
                }
            }
        }
    }

    violations
}

/// Collect the lines comprising a function body, given the definition line.
fn collect_fn_body<'a>(lines: &'a [&str], def_line: usize) -> Vec<&'a str> {
    let mut body = Vec::new();
    let mut brace_depth: i32 = 0;
    let mut in_body = false;

    for (i, line) in lines.iter().copied().enumerate().skip(def_line) {
        for ch in line.chars() {
            if ch == '{' {
                brace_depth += 1;
                in_body = true;
            } else if ch == '}' {
                brace_depth -= 1;
            }
        }
        if in_body {
            body.push(line);
            if brace_depth <= 0 && i > def_line {
                break;
            }
        }
    }

    body
}

/// Detect long if-else chains that should be match statements.
fn check_if_else_chains(lines: &[&str]) -> Vec<RuleViolation> {
    let mut violations = Vec::new();

    let mut i = 0;
    while i < lines.len() {
        let trimmed = lines[i].trim();

        // Check for `if` not preceded by `else` (start of a chain)
        // Must handle `if ... {` and not `} else if ... {` or `else if ... {`
        if (trimmed.starts_with("if ") || trimmed.starts_with("if("))
            && !trimmed.starts_with("else if")
            && !lines[i].contains("else if")
            && !trimmed.starts_with("//")
            && !trimmed.starts_with("/*")
        {
            let mut chain_len = 1;
            let start = i;

            // Look ahead for else if branches
            let mut j = i + 1;
            let mut brace_depth = count_opening_braces(trimmed) - count_closing_braces(trimmed);
            while j < lines.len() {
                let l = lines[j].trim();

                // Track brace depth
                brace_depth += count_opening_braces(l) - count_closing_braces(l);
                let has_else_if = l.contains("else if");
                let has_else_block = (l.contains("else {") || l.contains("else{")) && !l.contains("else if");

                if has_else_if && !has_else_block {
                    // We should only count a new branch if the else-if starts at the
                    // same or lower indentation as the original if (i.e., same scope level)
                    if brace_depth <= 1 {
                        chain_len += 1;
                    }
                } else if has_else_block && brace_depth <= 1 {
                    // Final `else { }` is not counted in chain length — it's the
                    // catch-all, not a condition. We just note the chain ends here.
                    break;
                }

                // If brace depth drops to 0 (or below), we've exited the entire
                // if-else construct
                if brace_depth <= 0 && (l == "}" || l.starts_with("}")) && !has_else_if {
                    break;
                }

                j += 1;
            }

            if chain_len >= 3 {
                violations.push(RuleViolation {
                    rule: "ast:if-else-chain".into(),
                    description: format!(
                        "Long if-else chain with {} branches starting at line {} — consider `match`",
                        chain_len, start + 1
                    ),
                    severity: ViolationSeverity::Warning,
                });
            }
        }
        i += 1;
    }

    violations
}

fn count_opening_braces(s: &str) -> i32 {
    // Count only braces outside of string literals (simplified)
    s.chars().filter(|&c| c == '{').count() as i32
}

fn count_closing_braces(s: &str) -> i32 {
    s.chars().filter(|&c| c == '}').count() as i32
}

/// Check for AI narration comments across the file.
fn check_narration_comments(lines: &[&str]) -> Vec<RuleViolation> {
    let mut violations = Vec::new();

    for (i, line) in lines.iter().enumerate() {
        let trimmed = line.trim();

        // Only check comments
        if !trimmed.starts_with("//") && !trimmed.starts_with("/*") && !trimmed.starts_with("*") {
            continue;
        }

        // Remove comment markers for pattern matching
        let comment_text = if let Some(s) = trimmed.strip_prefix("//") {
            s
        } else if let Some(s) = trimmed.strip_prefix("/*") {
            // strip_prefix on "/*" strips both chars — same as "//" for 2-char prefix
            s
        } else if let Some(s) = trimmed.strip_prefix('*') {
            s
        } else {
            continue;
        };
        let comment_text = comment_text.to_lowercase();

        // Check each pattern
        for pattern in NARRATION_PATTERNS {
            if comment_text.contains(pattern) {
                violations.push(RuleViolation {
                    rule: "ast:narration-comment".into(),
                    description: format!(
                        "AI narration comment at line {}: matches pattern '{}'",
                        i + 1,
                        pattern
                    ),
                    severity: ViolationSeverity::Warning,
                });
                break; // One violation per comment line
            }
        }
    }

    violations
}

// ── Public API ──────────────────────────────────────────────────────────────

/// Run all AST-level checks against a Rust source file.
///
/// This performs structural analysis that goes beyond what simple regex
/// can detect: function reference counting, block scoping for unreachable
/// code detection, if-else chain length measurement, and multi-line
/// comment pattern analysis anchored to specific line numbers.
pub fn check_ast(source: &str) -> RulesCheck {
    let lines: Vec<&str> = source.lines().collect();
    let mut violations: Vec<RuleViolation> = Vec::new();

    // ── Phase 1: Scan for function definitions and calls ──
    let mut fn_defs: HashMap<String, Vec<usize>> = HashMap::new();
    let mut fn_calls: HashMap<String, Vec<usize>> = HashMap::new();

    for (i, line) in lines.iter().enumerate() {
        if is_fn_def(line)
            && let Some(name) = extract_fn_name(line) {
                fn_defs.entry(name).or_default().push(i);
            }
    }

    // Phase 1b: Collect defined function names for call detection
    let defined_names: Vec<String> = fn_defs.keys().cloned().collect();

    for (i, line) in lines.iter().enumerate() {
        let calls = extract_fn_calls(line, &defined_names);
        for call in calls {
            fn_calls.entry(call).or_default().push(i);
        }
    }

    // ── Phase 2: Single-use helper detection ──
    violations.extend(check_single_use_helpers(&fn_defs, &fn_calls, &lines));

    // ── Phase 3: Over-abstraction detection ──
    violations.extend(check_over_abstraction(&fn_defs, &lines));

    // ── Phase 4: Unreachable code detection ──
    for i in 0..lines.len() {
        if let Some(desc) = has_unreachable_after(&lines, i) {
            violations.push(RuleViolation {
                rule: "ast:unreachable-code".into(),
                description: desc,
                severity: ViolationSeverity::Warning,
            });
        }
    }

    // ── Phase 5: AI narration comments ──
    violations.extend(check_narration_comments(&lines));

    // ── Phase 6: if-else chain detection ──
    violations.extend(check_if_else_chains(&lines));

    // ── Decision ──
    let passed = violations.iter().all(|v| v.severity != ViolationSeverity::Fatal);
    RulesCheck { passed, violations }
}

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

    #[test]
    fn test_clean_code_passes() {
        let source = r#"
fn add(a: i32, b: i32) -> i32 {
    a + b
}

fn multiply(x: i32, y: i32) -> i32 {
    x * y
}

pub fn main() {
    let result = add(1, 2);
    let product = multiply(3, 4);
    println!("{}", result + product);
}
"#;
        let check = check_ast(source);
        assert!(check.passed, "Clean code should pass, got violations: {:?}", check.violations);
    }

    #[test]
    fn test_narration_comments_detected() {
        let source = r#"
// Now we create the config parser
fn parse_config(input: &str) -> Config {
    // Here we implement the parsing logic
    Config { data: input.to_string() }
}

// This function handles the validation
fn validate(cfg: &Config) -> bool {
    // First we check the input
    cfg.data.len() > 0
}
"#;
        let check = check_ast(source);
        let narration_violations: Vec<_> = check.violations.iter().filter(|v| v.rule == "ast:narration-comment").collect();
        assert!(narration_violations.len() >= 3, "Expected at least 3 narration comment violations, got {}", narration_violations.len());
    }

    #[test]
    fn test_single_use_helper_detected() {
        let source = r#"
fn helper_parse_token(s: &str) -> &str {
    s.trim()
}

fn main() {
    let data = " hello ";
    let cleaned = helper_parse_token(data);
    println!("{}", cleaned);
}
"#;
        let check = check_ast(source);
        let single_use: Vec<_> = check.violations.iter().filter(|v| v.rule == "ast:single-use-helper").collect();
        assert_eq!(single_use.len(), 1, "Should detect single-use helper function");
        assert!(single_use[0].description.contains("helper_parse_token"));
    }

    #[test]
    fn test_over_abstraction_detected() {
        let source = r#"
fn do_real_work(x: i32) -> i32 {
    x * 2 + 1
}

fn thin_wrapper(value: i32) -> i32 {
    do_real_work(value)
}

fn another_thin_wrapper(a: i32) -> i32 {
    do_real_work(a)
}
"#;
        let check = check_ast(source);
        let wrappers: Vec<_> = check.violations.iter().filter(|v| v.rule == "ast:over-abstraction").collect();
        assert_eq!(wrappers.len(), 2, "Should detect both thin wrappers");
    }

    #[test]
    fn test_unreachable_code_detected() {
        let source = r#"
fn example() {
    let x = 42;
    return;
    let y = x + 1;
    println!("{}", y);
}
"#;
        let check = check_ast(source);
        let unreachable: Vec<_> = check.violations.iter().filter(|v| v.rule == "ast:unreachable-code").collect();
        assert!(!unreachable.is_empty(), "Should detect unreachable code after return");
    }

    #[test]
    fn test_if_else_chain_detected() {
        let source = r#"
fn classify(value: i32) -> &'static str {
    if value == 1 {
        "one"
    } else if value == 2 {
        "two"
    } else if value == 3 {
        "three"
    } else if value == 4 {
        "four"
    } else {
        "other"
    }
}
"#;
        let check = check_ast(source);
        let chains: Vec<_> = check.violations.iter().filter(|v| v.rule == "ast:if-else-chain").collect();
        assert!(!chains.is_empty(), "Should detect long if-else chain, got: {:?}", check.violations);
    }
    #[test]
    fn test_short_if_else_not_flagged() {
        let source = r#"
fn classify(value: i32) -> &'static str {
    if value == 1 {
        "one"
    } else if value == 2 {
        "two"
    } else {
        "other"
    }
}
"#;
        let check = check_ast(source);
        let chains: Vec<_> = check.violations.iter().filter(|v| v.rule == "ast:if-else-chain").collect();
        assert!(chains.is_empty(), "Short if-else chain (2 branches) should not be flagged");
    }

    #[test]
    fn test_unused_function_detected() {
        let source = r#"
fn helper_internal(x: i32) -> i32 {
    x * 2
}

pub fn called_fn() -> i32 {
    42
}

fn main() {
    let _ = called_fn();
}
"#;
        let check = check_ast(source);
        let unused: Vec<_> = check.violations.iter().filter(|v| v.rule == "ast:unused-function").collect();
        assert_eq!(unused.len(), 1, "Should detect one unused function");
        assert!(unused[0].description.contains("helper_internal"));
    }

    #[test]
    fn test_multiple_violation_types() {
        let source = r#"
// Now we set up the logger
fn setup_logger() -> Logger {
    // Here we create a new logger instance
    Logger::new()
}

fn main() {
    let logger = setup_logger();
    logger.log("hello");
    return;
    logger.log("unreachable");
}
"#;
        let check = check_ast(source);
        let rule_types: std::collections::HashSet<&str> = check.violations
            .iter()
            .map(|v| v.rule.as_str())
            .collect();

        // Clean code should have a helper that's called once — flag it
        assert!(rule_types.contains("ast:narration-comment"), "Should have narration comment violations");
        assert!(rule_types.contains("ast:unreachable-code"), "Should have unreachable code violations");
    }

    #[test]
    fn test_fn_def_extraction() {
        assert_eq!(extract_fn_name("fn foo() {}").as_deref(), Some("foo"));
        assert_eq!(extract_fn_name("pub fn bar(x: i32) -> i32").as_deref(), Some("bar"));
        assert_eq!(extract_fn_name("async fn baz()").as_deref(), Some("baz"));
        assert_eq!(extract_fn_name("pub async fn qux()").as_deref(), Some("qux"));
        assert_eq!(extract_fn_name("fn with_generics<T: Clone>(x: T)").as_deref(), Some("with_generics"));
        assert!(extract_fn_name("// just a comment").is_none());
        assert!(extract_fn_name("#[derive(Debug)]").is_none());
    }

    #[test]
    fn test_strip_modifiers() {
        assert_eq!(strip_visibility_and_modifiers("pub fn foo"), "fn foo");
        assert_eq!(strip_visibility_and_modifiers("pub(crate) fn bar"), "fn bar");
        assert_eq!(strip_visibility_and_modifiers("async fn baz"), "fn baz");
        assert_eq!(strip_visibility_and_modifiers("pub unsafe fn qux"), "fn qux");
    }

    #[test]
    fn test_fn_body_collection() {
        let lines: Vec<&str> = r#"
fn foo() {
    let x = 1;
    let y = 2;
    x + y
}
fn bar() {
    42
}
"#
        .lines()
        .collect();

        let body = collect_fn_body(&lines, 1);
        assert!(!body.is_empty(), "Should collect function body");
        assert!(body.iter().any(|l| l.contains("let x = 1")));
        assert!(body.iter().any(|l| l.contains("x + y")));

        let body2 = collect_fn_body(&lines, 6);
        assert!(body2.iter().any(|l| l.contains("42")), "Should collect bar's body");
    }
}