code-buster 0.11.0

Repository-aware static analysis for AI-assisted development
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
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
fn analyze_runtime(
    context: &LineContext<'_>,
    file_lower: &str,
    state: &mut ScanState,
    findings: &mut Vec<Finding>,
) {
    analyze_security(context, state, findings);
    update_runtime_scopes(context, state);

    let proc_declaration = is_proc_declaration(context.line);
    if proc_declaration {
        analyze_runtime_proc(context, file_lower, state, findings);
    }
    if proc_declaration && HOT_PROC.is_match(context.lower) {
        analyze_hot_proc(context, state, findings);
    }

    analyze_runtime_loops(context, state, findings);
    analyze_runtime_lifecycle(context, proc_declaration, state, findings);
    analyze_runtime_rendering(context, state, findings);
    analyze_runtime_simulation(context, proc_declaration, state, findings);
    analyze_runtime_tail(context, proc_declaration, state, findings);
}

fn update_runtime_scopes(context: &LineContext<'_>, state: &mut ScanState) {
    pop_scopes(context.indent, &mut state.loop_indents);
    pop_scopes(context.indent, &mut state.hot_proc_indents);
    pop_scopes(context.indent, &mut state.draw_proc_indents);
    while state
        .loop_collections
        .last()
        .is_some_and(|(_, indent)| context.indent <= *indent)
    {
        state.loop_collections.pop();
    }
}

fn is_proc_declaration(line: &str) -> bool {
    line.starts_with("proc ") || line.starts_with("func ")
}

fn analyze_runtime_proc(
    context: &LineContext<'_>,
    file_lower: &str,
    state: &mut ScanState,
    findings: &mut Vec<Finding>,
) {
    let proc_body = indented_body(context.lines, context.index, context.indent)
        .join("\n")
        .to_ascii_lowercase();
    analyze_average_risk(context, findings);
    analyze_unused_delta_time(context, &proc_body, findings);
    analyze_float_edge_coverage(context, file_lower, state, findings);
}

fn analyze_average_risk(context: &LineContext<'_>, findings: &mut Vec<Finding>) {
    if context.lower.contains("average") && context.lower.contains("openarray") {
        add_line(
            findings,
            context,
            "nim-average-openarray-risk",
            "average-like openArray API needs edge-case handling",
            "medium",
            Some(Severity::Warn),
        );
    }
}

fn analyze_unused_delta_time(
    context: &LineContext<'_>,
    proc_body: &str,
    findings: &mut Vec<Finding>,
) {
    let has_dt = (context.lower.contains("dt:") || context.lower.contains("delta"))
        && !context.lower.contains("_dt:")
        && !context.lower.contains("_delta");
    if has_dt && UPDATE_WORD.is_match(context.lower) && !DT_USE.is_match(proc_body) {
        add_line(
            findings,
            context,
            "nim-dt-not-used",
            "update-like proc accepts dt but does not appear to use it",
            "low",
            Some(Severity::Info),
        );
    }
}

fn analyze_float_edge_coverage(
    context: &LineContext<'_>,
    file_lower: &str,
    state: &mut ScanState,
    findings: &mut Vec<Finding>,
) {
    if state.float_edge_finding_added
        || !context.line.contains('*')
        || !FLOAT_API.is_match(context.lower)
        || FLOAT_EDGE.is_match(file_lower)
    {
        return;
    }
    push_str(
        findings,
        "nim-float-tests-missing-edge-cases",
        context.path,
        1,
        "exported floating-point API has no obvious edge-case coverage nearby",
        "low",
        Some(Severity::Info),
    );
    state.float_edge_finding_added = true;
}

fn analyze_hot_proc(
    context: &LineContext<'_>,
    state: &mut ScanState,
    findings: &mut Vec<Finding>,
) {
    state.hot_proc_indents.push(context.indent);
    if context.lower.contains("draw") || context.lower.contains("render") {
        state.draw_proc_indents.push(context.indent);
        state.render_begin_count = 0;
        state.render_end_count = 0;
        state.render_proc_line = context.index + 1;
    }
    analyze_layout_documentation(context, findings);
}

fn analyze_layout_documentation(context: &LineContext<'_>, findings: &mut Vec<Finding>) {
    let layout_sensitive = LAYOUT_TYPE.is_match(context.lower)
        || (LAYOUT_WORD.is_match(context.lower) && context.lower.contains("shape")
            || LAYOUT_WORD.is_match(context.lower) && context.lower.contains("dims"));
    let previous = context
        .index
        .checked_sub(1)
        .map_or("", |index| context.lines[index].trim());
    if layout_sensitive
        && !LAYOUT_DOC.is_match(&previous.to_ascii_lowercase())
        && !context.line.contains("##")
    {
        add_line(
            findings,
            context,
            "nim-layout-assumption-undocumented",
            "layout-sensitive public API lacks obvious shape/layout docs",
            "low",
            Some(Severity::Info),
        );
    }
}

// code-buster-ignore complex-function: a single line scan deliberately dispatches related capture security rules.
fn analyze_security(context: &LineContext<'_>, state: &mut ScanState, findings: &mut Vec<Finding>) {
    pop_scopes(context.indent, &mut state.capture_indents);
    if CAPTURE_START.is_match(context.line) {
        if !state.capture_indents.is_empty() {
            add_line(
                findings,
                context,
                "nim-nested-stdout-capture",
                "nested stdout/stderr capture detected",
                "low",
                Some(Severity::Info),
            );
        }
        state.capture_indents.push(context.indent);
    }
    if let Some(captures) = TAINTED_ASSIGNMENT.captures(context.line) {
        state.tainted_values.insert(captures[1].to_owned());
    }
    if let Some(captures) = TABLE_DECLARATION.captures(context.line)
        && !context.line.contains("initOrderedTable[")
    {
        state.unordered_tables.insert(captures[1].to_owned());
    }
    if EXEC_CALL.is_match(context.line)
        && (context.line.contains('&') || context.line.contains('$'))
        && state
            .tainted_values
            .iter()
            .any(|value| context.line.contains(value))
    {
        add_line(
            findings,
            context,
            "nim-tainted-exec",
            "user/input-derived value flows into process command",
            "low",
            Some(Severity::Warn),
        );
    }
    if context.line.starts_with("for ")
        && state
            .unordered_tables
            .iter()
            .any(|table| context.line.contains(&format!(" in {table}:")))
        && context.lines[context.index + 1..]
            .iter()
            .take(8)
            .any(|body| {
                let body = body.trim();
                body.starts_with("echo ")
                    || body.contains("result.add")
                    || body.contains("rows.add")
            })
    {
        add_line(
            findings,
            context,
            "nim-unordered-table-output",
            "unordered Table iteration contributes directly to ordered output",
            "medium",
            Some(Severity::Warn),
        );
    }
}

fn analyze_runtime_loops(
    context: &LineContext<'_>,
    state: &mut ScanState,
    findings: &mut Vec<Finding>,
) {
    record_runtime_loop(context, state);
    analyze_loop_file_concat(context, state, findings);
    analyze_hot_loop_allocation(context, state, findings);
    analyze_game_loop_allocation(context, state, findings);
    analyze_hot_loop_blocking_io(context, state, findings);
    analyze_entity_growth(context, state, findings);
    analyze_draw_asset_load(context, state, findings);
    analyze_render_randomness(context, state, findings);
}

fn record_runtime_loop(context: &LineContext<'_>, state: &mut ScanState) {
    if !context.line.starts_with("for ") && !context.line.starts_with("while ") {
        return;
    }
    state.loop_indents.push(context.indent);
    if let Some(captures) = LOOP_COLLECTION.captures(context.line) {
        state
            .loop_collections
            .push((captures[1].to_owned(), context.indent));
    }
}

fn analyze_loop_file_concat(
    context: &LineContext<'_>,
    state: &ScanState,
    findings: &mut Vec<Finding>,
) {
    if !state.loop_indents.is_empty()
        && context.lower.contains("result")
        && (context.lower.contains("&= readfile")
            || context.lower.contains("= result &") && context.lower.contains("readfile"))
    {
        add_line(
            findings,
            context,
            "nim-readfile-concat-temp",
            "readFile result is concatenated in a loop",
            "medium",
            Some(Severity::Info),
        );
    }
}

fn analyze_hot_loop_allocation(
    context: &LineContext<'_>,
    state: &ScanState,
    findings: &mut Vec<Finding>,
) {
    if !state.loop_indents.is_empty()
        && !state.hot_proc_indents.is_empty()
        && HOT_ALLOCATION.is_match(context.lower)
    {
        add_line(
            findings,
            context,
            "nim-hot-loop-allocation",
            "allocation-like operation inside loop",
            "low",
            Some(Severity::Info),
        );
    }
}

fn analyze_game_loop_allocation(
    context: &LineContext<'_>,
    state: &ScanState,
    findings: &mut Vec<Finding>,
) {
    if !state.hot_proc_indents.is_empty() && GAME_ALLOCATION.is_match(context.lower) {
        add_line(
            findings,
            context,
            "nim-game-loop-allocation",
            "allocation-like operation inside update/render loop",
            "low",
            Some(Severity::Info),
        );
    }
}

fn analyze_hot_loop_blocking_io(
    context: &LineContext<'_>,
    state: &ScanState,
    findings: &mut Vec<Finding>,
) {
    if !state.hot_proc_indents.is_empty() && BLOCKING_IO.is_match(context.lower) {
        add_line(
            findings,
            context,
            "nim-update-blocking-io",
            "blocking IO appears inside update/render loop",
            "medium",
            Some(Severity::Warn),
        );
    }
}

fn analyze_entity_growth(
    context: &LineContext<'_>,
    state: &ScanState,
    findings: &mut Vec<Finding>,
) {
    if state.hot_proc_indents.is_empty()
        || !ENTITY_WORD.is_match(context.lower)
        || !(context.lower.contains(".add(") || context.lower.contains(".add "))
    {
        return;
    }
    let nearby = nearby_lines(context.lines, context.index, 5, 5).to_ascii_lowercase();
    if !GROWTH_LIMIT.is_match(&nearby) {
        add_line(
            findings,
            context,
            "nim-unbounded-entity-growth",
            "entity-like collection grows in hot loop without obvious cap/removal",
            "low",
            Some(Severity::Info),
        );
    }
}

fn analyze_draw_asset_load(
    context: &LineContext<'_>,
    state: &ScanState,
    findings: &mut Vec<Finding>,
) {
    if !state.draw_proc_indents.is_empty() && ASSET_LOAD_CALL.is_match(context.lower) {
        add_line(
            findings,
            context,
            "nim-draw-loads-asset",
            "asset loading appears inside draw/render proc",
            "medium",
            Some(Severity::Warn),
        );
    }
}

fn analyze_render_randomness(
    context: &LineContext<'_>,
    state: &ScanState,
    findings: &mut Vec<Finding>,
) {
    if !state.draw_proc_indents.is_empty() && RANDOM_CALL.is_match(context.lower) {
        add_line(
            findings,
            context,
            "nim-random-in-render",
            "random call appears inside draw/render proc",
            "low",
            Some(Severity::Info),
        );
    }
}

fn analyze_runtime_lifecycle(
    context: &LineContext<'_>,
    proc_declaration: bool,
    state: &mut ScanState,
    findings: &mut Vec<Finding>,
) {
    let in_update = !state.hot_proc_indents.is_empty() && state.draw_proc_indents.is_empty();
    analyze_save_version(context, proc_declaration, findings);
    analyze_destroyed_entity_access(context, in_update, state, findings);
    analyze_component_nil_check(context, in_update, findings);
    analyze_nested_collision(context, in_update, state, findings);
}

fn analyze_save_version(
    context: &LineContext<'_>,
    proc_declaration: bool,
    findings: &mut Vec<Finding>,
) {
    if !proc_declaration || !SAVE_PROC.is_match(context.lower) {
        return;
    }
    let body = indented_body(context.lines, context.index, context.indent)
        .join("\n")
        .to_ascii_lowercase();
    if SAVE_WRITE.is_match(&body) && !body.contains("version") {
        push_str(
            findings,
            "nim-save-missing-version",
            context.path,
            1,
            "save proc writes data but has no version field",
            "low",
            Some(Severity::Info),
        );
    }
}

fn analyze_destroyed_entity_access(
    context: &LineContext<'_>,
    in_update: bool,
    state: &mut ScanState,
    findings: &mut Vec<Finding>,
) {
    if !in_update {
        return;
    }
    if ENTITY_LIFECYCLE.is_match(context.lower) {
        record_destroyed_entity(context, state);
        return;
    }
    for entity in &state.destroyed_entities {
        if has_member(context.lower, entity, &[])
            && !has_member(context.lower, entity, &["alive", "dead", "destroyed"])
        {
            add_line(
                findings,
                context,
                "nim-entity-access-after-destroy",
                "entity field accessed after being marked dead/destroyed",
                "low",
                Some(Severity::Warn),
            );
            break;
        }
    }
}

fn record_destroyed_entity(context: &LineContext<'_>, state: &mut ScanState) {
    let Some(captures) = ENTITY_RECEIVER.captures(context.line) else {
        return;
    };
    let entity = captures[1].to_ascii_lowercase();
    if entity.len() > 2 {
        state.destroyed_entities.insert(entity);
    }
}

fn analyze_component_nil_check(
    context: &LineContext<'_>,
    in_update: bool,
    findings: &mut Vec<Finding>,
) {
    if !in_update || !COMPONENT_LOOKUP.is_match(context.lower) {
        return;
    }
    let Some(captures) = ASSIGNED_VALUE.captures(context.line) else {
        return;
    };
    let name = captures[1].to_ascii_lowercase();
    let nearby = context.lines[context.index + 1..]
        .iter()
        .take(5)
        .copied()
        .collect::<Vec<_>>()
        .join("\n")
        .to_ascii_lowercase();
    if has_nil_check(&nearby, &name) {
        return;
    }
    add_line(
        findings,
        context,
        "nim-nil-component-access",
        "component obtained but not checked for nil before use",
        "low",
        Some(Severity::Info),
    );
}

fn has_nil_check(source: &str, name: &str) -> bool {
    source.contains(&format!("{name} != nil"))
        || source.contains(&format!("{name} == nil"))
        || source.contains(&format!("{name}.isnil"))
        || source.contains(&format!("isnil({name}"))
}

fn analyze_nested_collision(
    context: &LineContext<'_>,
    in_update: bool,
    state: &ScanState,
    findings: &mut Vec<Finding>,
) {
    if !in_update || state.loop_indents.len() < 2 || !COLLISION_CALL.is_match(context.lower) {
        return;
    }
    let nearby = context.lines[context.index.saturating_sub(10)..=context.index]
        .join("\n")
        .to_ascii_lowercase();
    if !BROAD_PHASE.is_match(&nearby) {
        add_line(
            findings,
            context,
            "nim-o-n-squared-collision",
            "nested-loop collision check without broad-phase hint",
            "medium",
            Some(Severity::Warn),
        );
    }
}

// code-buster-ignore complex-function: render-loop checks share camera and procedure state in one line scan.
fn analyze_runtime_rendering(
    context: &LineContext<'_>,
    state: &mut ScanState,
    findings: &mut Vec<Finding>,
) {
    let in_draw = !state.draw_proc_indents.is_empty();
    if in_draw && CAMERA_MUTATION.is_match(context.lower) {
        state.camera_modified = true;
        state.camera_proc_line = state.render_proc_line;
    }
    if in_draw && CAMERA_RESTORE.is_match(context.lower) {
        state.camera_restored = true;
    }
    let asset_load = context.lower.contains("load") && ASSET_WORD.is_match(context.lower);
    let asset_free = ASSET_FREE.is_match(context.lower) && ASSET_FREE_WORD.is_match(context.lower);
    state.asset_load_count += usize::from(asset_load);
    state.asset_free_count += usize::from(asset_free);
    if in_draw
        && DRAW_PRIMITIVE.is_match(context.lower)
        && DEBUG_WORD.is_match(&context.raw.to_ascii_lowercase())
    {
        let nearby = context.lines[context.index.saturating_sub(5)..=context.index]
            .join("\n")
            .to_ascii_lowercase();
        if !nearby.contains("if debug") {
            add_line(
                findings,
                context,
                "nim-debug-draw-not-gated",
                "debug-looking draw call not gated by a debug flag",
                "low",
                Some(Severity::Info),
            );
        }
    }
}

fn analyze_runtime_simulation(
    context: &LineContext<'_>,
    proc_declaration: bool,
    state: &ScanState,
    findings: &mut Vec<Finding>,
) {
    let in_update = !state.hot_proc_indents.is_empty() && state.draw_proc_indents.is_empty();
    let in_draw = !state.draw_proc_indents.is_empty();
    analyze_simulation_configuration(context, in_update, in_draw, findings);
    analyze_update_rendering(context, proc_declaration, in_update, findings);
    analyze_simulation_comparisons(context, in_update, in_draw, findings);
    analyze_simulation_determinism(context, in_update, findings);
    analyze_per_frame_formatting(context, proc_declaration, in_update, in_draw, findings);
    analyze_input_polling(context, proc_declaration, in_draw, findings);
    analyze_sound_playback(context, in_update, in_draw, findings);
    analyze_hot_loop_saves(context, in_update, in_draw, findings);
}

fn analyze_simulation_configuration(
    context: &LineContext<'_>,
    in_update: bool,
    in_draw: bool,
    findings: &mut Vec<Finding>,
) {
    if (in_update || in_draw)
        && SCREEN_DIMENSION.is_match(context.lower)
        && SCREEN_NUMBER.is_match(context.lower)
        && !CONFIG_WORD.is_match(context.lower)
    {
        add_line(
            findings,
            context,
            "nim-hardcoded-screen-size",
            "screen dimension appears hardcoded instead of using config",
            "low",
            Some(Severity::Info),
        );
    }
    if in_update && context.lower.contains("* dt") && PHYSICS_WORD.is_match(context.lower) {
        let nearby = nearby_lines(context.lines, context.index, 10, 10).to_ascii_lowercase();
        if !nearby.contains("fixed") {
            add_line(
                findings,
                context,
                "nim-physics-variable-timestep",
                "physics integration uses variable dt without fixed timestep",
                "low",
                Some(Severity::Info),
            );
        }
    }
}

fn analyze_update_rendering(
    context: &LineContext<'_>,
    proc_declaration: bool,
    in_update: bool,
    findings: &mut Vec<Finding>,
) {
    if in_update
        && !proc_declaration
        && DRAW_IN_UPDATE.is_match(context.lower)
        && !context.lower.contains("debug")
    {
        add_line(
            findings,
            context,
            "nim-draw-call-in-update",
            "draw/render call appears inside update proc",
            "medium",
            Some(Severity::Warn),
        );
    }
}

fn analyze_simulation_comparisons(
    context: &LineContext<'_>,
    in_update: bool,
    in_draw: bool,
    findings: &mut Vec<Finding>,
) {
    if in_update && context.line.contains(" == ") && PHYSICS_FLOAT.is_match(context.lower) {
        add_line(
            findings,
            context,
            "nim-float-equality-physics",
            "floating-point equality comparison in update/sim code",
            "low",
            Some(Severity::Info),
        );
    }
    if (in_update || in_draw)
        && DISTANCE_CALL.is_match(context.lower)
        && context.lower.contains(" == 0")
    {
        add_line(
            findings,
            context,
            "nim-missing-epsilon-distance",
            "distance compared exactly to zero",
            "low",
            Some(Severity::Info),
        );
    }
}

fn analyze_simulation_determinism(
    context: &LineContext<'_>,
    in_update: bool,
    findings: &mut Vec<Finding>,
) {
    if in_update
        && RAND_SIMULATION.is_match(context.lower)
        && !context.lower.contains("rng")
        && !context.lower.contains("seed")
    {
        add_line(
            findings,
            context,
            "nim-random-in-simulation",
            "unseeded random call in update/sim proc",
            "low",
            Some(Severity::Info),
        );
    }
    if in_update && WALL_CLOCK.is_match(context.lower) {
        add_line(
            findings,
            context,
            "nim-wall-clock-in-update",
            "wall-clock time used inside update/sim proc",
            "medium",
            Some(Severity::Warn),
        );
    }
}

fn analyze_per_frame_formatting(
    context: &LineContext<'_>,
    proc_declaration: bool,
    in_update: bool,
    in_draw: bool,
    findings: &mut Vec<Finding>,
) {
    if (in_update || in_draw)
        && !proc_declaration
        && (context.raw.contains("$\"")
            || context.raw.contains("$(")
            || context.lower.contains(".format(")
            || context.lower.contains("formatstring"))
    {
        add_line(
            findings,
            context,
            "nim-per-frame-string-format",
            "string formatting appears inside update/draw loop",
            "low",
            Some(Severity::Info),
        );
    }
}

fn analyze_input_polling(
    context: &LineContext<'_>,
    proc_declaration: bool,
    in_draw: bool,
    findings: &mut Vec<Finding>,
) {
    if in_draw && !proc_declaration && INPUT_CALL.is_match(context.lower) {
        add_line(
            findings,
            context,
            "nim-input-in-draw",
            "input polling appears inside draw/render proc",
            "medium",
            Some(Severity::Warn),
        );
    }
}

fn analyze_sound_playback(
    context: &LineContext<'_>,
    in_update: bool,
    in_draw: bool,
    findings: &mut Vec<Finding>,
) {
    if !(in_update || in_draw) || !SOUND_CALL.is_match(context.lower) {
        return;
    }
    let nearby = context.lines[context.index.saturating_sub(3)..=context.index]
        .join("\n")
        .to_ascii_lowercase();
    if !nearby.contains("pressed") {
        add_line(
            findings,
            context,
            "nim-sound-every-frame",
            "sound played in hot loop without an edge/trigger guard",
            "medium",
            Some(Severity::Warn),
        );
    }
}

fn analyze_hot_loop_saves(
    context: &LineContext<'_>,
    in_update: bool,
    in_draw: bool,
    findings: &mut Vec<Finding>,
) {
    if (in_update || in_draw) && SAVE_CALL.is_match(context.lower) {
        add_line(
            findings,
            context,
            "nim-save-in-update",
            "save/write appears inside update/draw loop",
            "medium",
            Some(Severity::Warn),
        );
    }
}

// code-buster-ignore complex-function: end-of-line runtime checks share loop and render state.
fn analyze_runtime_tail(
    context: &LineContext<'_>,
    proc_declaration: bool,
    state: &mut ScanState,
    findings: &mut Vec<Finding>,
) {
    if !state.draw_proc_indents.is_empty() {
        if RENDER_BEGIN.is_match(context.lower) {
            state.render_begin_count += 1;
        }
        if RENDER_END.is_match(context.lower) {
            state.render_end_count += 1;
        }
    }
    if !state.loop_collections.is_empty() && !context.line.starts_with("for ") {
        for (collection, _) in &state.loop_collections {
            if has_member_call(
                context.lower,
                collection,
                &["add", "delete", "del", "remove", "pop"],
            ) {
                add_line(
                    findings,
                    context,
                    "nim-mutate-while-iterating",
                    "collection appears to be mutated while being iterated",
                    "medium",
                    Some(Severity::Warn),
                );
                break;
            }
        }
    }
    if proc_declaration
        && let (Some(open), Some(close)) = (context.line.find('('), context.line.rfind(')'))
        && close > open
    {
        let names = context.line[open + 1..close]
            .split(',')
            .filter_map(|segment| segment.split(':').next())
            .map(str::trim)
            .filter(|candidate| IDENTIFIER.is_match(candidate))
            .map(str::to_ascii_lowercase)
            .collect::<BTreeSet<_>>();
        state.proc_parameters.push((context.index + 1, names));
    }
}