rustqual 0.5.5

Comprehensive Rust code quality analyzer — six dimensions: Complexity, Coupling, DRY, IOSP, SRP, Test Quality
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
use crate::analyzer::{Classification, FunctionAnalysis};
use crate::report::AnalysisResult;

/// A single finding with its location, category, and detail.
#[derive(Debug, Clone)]
pub struct FindingEntry {
    pub file: String,
    pub line: usize,
    pub category: &'static str,
    pub detail: String,
    pub function_name: String,
}

impl FindingEntry {
    fn new(
        file: &str,
        line: usize,
        category: &'static str,
        detail: String,
        context: String,
    ) -> Self {
        Self {
            file: file.to_string(),
            line,
            category,
            detail,
            function_name: context,
        }
    }
}

/// Collect all findings from an analysis result into a flat, sorted list.
/// Integration: orchestrates per-dimension collectors via closures.
pub fn collect_all_findings(analysis: &AnalysisResult) -> Vec<FindingEntry> {
    let mut entries = Vec::new();
    collect_function_findings(&analysis.results, &mut entries);
    collect_dry_findings(analysis, &mut entries);
    collect_srp_findings(analysis, &mut entries);
    collect_coupling_findings(analysis, &mut entries);
    collect_tq_findings(analysis, &mut entries);
    collect_structural_findings(analysis, &mut entries);
    entries.sort_by(|a, b| a.file.cmp(&b.file).then(a.line.cmp(&b.line)));
    entries
}

/// Print findings in one-line-per-finding format with heading.
/// Operation: formatting logic, no own calls.
pub fn print_findings(entries: &[FindingEntry]) {
    if entries.is_empty() {
        return;
    }
    let n = entries.len();
    let heading = format!("═══ {} Finding{} ═══", n, if n == 1 { "" } else { "s" });
    println!("\n{}", colored::Colorize::bold(heading.as_str()));
    entries.iter().for_each(|e| {
        let detail = if e.function_name.is_empty() {
            e.detail.clone()
        } else if e.detail.is_empty() {
            format!("in {}", e.function_name)
        } else {
            format!("{}  in {}", e.detail, e.function_name)
        };
        if e.file.is_empty() {
            println!("  {}  {}", e.category, detail);
        } else {
            println!("  {}:{}  {}  {}", e.file, e.line, e.category, detail);
        }
    });
}

// ── Per-dimension collectors (Operations) ───────────────────────

/// Collect IOSP violation and complexity findings from function results.
/// Operation: iterates function results checking flags; no own calls.
fn collect_function_findings(results: &[FunctionAnalysis], entries: &mut Vec<FindingEntry>) {
    results.iter().filter(|f| !f.suppressed).for_each(|f| {
        let e = |cat, detail: String| {
            FindingEntry::new(&f.file, f.line, cat, detail, f.qualified_name.clone())
        };
        if matches!(f.classification, Classification::Violation { .. }) {
            entries.push(e("VIOLATION", "logic + calls".to_string()));
        }
        if f.cognitive_warning {
            let cx = f.complexity.as_ref().map_or(0, |m| m.cognitive_complexity);
            entries.push(e("COGNITIVE", format!("complexity {cx}")));
        }
        if f.cyclomatic_warning {
            let cx = f.complexity.as_ref().map_or(0, |m| m.cyclomatic_complexity);
            entries.push(e("CYCLOMATIC", format!("complexity {cx}")));
        }
        if let Some(ref m) = f.complexity {
            m.magic_numbers.iter().for_each(|mn| {
                entries.push(FindingEntry::new(
                    &f.file,
                    mn.line,
                    "MAGIC_NUMBER",
                    mn.value.clone(),
                    f.qualified_name.clone(),
                ));
            });
        }
        if f.nesting_depth_warning {
            let depth = f.complexity.as_ref().map_or(0, |m| m.max_nesting);
            entries.push(e("NESTING", format!("depth {depth}")));
        }
        if f.function_length_warning {
            let lines = f.complexity.as_ref().map_or(0, |m| m.function_lines);
            entries.push(e("LONG_FN", format!("{lines} lines")));
        }
        if f.unsafe_warning {
            let blocks = f.complexity.as_ref().map_or(0, |m| m.unsafe_blocks);
            entries.push(e("UNSAFE", format!("{blocks} blocks")));
        }
        if f.error_handling_warning {
            entries.push(e("ERROR_HANDLING", "unwrap/panic/todo".to_string()));
        }
    });
}

/// Collect DRY findings (duplicates, dead code, fragments, boilerplate, wildcards).
/// Operation: iterates DRY analysis results; no own calls.
fn collect_dry_findings(analysis: &AnalysisResult, entries: &mut Vec<FindingEntry>) {
    analysis
        .duplicates
        .iter()
        .filter(|g| !g.suppressed)
        .for_each(|group| {
            let kind = match &group.kind {
                crate::dry::DuplicateKind::Exact => "exact".to_string(),
                crate::dry::DuplicateKind::NearDuplicate { similarity } => {
                    format!("{:.0}% similar", similarity * 100.0)
                }
            };
            group.entries.iter().for_each(|e| {
                entries.push(FindingEntry::new(
                    &e.file,
                    e.line,
                    "DUPLICATE",
                    kind.clone(),
                    e.qualified_name.clone(),
                ));
            });
        });
    analysis.dead_code.iter().for_each(|w| {
        let detail = format!("{:?}", w.kind).to_lowercase();
        entries.push(FindingEntry::new(
            &w.file,
            w.line,
            "DEAD_CODE",
            detail,
            w.qualified_name.clone(),
        ));
    });
    analysis
        .fragments
        .iter()
        .filter(|g| !g.suppressed)
        .for_each(|group| {
            group.entries.iter().for_each(|e| {
                let detail = format!("{} stmts", group.statement_count);
                entries.push(FindingEntry::new(
                    &e.file,
                    e.start_line,
                    "FRAGMENT",
                    detail,
                    e.function_name.clone(),
                ));
            });
        });
    analysis
        .boilerplate
        .iter()
        .filter(|b| !b.suppressed)
        .for_each(|b| {
            let name = b.struct_name.clone().unwrap_or_default();
            entries.push(FindingEntry::new(
                &b.file,
                b.line,
                "BOILERPLATE",
                b.pattern_id.clone(),
                name,
            ));
        });
    analysis
        .wildcard_warnings
        .iter()
        .filter(|w| !w.suppressed)
        .for_each(|w| {
            entries.push(FindingEntry::new(
                &w.file,
                w.line,
                "WILDCARD",
                w.module_path.clone(),
                String::new(),
            ));
        });
    analysis
        .repeated_matches
        .iter()
        .filter(|g| !g.suppressed)
        .for_each(|group| {
            group.entries.iter().for_each(|e| {
                entries.push(FindingEntry::new(
                    &e.file,
                    e.line,
                    "REPEATED_MATCH",
                    group.enum_name.clone(),
                    e.function_name.clone(),
                ));
            });
        });
}

/// Collect SRP findings.
/// Operation: iterates SRP warnings; no own calls.
fn collect_srp_findings(analysis: &AnalysisResult, entries: &mut Vec<FindingEntry>) {
    let Some(srp) = &analysis.srp else { return };
    srp.struct_warnings
        .iter()
        .filter(|w| !w.suppressed)
        .for_each(|w| {
            entries.push(FindingEntry::new(
                &w.file,
                w.line,
                "SRP_STRUCT",
                format!("LCOM4={}", w.lcom4),
                w.struct_name.clone(),
            ));
        });
    srp.module_warnings
        .iter()
        .filter(|w| !w.suppressed)
        .for_each(|w| {
            entries.push(FindingEntry::new(
                &w.file,
                1,
                "SRP_MODULE",
                format!("{} lines", w.production_lines),
                w.module.clone(),
            ));
        });
    srp.param_warnings
        .iter()
        .filter(|w| !w.suppressed)
        .for_each(|w| {
            entries.push(FindingEntry::new(
                &w.file,
                w.line,
                "SRP_PARAMS",
                format!("{} params", w.parameter_count),
                w.function_name.clone(),
            ));
        });
}

/// Collect coupling findings (threshold warnings, cycles, SDP violations).
/// Operation: iterates coupling analysis; no own calls.
fn collect_coupling_findings(analysis: &AnalysisResult, entries: &mut Vec<FindingEntry>) {
    let Some(ca) = &analysis.coupling else { return };
    ca.metrics.iter().filter(|m| m.warning).for_each(|m| {
        let detail = format!("I={:.2} Ca={} Ce={}", m.instability, m.afferent, m.efferent);
        entries.push(FindingEntry::new(
            "",
            0,
            "COUPLING",
            detail,
            m.module_name.clone(),
        ));
    });
    ca.cycles.iter().for_each(|c| {
        let detail = c.modules.join(" > ");
        entries.push(FindingEntry::new("", 0, "CYCLE", detail, String::new()));
    });
    ca.sdp_violations
        .iter()
        .filter(|v| !v.suppressed)
        .for_each(|v| {
            let detail = format!("{} -> {}", v.from_module, v.to_module);
            entries.push(FindingEntry::new(
                "",
                0,
                "SDP",
                detail,
                v.from_module.clone(),
            ));
        });
}

/// Collect TQ findings.
/// Operation: iterates TQ warnings; no own calls.
fn collect_tq_findings(analysis: &AnalysisResult, entries: &mut Vec<FindingEntry>) {
    let Some(tq) = &analysis.tq else { return };
    tq.warnings.iter().filter(|w| !w.suppressed).for_each(|w| {
        let cat = match &w.kind {
            crate::tq::TqWarningKind::NoAssertion => "TQ_NO_ASSERT",
            crate::tq::TqWarningKind::NoSut => "TQ_NO_SUT",
            crate::tq::TqWarningKind::Untested => "TQ_UNTESTED",
            crate::tq::TqWarningKind::Uncovered => "TQ_UNCOVERED",
            crate::tq::TqWarningKind::UntestedLogic { .. } => "TQ_UNTESTED_LOGIC",
        };
        entries.push(FindingEntry::new(
            &w.file,
            w.line,
            cat,
            String::new(),
            w.function_name.clone(),
        ));
    });
}

/// Collect structural findings.
/// Operation: iterates structural warnings; no own calls.
fn collect_structural_findings(analysis: &AnalysisResult, entries: &mut Vec<FindingEntry>) {
    let Some(st) = &analysis.structural else {
        return;
    };
    st.warnings.iter().filter(|w| !w.suppressed).for_each(|w| {
        entries.push(FindingEntry::new(
            &w.file,
            w.line,
            "STRUCTURAL",
            w.kind.code().to_string(),
            w.name.clone(),
        ));
    });
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::analyzer::{
        Classification, ComplexityMetrics, FunctionAnalysis, MagicNumberOccurrence,
    };
    use crate::report::{AnalysisResult, Summary};

    fn make_fa(name: &str, file: &str, line: usize) -> FunctionAnalysis {
        FunctionAnalysis {
            name: name.to_string(),
            file: file.to_string(),
            line,
            classification: Classification::Operation,
            parent_type: None,
            suppressed: false,
            complexity: None,
            qualified_name: name.to_string(),
            severity: None,
            cognitive_warning: false,
            cyclomatic_warning: false,
            nesting_depth_warning: false,
            function_length_warning: false,
            unsafe_warning: false,
            error_handling_warning: false,
            complexity_suppressed: false,
            own_calls: vec![],
            parameter_count: 0,
            is_trait_impl: false,
            is_test: false,
            effort_score: None,
        }
    }

    fn empty_analysis() -> AnalysisResult {
        AnalysisResult {
            results: vec![],
            summary: Summary::default(),
            coupling: None,
            duplicates: vec![],
            dead_code: vec![],
            fragments: vec![],
            boilerplate: vec![],
            wildcard_warnings: vec![],
            repeated_matches: vec![],
            srp: None,
            tq: None,
            structural: None,
        }
    }

    #[test]
    fn test_collect_empty_analysis() {
        let analysis = empty_analysis();
        let findings = collect_all_findings(&analysis);
        assert!(findings.is_empty());
    }

    #[test]
    fn test_collect_magic_numbers() {
        let mut analysis = empty_analysis();
        let mut fa = make_fa("test_fn", "src/lib.rs", 10);
        fa.complexity = Some(ComplexityMetrics {
            magic_numbers: vec![
                MagicNumberOccurrence {
                    line: 12,
                    value: "42".to_string(),
                },
                MagicNumberOccurrence {
                    line: 15,
                    value: "99".to_string(),
                },
            ],
            ..Default::default()
        });
        analysis.results = vec![fa];
        let findings = collect_all_findings(&analysis);
        assert_eq!(findings.len(), 2);
        assert_eq!(findings[0].category, "MAGIC_NUMBER");
        assert_eq!(findings[0].detail, "42");
        assert_eq!(findings[1].detail, "99");
    }

    #[test]
    fn test_collect_violation() {
        let mut analysis = empty_analysis();
        let mut fa = make_fa("bad_fn", "src/lib.rs", 5);
        fa.classification = Classification::Violation {
            has_logic: true,
            has_own_calls: true,
            logic_locations: vec![],
            call_locations: vec![],
        };
        analysis.results = vec![fa];
        let findings = collect_all_findings(&analysis);
        assert_eq!(findings.len(), 1);
        assert_eq!(findings[0].category, "VIOLATION");
    }

    #[test]
    fn test_sorted_by_file_and_line() {
        let mut analysis = empty_analysis();
        let mut fa1 = make_fa("fn_b", "src/b.rs", 20);
        fa1.error_handling_warning = true;
        fa1.complexity = Some(ComplexityMetrics::default());
        let mut fa2 = make_fa("fn_a", "src/a.rs", 10);
        fa2.error_handling_warning = true;
        fa2.complexity = Some(ComplexityMetrics::default());
        analysis.results = vec![fa1, fa2];
        let findings = collect_all_findings(&analysis);
        assert_eq!(findings[0].file, "src/a.rs");
        assert_eq!(findings[1].file, "src/b.rs");
    }

    #[test]
    fn test_suppressed_not_collected() {
        let mut analysis = empty_analysis();
        let mut fa = make_fa("suppressed_fn", "src/lib.rs", 5);
        fa.suppressed = true;
        fa.classification = Classification::Violation {
            has_logic: true,
            has_own_calls: true,
            logic_locations: vec![],
            call_locations: vec![],
        };
        analysis.results = vec![fa];
        let findings = collect_all_findings(&analysis);
        assert!(findings.is_empty());
    }

    // ── Contract tests: when summary counts match per-entry semantics,
    // total_findings() must equal collect_all_findings().len().
    // Pipeline integration is tested by test_self_analysis_no_violations. ──

    #[test]
    fn test_total_findings_consistent_magic_numbers() {
        let mut analysis = empty_analysis();
        let mut fa = make_fa("fn1", "src/lib.rs", 10);
        fa.complexity = Some(ComplexityMetrics {
            magic_numbers: vec![
                MagicNumberOccurrence {
                    line: 12,
                    value: "42".to_string(),
                },
                MagicNumberOccurrence {
                    line: 15,
                    value: "99".to_string(),
                },
            ],
            ..Default::default()
        });
        analysis.results = vec![fa];
        // Pipeline must count per-occurrence, not per-function
        analysis.summary.magic_number_warnings = 2;
        let findings = collect_all_findings(&analysis);
        assert_eq!(
            analysis.summary.total_findings(),
            findings.len(),
            "total_findings() must equal collect_all_findings().len()"
        );
    }

    #[test]
    fn test_total_findings_consistent_duplicates() {
        use crate::dry::functions::{DuplicateEntry, DuplicateGroup, DuplicateKind};
        let mut analysis = empty_analysis();
        analysis.duplicates = vec![DuplicateGroup {
            entries: vec![
                DuplicateEntry {
                    name: "fn_a".to_string(),
                    qualified_name: "mod::fn_a".to_string(),
                    file: "src/a.rs".to_string(),
                    line: 10,
                },
                DuplicateEntry {
                    name: "fn_b".to_string(),
                    qualified_name: "mod::fn_b".to_string(),
                    file: "src/b.rs".to_string(),
                    line: 20,
                },
            ],
            kind: DuplicateKind::Exact,
            suppressed: false,
        }];
        // Pipeline must count per-entry (2), not per-group (1)
        analysis.summary.duplicate_groups = 2;
        let findings = collect_all_findings(&analysis);
        assert_eq!(
            analysis.summary.total_findings(),
            findings.len(),
            "total_findings() must equal collect_all_findings().len()"
        );
    }

    #[test]
    fn test_total_findings_consistent_fragments() {
        use crate::dry::fragments::{FragmentEntry, FragmentGroup};
        let mut analysis = empty_analysis();
        analysis.fragments = vec![FragmentGroup {
            entries: vec![
                FragmentEntry {
                    function_name: "fn_a".to_string(),
                    qualified_name: "mod::fn_a".to_string(),
                    file: "src/a.rs".to_string(),
                    start_line: 10,
                    end_line: 15,
                },
                FragmentEntry {
                    function_name: "fn_b".to_string(),
                    qualified_name: "mod::fn_b".to_string(),
                    file: "src/b.rs".to_string(),
                    start_line: 20,
                    end_line: 25,
                },
                FragmentEntry {
                    function_name: "fn_c".to_string(),
                    qualified_name: "mod::fn_c".to_string(),
                    file: "src/c.rs".to_string(),
                    start_line: 30,
                    end_line: 35,
                },
            ],
            statement_count: 3,
            suppressed: false,
        }];
        // Pipeline must count per-entry (3), not per-group (1)
        analysis.summary.fragment_groups = 3;
        let findings = collect_all_findings(&analysis);
        assert_eq!(
            analysis.summary.total_findings(),
            findings.len(),
            "total_findings() must equal collect_all_findings().len()"
        );
    }

    #[test]
    fn test_total_findings_consistent_mixed() {
        use crate::dry::functions::{DuplicateEntry, DuplicateGroup, DuplicateKind};
        let mut analysis = empty_analysis();
        // 1 function with 2 magic numbers
        let mut fa = make_fa("fn1", "src/lib.rs", 10);
        fa.complexity = Some(ComplexityMetrics {
            magic_numbers: vec![
                MagicNumberOccurrence {
                    line: 12,
                    value: "400".to_string(),
                },
                MagicNumberOccurrence {
                    line: 13,
                    value: "800".to_string(),
                },
            ],
            ..Default::default()
        });
        analysis.results = vec![fa];
        // 1 duplicate group with 2 entries
        analysis.duplicates = vec![DuplicateGroup {
            entries: vec![
                DuplicateEntry {
                    name: "fn_a".to_string(),
                    qualified_name: "mod::fn_a".to_string(),
                    file: "src/a.rs".to_string(),
                    line: 100,
                },
                DuplicateEntry {
                    name: "fn_b".to_string(),
                    qualified_name: "mod::fn_b".to_string(),
                    file: "src/b.rs".to_string(),
                    line: 200,
                },
            ],
            kind: DuplicateKind::Exact,
            suppressed: false,
        }];
        analysis.summary.magic_number_warnings = 2;
        analysis.summary.duplicate_groups = 2;
        let findings = collect_all_findings(&analysis);
        // 2 magic numbers + 2 duplicate entries = 4 findings
        assert_eq!(findings.len(), 4);
        assert_eq!(
            analysis.summary.total_findings(),
            findings.len(),
            "total_findings() must equal collect_all_findings().len() — was the bug from issue report"
        );
    }

    #[test]
    fn test_total_findings_consistent_coupling() {
        let mut analysis = empty_analysis();
        analysis.coupling = Some(crate::coupling::CouplingAnalysis {
            metrics: vec![crate::coupling::CouplingMetrics {
                module_name: "db".to_string(),
                afferent: 2,
                efferent: 5,
                instability: 0.71,
                incoming: vec![],
                outgoing: vec![],
                suppressed: false,
                warning: true,
            }],
            cycles: vec![crate::coupling::CycleReport {
                modules: vec!["a".to_string(), "b".to_string()],
            }],
            sdp_violations: vec![],
        });
        // 1 coupling warning + 1 cycle = 2
        analysis.summary.coupling_warnings = 1;
        analysis.summary.coupling_cycles = 1;
        let findings = collect_all_findings(&analysis);
        assert_eq!(
            analysis.summary.total_findings(),
            findings.len(),
            "coupling warnings and cycles must appear in findings list"
        );
        assert!(
            findings.iter().any(|f| f.category == "COUPLING"
                && f.function_name == "db"
                && f.detail.contains("I=0.71")),
            "expected a COUPLING finding for db with instability detail"
        );
        assert!(
            findings
                .iter()
                .any(|f| f.category == "CYCLE" && f.detail.contains("a > b")),
            "expected a CYCLE finding describing the a > b cycle"
        );
    }
}