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
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
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
//! Direct signal detectors for the StyleSignal system.
//!
//! Each detector implements `SignalDetector` and produces scores
//! directly from parsed AST files, bypassing the Rule → Issue pipeline.

use crate::language::Language;
use crate::signals::{SignalDetector, StyleSignal};
use crate::style_ir::StyleIr;
use crate::treesitter::duplication::IntraFileDupDetector;
use crate::treesitter::engine::ParsedFile;

/// All languages that have a LanguageAdapter implementation.
const ADAPTER_LANGUAGES: &[Language] = &[
    Language::Rust,
    Language::Python,
    Language::JavaScript,
    Language::TypeScript,
    Language::Go,
    Language::Java,
    Language::Ruby,
    Language::Swift,
    Language::Zig,
    Language::C,
    Language::Cpp,
];

// ── PanicAddiction Detector ───────────────────────────────────────

/// Detects PanicAddiction signal: .unwrap(), .expect(), panic!() calls.
pub struct PanicAddictionDetector;

impl PanicAddictionDetector {
    pub fn new() -> Self {
        Self
    }
}

impl Default for PanicAddictionDetector {
    fn default() -> Self {
        Self::new()
    }
}

impl SignalDetector for PanicAddictionDetector {
    fn signal(&self) -> StyleSignal {
        StyleSignal::PanicAddiction
    }

    fn supported_languages(&self) -> &'static [Language] {
        ADAPTER_LANGUAGES
    }

    fn count_violations(&self, file: &ParsedFile) -> usize {
        StyleIr::from_parsed(file)
            .map(|ir| ir.panic_call_count)
            .unwrap_or(0)
    }

    fn count_violations_with_ir(&self, ir: &StyleIr, _file: &ParsedFile) -> usize {
        ir.panic_call_count
    }
}

// ── NamingChaos Detector ─────────────────────────────────────────

/// Detects NamingChaos signal: single-letter vars, terrible/meaningless names,
/// Hungarian notation, and abbreviation abuse.
pub struct NamingChaosDetector;

impl NamingChaosDetector {
    pub fn new() -> Self {
        Self
    }
}

impl Default for NamingChaosDetector {
    fn default() -> Self {
        Self::new()
    }
}

impl SignalDetector for NamingChaosDetector {
    fn signal(&self) -> StyleSignal {
        StyleSignal::NamingChaos
    }

    fn supported_languages(&self) -> &'static [Language] {
        ADAPTER_LANGUAGES
    }

    fn count_violations(&self, file: &ParsedFile) -> usize {
        StyleIr::from_parsed(file)
            .map(|ir| ir.naming_violation_count)
            .unwrap_or(0)
    }

    fn count_violations_with_ir(&self, ir: &StyleIr, _file: &ParsedFile) -> usize {
        ir.naming_violation_count
    }
}

// ── NestedHell Detector ──────────────────────────────────────────

/// Detects NestedHell signal: deeply-nested block scopes (≥5 levels).
pub struct NestedHellDetector;

impl NestedHellDetector {
    pub fn new() -> Self {
        Self
    }
}

impl Default for NestedHellDetector {
    fn default() -> Self {
        Self::new()
    }
}

impl SignalDetector for NestedHellDetector {
    fn signal(&self) -> StyleSignal {
        StyleSignal::NestedHell
    }

    fn supported_languages(&self) -> &'static [Language] {
        ADAPTER_LANGUAGES
    }

    fn count_violations(&self, file: &ParsedFile) -> usize {
        StyleIr::from_parsed(file)
            .map(|ir| ir.deeply_nested_block_count + ir.defer_in_loop_count)
            .unwrap_or(0)
    }

    fn count_violations_with_ir(&self, ir: &StyleIr, _file: &ParsedFile) -> usize {
        ir.deeply_nested_block_count + ir.defer_in_loop_count
    }
}

// ── HotfixCulture Detector ────────────────────────────────────────

/// Detects HotfixCulture signal: println!, dbg!, todo!, unimplemented! calls.
pub struct HotfixCultureDetector;

impl HotfixCultureDetector {
    pub fn new() -> Self {
        Self
    }
}

impl Default for HotfixCultureDetector {
    fn default() -> Self {
        Self::new()
    }
}

impl SignalDetector for HotfixCultureDetector {
    fn signal(&self) -> StyleSignal {
        StyleSignal::HotfixCulture
    }

    fn supported_languages(&self) -> &'static [Language] {
        ADAPTER_LANGUAGES
    }

    fn count_violations(&self, file: &ParsedFile) -> usize {
        StyleIr::from_parsed(file)
            .map(|ir| ir.debug_call_count)
            .unwrap_or(0)
    }

    fn count_violations_with_ir(&self, ir: &StyleIr, _file: &ParsedFile) -> usize {
        ir.debug_call_count
    }
}

// ── OverEngineering Detector ─────────────────────────────────────

/// Detects OverEngineering signal: god functions (>50 lines) and excessive params (>5).
pub struct OverEngineeringDetector;

impl OverEngineeringDetector {
    pub fn new() -> Self {
        Self
    }
}

impl Default for OverEngineeringDetector {
    fn default() -> Self {
        Self::new()
    }
}

impl SignalDetector for OverEngineeringDetector {
    fn signal(&self) -> StyleSignal {
        StyleSignal::OverEngineering
    }

    fn supported_languages(&self) -> &'static [Language] {
        ADAPTER_LANGUAGES
    }

    fn count_violations(&self, file: &ParsedFile) -> usize {
        StyleIr::from_parsed(file)
            .map(|ir| ir.over_engineering_count())
            .unwrap_or(0)
    }

    fn count_violations_with_ir(&self, ir: &StyleIr, _file: &ParsedFile) -> usize {
        ir.over_engineering_count()
    }
}

// ── CodeSmells Detector ────────────────────────────────────────────

/// Detects CodeSmells signal: unsafe blocks, magic numbers, unnecessary clone, etc.
pub struct CodeSmellsDetector;

impl CodeSmellsDetector {
    pub fn new() -> Self {
        Self
    }
}

impl Default for CodeSmellsDetector {
    fn default() -> Self {
        Self::new()
    }
}

impl SignalDetector for CodeSmellsDetector {
    fn signal(&self) -> StyleSignal {
        StyleSignal::CodeSmells
    }

    fn supported_languages(&self) -> &'static [Language] {
        ADAPTER_LANGUAGES
    }

    fn count_violations(&self, file: &ParsedFile) -> usize {
        StyleIr::from_parsed(file)
            .map(|ir| ir.code_smell_count())
            .unwrap_or(0)
    }

    fn count_violations_with_ir(&self, ir: &StyleIr, _file: &ParsedFile) -> usize {
        ir.code_smell_count()
    }
}

// ── Duplication Detector ───────────────────────────────────────────

/// Detects Duplication signal: intra-file duplicated code blocks.
///
/// Cross-file duplication detection is stateful (accumulates fingerprints
/// across all files) and is handled separately in the analysis pipeline.
pub struct DuplicationDetector;

impl DuplicationDetector {
    pub fn new() -> Self {
        Self
    }
}

impl Default for DuplicationDetector {
    fn default() -> Self {
        Self::new()
    }
}

impl SignalDetector for DuplicationDetector {
    fn signal(&self) -> StyleSignal {
        StyleSignal::Duplication
    }

    fn supported_languages(&self) -> &'static [Language] {
        ADAPTER_LANGUAGES
    }

    fn count_violations(&self, file: &ParsedFile) -> usize {
        IntraFileDupDetector::check(file).len()
    }
}

// ── LegacyCode Detector ─────────────────────────────────────────────

/// Detects LegacyCode signal: blocks of commented-out code left in source.
pub struct LegacyCodeDetector;

impl LegacyCodeDetector {
    pub fn new() -> Self {
        Self
    }
}

impl Default for LegacyCodeDetector {
    fn default() -> Self {
        Self::new()
    }
}

impl SignalDetector for LegacyCodeDetector {
    fn signal(&self) -> StyleSignal {
        StyleSignal::LegacyCode
    }

    fn supported_languages(&self) -> &'static [Language] {
        ADAPTER_LANGUAGES
    }

    fn skips_test_files(&self) -> bool {
        false
    }

    fn count_violations(&self, file: &ParsedFile) -> usize {
        StyleIr::from_parsed(file)
            .map(|ir| ir.commented_out_lines)
            .unwrap_or(0)
    }

    fn count_violations_with_ir(&self, ir: &StyleIr, _file: &ParsedFile) -> usize {
        ir.commented_out_lines
    }
}

// ── TodoMountain Detector ────────────────────────────────────────────

/// Detects TodoMountain signal: TODO/FIXME/BUG/HACK markers in comments.
pub struct TodoMountainDetector;

impl TodoMountainDetector {
    pub fn new() -> Self {
        Self
    }
}

impl Default for TodoMountainDetector {
    fn default() -> Self {
        Self::new()
    }
}

impl SignalDetector for TodoMountainDetector {
    fn signal(&self) -> StyleSignal {
        StyleSignal::TodoMountain
    }

    fn supported_languages(&self) -> &'static [Language] {
        ADAPTER_LANGUAGES
    }

    fn skips_test_files(&self) -> bool {
        false
    }

    fn count_violations(&self, file: &ParsedFile) -> usize {
        StyleIr::from_parsed(file)
            .map(|ir| ir.todo_count)
            .unwrap_or(0)
    }

    fn count_violations_with_ir(&self, ir: &StyleIr, _file: &ParsedFile) -> usize {
        ir.todo_count
    }
}

// ── LineCountSmell Detector ──────────────────────────────────────────

/// Detects LineCountSmell signal: files exceeding reasonable line thresholds.
pub struct LineCountSmellDetector;

impl LineCountSmellDetector {
    pub fn new() -> Self {
        Self
    }
}

impl Default for LineCountSmellDetector {
    fn default() -> Self {
        Self::new()
    }
}

impl SignalDetector for LineCountSmellDetector {
    fn signal(&self) -> StyleSignal {
        StyleSignal::LineCountSmell
    }

    fn supported_languages(&self) -> &'static [Language] {
        ADAPTER_LANGUAGES
    }

    fn skips_test_files(&self) -> bool {
        false
    }

    fn count_violations(&self, file: &ParsedFile) -> usize {
        let line_count = file.content.lines().count();
        let is_test = file.path.to_string_lossy().contains("test");
        let threshold = if is_test { 2000 } else { 1000 };
        if line_count > threshold {
            line_count
        } else {
            0
        }
    }
}

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

    use crate::treesitter::engine::{ParsedFile, TreeSitterEngine};

    fn parse_rust(source: &str) -> ParsedFile {
        let engine = TreeSitterEngine::new();
        engine
            .parse_file(std::path::Path::new("test.rs"), source)
            .expect("Rust parse should succeed")
    }

    // ── SignalDetector — PanicAddictionDetector ────────────────────

    /// Objective: Verify PanicAddictionDetector finds .unwrap() calls.
    #[test]
    fn test_detector_panic_unwrap() {
        let file = parse_rust("fn main() { let x = val.unwrap(); let y = other.unwrap(); }");
        let detector = PanicAddictionDetector::new();
        let count = detector.count_violations(&file);
        assert_eq!(count, 2, "should find 2 unwrap calls, got {count}");
    }

    /// Objective: Verify PanicAddictionDetector does NOT flag .expect() calls.
    #[test]
    fn test_detector_panic_expect_allowed() {
        let file = parse_rust("fn main() { let x = val.expect(\"msg\"); }");
        let detector = PanicAddictionDetector::new();
        let count = detector.count_violations(&file);
        assert_eq!(count, 0, "expect() is allowed, got {count}");
    }

    /// Objective: Verify PanicAddictionDetector finds panic!() macro calls.
    #[test]
    fn test_detector_panic_macro() {
        let file = parse_rust(
            r#"
fn main() {
    panic!("something went wrong");
    panic!("another panic");
}
"#,
        );
        let detector = PanicAddictionDetector::new();
        let count = detector.count_violations(&file);
        assert_eq!(count, 2, "should find 2 panic!() calls, got {count}");
    }

    /// Objective: Verify detector finds unwrap + panic but not expect.
    #[test]
    fn test_detector_panic_mixed() {
        let file = parse_rust(
            r#"
fn main() {
    let a = x.unwrap();
    let b = y.expect("msg");
    panic!("boom");
}
"#,
        );
        let detector = PanicAddictionDetector::new();
        let count = detector.count_violations(&file);
        assert_eq!(
            count, 2,
            "unwrap + panic = 2, expect is allowed, got {count}"
        );
    }

    // ── SignalDetector — NamingChaosDetector ─────────────────────

    /// Objective: Verify NamingChaosDetector catches single-letter var.
    #[test]
    fn test_detector_naming_single_letter() {
        let file = parse_rust("fn main() { let a = 1; }");
        let detector = NamingChaosDetector::new();
        assert_eq!(detector.count_violations(&file), 1, "single-letter a");
    }

    /// Objective: Verify NamingChaosDetector catches terrible naming.
    #[test]
    fn test_detector_naming_terrible() {
        let file = parse_rust("fn main() { let data = 1; }");
        let detector = NamingChaosDetector::new();
        assert_eq!(detector.count_violations(&file), 1, "terrible name 'data'");
    }

    /// Objective: Verify NamingChaosDetector returns 0 for clean naming.
    #[test]
    fn test_detector_naming_clean() {
        let file = parse_rust("fn main() { let user_name = \"alice\"; }");
        let detector = NamingChaosDetector::new();
        assert_eq!(detector.count_violations(&file), 0, "clean naming");
    }

    // ── SignalDetector — NestedHellDetector ──────────────────────

    /// Objective: Verify NestedHellDetector finds deeply-nested blocks.
    #[test]
    fn test_detector_nested_hell_deep() {
        let file = parse_rust(
            r#"
fn main() {
    if true {
        if true {
            if true {
                if true {
                    if true {
                        if true {
                            let x = 1;
                        }
                    }
                }
            }
        }
    }
}
"#,
        );
        let detector = NestedHellDetector::new();
        let count = detector.count_violations(&file);
        assert!(
            count >= 1,
            "6-level deep nesting should find at least 1 deeply-nested block, got {count}"
        );
    }

    /// Objective: Verify NestedHellDetector returns 0 for flat code.
    #[test]
    fn test_detector_nested_hell_flat() {
        let file = parse_rust(
            r#"
fn main() {
    let x = 1;
    let y = 2;
}
"#,
        );
        let detector = NestedHellDetector::new();
        assert_eq!(
            detector.count_violations(&file),
            0,
            "flat code should have 0 violations"
        );
    }

    /// Objective: Verify NestedHellDetector counts only blocks at depth >= 5.
    #[test]
    fn test_detector_nested_hell_just_under_threshold() {
        let file = parse_rust(
            r#"
fn main() {
    if true {
        if true {
            if true {
                if true {
                    let x = 1;
                }
            }
        }
    }
}
"#,
        );
        let detector = NestedHellDetector::new();
        assert_eq!(
            detector.count_violations(&file),
            0,
            "4-level nesting should be under threshold (5)"
        );
    }

    // ── SignalDetector — HotfixCultureDetector ─────────────────────

    /// Objective: Verify HotfixCultureDetector counts println! calls.
    #[test]
    fn test_detector_hotfix_println() {
        let file = parse_rust(
            r#"
fn main() {
    println!("hello");
    println!("world");
}
"#,
        );
        let detector = HotfixCultureDetector::new();
        assert_eq!(detector.count_violations(&file), 2, "2 println! calls");
    }

    /// Objective: Verify HotfixCultureDetector counts todo! and unimplemented!.
    #[test]
    fn test_detector_hotfix_todo() {
        let file = parse_rust(
            r#"
fn main() {
    todo!("implement this");
    unimplemented!();
}
"#,
        );
        let detector = HotfixCultureDetector::new();
        assert_eq!(
            detector.count_violations(&file),
            2,
            "todo! + unimplemented! = 2"
        );
    }

    /// Objective: Verify HotfixCultureDetector returns 0 for clean code.
    #[test]
    fn test_detector_hotfix_clean() {
        let file = parse_rust(
            r#"
fn add(a: i32, b: i32) -> i32 {
    a + b
}
"#,
        );
        let detector = HotfixCultureDetector::new();
        assert_eq!(detector.count_violations(&file), 0, "no debug calls");
    }

    /// Objective: Verify HotfixCultureDetector counts dbg! and eprintln! too.
    #[test]
    fn test_detector_hotfix_dbg_eprintln() {
        let file = parse_rust(
            r#"
fn main() {
    dbg!(42);
    eprintln!("error!");
    eprint!("warning!");
}
"#,
        );
        let detector = HotfixCultureDetector::new();
        assert_eq!(
            detector.count_violations(&file),
            3,
            "dbg! + eprintln! + eprint! = 3"
        );
    }

    // ── SignalDetector — OverEngineeringDetector ──────────────────

    /// Objective: Verify OverEngineeringDetector counts god functions (>50 lines).
    #[test]
    fn test_detector_overengineering_god_function() {
        let file = parse_rust(
            r#"
fn main() {
    let a = 1;
    let b = 2;
    let c = 3;
    let d = 4;
    let e = 5;
}
"#,
        );
        let detector = OverEngineeringDetector::new();
        // The main function is short (< 50 lines), no god functions
        assert_eq!(
            detector.count_violations(&file),
            0,
            "short function should not count as overengineered"
        );
    }

    /// Objective: Verify OverEngineeringDetector counts excessive params (>5).
    #[test]
    fn test_detector_overengineering_excessive_params() {
        let file = parse_rust(
            r#"
fn process(a: i32, b: i32, c: i32, d: i32, e: i32, f: i32) -> i32 {
    a + b + c + d + e + f
}
"#,
        );
        let detector = OverEngineeringDetector::new();
        assert_eq!(
            detector.count_violations(&file),
            1,
            "function with 6 params should count as violation"
        );
    }

    /// Objective: Verify OverEngineeringDetector is 0 for clean functions.
    #[test]
    fn test_detector_overengineering_clean() {
        let file = parse_rust(
            r#"
fn add(a: i32, b: i32) -> i32 {
    a + b
}
"#,
        );
        let detector = OverEngineeringDetector::new();
        assert_eq!(detector.count_violations(&file), 0, "clean function");
    }

    // ── SignalDetector — CodeSmellsDetector ──────────────────────

    /// Objective: Verify CodeSmellsDetector finds unsafe blocks.
    #[test]
    fn test_detector_code_smells_unsafe() {
        let file = parse_rust(
            r#"
fn main() {
    unsafe {
        let p = 42 as *const i32;
        let _ = *p;
    }
}
"#,
        );
        let detector = CodeSmellsDetector::new();
        let count = detector.count_violations(&file);
        assert!(
            count >= 2,
            "unsafe block (2 points) should be >= 2, got {count}"
        );
    }

    /// Objective: Verify CodeSmellsDetector counts magic numbers in expressions.
    #[test]
    fn test_detector_code_smells_magic() {
        let file = parse_rust(
            r#"
fn main() {
    let x = 1;
    foo(42);
    bar(100);
}
"#,
        );
        let detector = CodeSmellsDetector::new();
        assert_eq!(detector.count_violations(&file), 2, "two magic numbers = 2");
    }

    /// Objective: Verify CodeSmellsDetector skips numbers in const/let declarations.
    #[test]
    fn test_detector_code_smells_const_ok() {
        let file = parse_rust(
            r#"
const MAX: i32 = 100;
fn main() {
    let x = MAX;
}
"#,
        );
        let detector = CodeSmellsDetector::new();
        assert_eq!(
            detector.count_violations(&file),
            0,
            "const value and no-magic should be 0"
        );
    }

    /// Objective: Verify CodeSmellsDetector skips 0 and 1 in trivial expressions.
    #[test]
    fn test_detector_code_smells_trivial_numbers_ok() {
        let file = parse_rust(
            r#"
fn main() {
    let x = 0;
    let y = x + 1;
}
"#,
        );
        let detector = CodeSmellsDetector::new();
        assert_eq!(detector.count_violations(&file), 0, "0 and 1 not magic");
    }

    /// Objective: Verify CodeSmellsDetector returns 0 for clean code.
    #[test]
    fn test_detector_code_smells_clean() {
        let file = parse_rust(
            r#"
fn add(a: i32, b: i32) -> i32 {
    a + b
}
"#,
        );
        let detector = CodeSmellsDetector::new();
        assert_eq!(detector.count_violations(&file), 0, "clean code = 0");
    }

    // ── SignalDetector — DuplicationDetector ─────────────────────

    /// Objective: Verify DuplicationDetector finds intra-file duplication.
    #[test]
    fn test_detector_duplication_intra_file() {
        let file = parse_rust(
            r#"
fn setup_a() {
    let x = 1;
    let y = 2;
    let z = 3;
    let w = 4;
    let v = 5;
}
fn setup_b() {
    let x = 1;
    let y = 2;
    let z = 3;
    let w = 4;
    let v = 5;
}
"#,
        );
        let detector = DuplicationDetector::new();
        let count = detector.count_violations(&file);
        assert!(count >= 1, "duplicated blocks should be >= 1, got {count}");
    }

    /// Objective: Verify DuplicationDetector returns 0 for clean code.
    #[test]
    fn test_detector_duplication_clean() {
        let file = parse_rust(
            r#"
fn add(a: i32, b: i32) -> i32 { a + b }
fn sub(a: i32, b: i32) -> i32 { a - b }
"#,
        );
        let detector = DuplicationDetector::new();
        assert_eq!(detector.count_violations(&file), 0, "no duplication");
    }

    /// Objective: Verify DuplicationDetector returns 0 for short files (<10 lines).
    #[test]
    fn test_detector_duplication_short_file() {
        let file = parse_rust("fn main() { let x = 1; }");
        let detector = DuplicationDetector::new();
        assert_eq!(detector.count_violations(&file), 0, "short file = 0");
    }

    // ── SignalDetector — LegacyCodeDetector ─────────────────────

    /// Objective: Verify LegacyCodeDetector finds consecutive commented-out code lines.
    #[test]
    fn test_detector_legacy_code_block() {
        let file = parse_rust(
            r#"
fn main() {
    // let x = 1;
    // let y = 2;
    // let z = 3;
    // let w = x + y;
    // foo(w);
    bar();
}
"#,
        );
        let detector = LegacyCodeDetector::new();
        assert!(
            detector.count_violations(&file) >= 5,
            "5 consecutive commented-out lines should be >= 5"
        );
    }

    /// Objective: Verify LegacyCodeDetector ignores doc comments.
    #[test]
    fn test_detector_legacy_code_doc_ok() {
        let file = parse_rust(
            r#"
/// Documented function
fn documented() -> i32 {
    // normal comment
    42
}
"#,
        );
        let detector = LegacyCodeDetector::new();
        assert_eq!(
            detector.count_violations(&file),
            0,
            "doc comment + 1 normal = 0"
        );
    }

    /// Objective: Verify LegacyCodeDetector returns 0 for short comments.
    #[test]
    fn test_detector_legacy_code_short_ok() {
        let file = parse_rust(
            r#"
// short
// comments
// are fine
fn main() {}
"#,
        );
        let detector = LegacyCodeDetector::new();
        assert_eq!(detector.count_violations(&file), 0, "short comments = 0");
    }

    /// Objective: Verify LegacyCodeDetector is 0 for empty code.
    #[test]
    fn test_detector_legacy_code_empty() {
        let file = parse_rust("// just a single comment");
        let detector = LegacyCodeDetector::new();
        assert_eq!(detector.count_violations(&file), 0);
    }

    /// Objective: Verify LegacyCodeDetector catches 4+ consecutive commented lines.
    #[test]
    fn test_detector_legacy_code_four_lines() {
        let file = parse_rust(
            r#"
// fn old() {
//     do_thing();
//     let x = 1;
//     x
// }
fn new() {}
"#,
        );
        let detector = LegacyCodeDetector::new();
        assert!(
            detector.count_violations(&file) >= 4,
            "4 consecutive commented lines"
        );
    }

    // ── SignalDetector — TodoMountainDetector ────────────────────

    /// Objective: Verify TodoMountainDetector counts TODO markers.
    #[test]
    fn test_detector_todo_basic() {
        let file = parse_rust(
            r#"
// TODO: refactor
// FIXME: fix this
fn main() {}
"#,
        );
        let detector = TodoMountainDetector::new();
        assert_eq!(detector.count_violations(&file), 2);
    }

    /// Objective: Verify TodoMountainDetector counts BUG and HACK too.
    #[test]
    fn test_detector_todo_bug_hack() {
        let file = parse_rust(
            r#"
// BUG: critical issue here
// HACK: workaround
fn main() {}
"#,
        );
        let detector = TodoMountainDetector::new();
        assert_eq!(detector.count_violations(&file), 2);
    }

    /// Objective: Verify TodoMountainDetector returns 0 for clean code.
    #[test]
    fn test_detector_todo_clean() {
        let file = parse_rust(
            r#"
fn main() {
    let x = 1;
}
"#,
        );
        let detector = TodoMountainDetector::new();
        assert_eq!(detector.count_violations(&file), 0);
    }

    /// Objective: Verify TodoMountainDetector is case-insensitive.
    #[test]
    fn test_detector_todo_case_insensitive() {
        let file = parse_rust(
            r#"
// todo: lowercase
// fixme: lowercase
fn main() {}
"#,
        );
        let detector = TodoMountainDetector::new();
        assert!(
            detector.count_violations(&file) >= 2,
            "case-insensitive TODO + FIXME"
        );
    }

    /// Objective: Verify TodoMountainDetector counts inline markers.
    #[test]
    fn test_detector_todo_inline() {
        let file = parse_rust(
            r#"
fn main() {
    let x = 1; // TODO: use constant
    let y = 2; // FIXME: off by one
}
"#,
        );
        let detector = TodoMountainDetector::new();
        assert_eq!(detector.count_violations(&file), 2);
    }

    // ── SignalDetector — LineCountSmellDetector ──────────────────

    fn create_rust_file(lines: usize) -> String {
        let mut s = String::from("fn main() {\n");
        for i in 0..lines.saturating_sub(2) {
            s.push_str(&format!("    let x_{} = {};\n", i, i));
        }
        s.push_str("}\n");
        s
    }

    /// Objective: Verify LineCountSmellDetector signals for large files.
    #[test]
    fn test_detector_linecount_over_threshold() {
        let code = create_rust_file(1100);
        let engine = TreeSitterEngine::new();
        let file = engine
            .parse_file(std::path::Path::new("lib.rs"), &code)
            .expect("parse should work");
        let detector = LineCountSmellDetector::new();
        assert!(
            detector.count_violations(&file) > 0,
            "1100-line file should trigger smell"
        );
    }

    /// Objective: Verify LineCountSmellDetector does NOT signal small files.
    #[test]
    fn test_detector_linecount_under_threshold() {
        let code = create_rust_file(100);
        let file = parse_rust(&code);
        let detector = LineCountSmellDetector::new();
        assert_eq!(detector.count_violations(&file), 0, "100-line file = 0");
    }

    /// Objective: Verify LineCountSmellDetector uses higher threshold for test files.
    #[test]
    fn test_detector_linecount_test_threshold() {
        use std::path::Path;
        let code = create_rust_file(1100);
        let engine = TreeSitterEngine::new();
        let file = engine
            .parse_file(Path::new("test_suite.rs"), &code)
            .expect("parse should work");
        let detector = LineCountSmellDetector::new();
        assert_eq!(
            detector.count_violations(&file),
            0,
            "1100-line test file = 0 (test threshold = 2000)"
        );
    }

    /// Objective: Verify LineCountSmellDetector crosses test threshold.
    #[test]
    fn test_detector_linecount_test_over_threshold() {
        use std::path::Path;
        let code = create_rust_file(2100);
        let engine = TreeSitterEngine::new();
        let file = engine
            .parse_file(Path::new("test_suite.rs"), &code)
            .expect("parse should work");
        let detector = LineCountSmellDetector::new();
        assert!(
            detector.count_violations(&file) > 0,
            "2100-line test file should trigger smell"
        );
    }

    // ── SignalDetector — NamingChaosDetector additional ──────────

    /// Objective: Verify NamingChaosDetector catches Hungarian notation.
    #[test]
    fn test_detector_naming_hungarian() {
        let file = parse_rust(
            r#"
fn main() {
    let strName = String::new();
    let intCount = 42;
}
"#,
        );
        let detector = NamingChaosDetector::new();
        assert!(
            detector.count_violations(&file) >= 2,
            "Hungarian notation vars"
        );
    }

    /// Objective: Verify NamingChaosDetector catches non-idiomatic single-letter.
    #[test]
    fn test_detector_naming_non_idiomatic_single() {
        let file = parse_rust(
            r#"
fn main() {
    let z = 1;
    let q = 2;
}
"#,
        );
        let detector = NamingChaosDetector::new();
        assert_eq!(detector.count_violations(&file), 2, "z + q = 2");
    }

    /// Objective: Verify NamingChaosDetector exempts idiomatic single-letter vars.
    #[test]
    fn test_detector_naming_idiomatic_single_ok() {
        let file = parse_rust(
            r#"
fn main() {
    let i = 0;
    let j = 1;
    let n = 100;
}
"#,
        );
        let detector = NamingChaosDetector::new();
        assert_eq!(detector.count_violations(&file), 0, "i, j, n are idiomatic");
    }

    // ── SignalDetector — PanicAddictionDetector additional ───────

    /// Objective: Verify PanicAddictionDetector returns 0 for safe code.
    #[test]
    fn test_detector_panic_clean() {
        let file = parse_rust(
            r#"
fn safe() -> Result<i32, String> {
    Ok(42)
}
"#,
        );
        let detector = PanicAddictionDetector::new();
        assert_eq!(detector.count_violations(&file), 0, "safe code = 0");
    }

    /// Objective: Verify PanicAddictionDetector finds .unwrap() in closures.
    #[test]
    fn test_detector_panic_unwrap_in_closure() {
        let file = parse_rust(
            r#"
fn main() {
    let result = (0..10).filter(|x| x.unwrap() > 0);
}
"#,
        );
        let detector = PanicAddictionDetector::new();
        assert_eq!(detector.count_violations(&file), 1, "unwrap in closure");
    }

    // ── SignalDetector — OverEngineeringDetector additional ──────

    /// Objective: Verify OverEngineeringDetector counts god function with >50 lines.
    #[test]
    fn test_detector_overengineering_god_function_55_lines() {
        let mut code = String::from("fn god() {\n");
        for i in 0..53 {
            code.push_str(&format!("    let var_{} = {};\n", i, i));
        }
        code.push_str("}\n");
        let file = parse_rust(&code);
        let detector = OverEngineeringDetector::new();
        assert!(
            detector.count_violations(&file) >= 1,
            "55-line god function should count"
        );
    }

    // ── SignalDetector — CodeSmellsDetector additional ───────────

    /// Objective: Verify CodeSmellsDetector handles empty functions.
    #[test]
    fn test_detector_code_smells_empty_fn() {
        let file = parse_rust("fn empty() {}");
        let detector = CodeSmellsDetector::new();
        assert_eq!(detector.count_violations(&file), 0);
    }

    /// Objective: Verify CodeSmellsDetector counts multiply-imported items.
    #[test]
    fn test_detector_code_smells_duplicate_import() {
        let file = parse_rust(
            r#"
use std::collections::HashMap;
use std::collections::HashMap;
fn main() {}
"#,
        );
        let detector = CodeSmellsDetector::new();
        assert!(
            detector.count_violations(&file) >= 1,
            "duplicate import should be > 0"
        );
    }

    /// Objective: Verify signal() returns the correct StyleSignal variant.
    #[test]
    fn test_detector_panic_signal_type() {
        assert_eq!(
            PanicAddictionDetector::new().signal(),
            StyleSignal::PanicAddiction
        );
    }

    #[test]
    fn test_detector_naming_signal_type() {
        assert_eq!(
            NamingChaosDetector::new().signal(),
            StyleSignal::NamingChaos
        );
    }

    #[test]
    fn test_detector_nested_signal_type() {
        assert_eq!(NestedHellDetector::new().signal(), StyleSignal::NestedHell);
    }

    #[test]
    fn test_detector_hotfix_signal_type() {
        assert_eq!(
            HotfixCultureDetector::new().signal(),
            StyleSignal::HotfixCulture
        );
    }

    #[test]
    fn test_detector_overeng_signal_type() {
        assert_eq!(
            OverEngineeringDetector::new().signal(),
            StyleSignal::OverEngineering
        );
    }

    #[test]
    fn test_detector_code_smells_signal_type() {
        assert_eq!(CodeSmellsDetector::new().signal(), StyleSignal::CodeSmells);
    }

    #[test]
    fn test_detector_legacy_signal_type() {
        assert_eq!(LegacyCodeDetector::new().signal(), StyleSignal::LegacyCode);
    }

    #[test]
    fn test_detector_todo_signal_type() {
        assert_eq!(
            TodoMountainDetector::new().signal(),
            StyleSignal::TodoMountain
        );
    }

    #[test]
    fn test_detector_linecount_signal_type() {
        assert_eq!(
            LineCountSmellDetector::new().signal(),
            StyleSignal::LineCountSmell
        );
    }

    #[test]
    fn test_detector_duplication_signal_type() {
        assert_eq!(
            DuplicationDetector::new().signal(),
            StyleSignal::Duplication
        );
    }
}