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
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
use std::collections::HashMap;

use syn::spanned::Spanned;
use syn::visit::Visit;

use crate::config::sections::DuplicatesConfig;

/// Maximum entries per hash group before skipping pairwise comparison.
const MAX_WINDOW_GROUP_SIZE: usize = 50;

// ── Result types ────────────────────────────────────────────────

/// A group of matching code fragments across different functions.
#[derive(Debug, Clone)]
pub struct FragmentGroup {
    pub entries: Vec<FragmentEntry>,
    pub statement_count: usize,
    pub suppressed: bool,
}

/// An individual fragment location within a function.
#[derive(Debug, Clone)]
pub struct FragmentEntry {
    pub function_name: String,
    pub qualified_name: String,
    pub file: String,
    pub start_line: usize,
    pub end_line: usize,
}

// ── Internal types ──────────────────────────────────────────────

/// Metadata for a function whose body was scanned for fragments.
struct FnInfo {
    name: String,
    qualified_name: String,
    file: String,
    /// (start_line, end_line) for each top-level statement in the body.
    stmt_lines: Vec<(usize, usize)>,
}

/// A hashed window of consecutive statements within a function.
struct WindowEntry {
    fn_idx: usize,
    stmt_start: usize,
    hash: u64,
}

/// A matched pair of windows in two different functions.
struct PairMatch {
    fn_a: usize,
    fn_b: usize,
    stmt_a: usize,
    stmt_b: usize,
}

// ── Detection API ───────────────────────────────────────────────

/// Detect duplicate code fragments across parsed files.
/// Integration: orchestrates window collection, pair matching, and fragment merging.
pub fn detect_fragments(
    parsed: &[(String, String, syn::File)],
    config: &DuplicatesConfig,
) -> Vec<FragmentGroup> {
    let (fn_infos, windows) = collect_all_windows(parsed, config);
    let pairs = extract_matching_pairs(&windows);
    merge_into_fragments(pairs, &fn_infos, config.min_statements)
}

// ── Window collection ───────────────────────────────────────────

/// Collect all statement windows from all functions in parsed files.
/// Trivial: creates visitor and delegates to visit_all_files.
fn collect_all_windows(
    parsed: &[(String, String, syn::File)],
    config: &DuplicatesConfig,
) -> (Vec<FnInfo>, Vec<WindowEntry>) {
    let mut collector = FragmentCollector {
        config,
        file: String::new(),
        fn_infos: Vec::new(),
        windows: Vec::new(),
        in_test: false,
        parent_type: None,
        is_trait_impl: false,
    };
    super::visit_all_files(parsed, &mut collector);
    (collector.fn_infos, collector.windows)
}

// ── Pair matching ───────────────────────────────────────────────

/// Group windows by hash and extract cross-function matching pairs.
/// Operation: hash grouping + pair extraction logic, no own calls.
fn extract_matching_pairs(windows: &[WindowEntry]) -> Vec<PairMatch> {
    let mut by_hash: HashMap<u64, Vec<usize>> = HashMap::new();
    for (i, w) in windows.iter().enumerate() {
        by_hash.entry(w.hash).or_default().push(i);
    }

    let mut pairs = Vec::new();
    for indices in by_hash.values() {
        if indices.len() < 2 || indices.len() > MAX_WINDOW_GROUP_SIZE {
            continue;
        }
        for i in 0..indices.len() {
            for j in (i + 1)..indices.len() {
                let wa = &windows[indices[i]];
                let wb = &windows[indices[j]];
                if wa.fn_idx != wb.fn_idx {
                    pairs.push(PairMatch {
                        fn_a: wa.fn_idx,
                        fn_b: wb.fn_idx,
                        stmt_a: wa.stmt_start,
                        stmt_b: wb.stmt_start,
                    });
                }
            }
        }
    }
    pairs
}

// ── Fragment merging ────────────────────────────────────────────

/// Merge adjacent pair matches into maximal fragment groups.
/// Operation: sorting + interval merging logic, no own calls.
// qual:allow(complexity) reason: "interval merging algorithm with nested loops"
fn merge_into_fragments(
    mut pairs: Vec<PairMatch>,
    fn_infos: &[FnInfo],
    window_size: usize,
) -> Vec<FragmentGroup> {
    if pairs.is_empty() {
        return vec![];
    }

    // Canonical ordering: smaller fn_idx first in each pair
    for p in &mut pairs {
        if p.fn_a > p.fn_b {
            std::mem::swap(&mut p.fn_a, &mut p.fn_b);
            std::mem::swap(&mut p.stmt_a, &mut p.stmt_b);
        }
    }
    pairs.sort_unstable_by_key(|p| (p.fn_a, p.fn_b, p.stmt_a, p.stmt_b));
    pairs.dedup_by_key(|p| (p.fn_a, p.fn_b, p.stmt_a, p.stmt_b));

    let mut result = Vec::new();
    let mut i = 0;
    while i < pairs.len() {
        let fa = pairs[i].fn_a;
        let fb = pairs[i].fn_b;

        // Find end of this function pair's matches
        let mut j = i;
        while j < pairs.len() && pairs[j].fn_a == fa && pairs[j].fn_b == fb {
            j += 1;
        }

        // Merge consecutive matches: stmt_a and stmt_b both increment by 1
        let pair_slice = &pairs[i..j];
        let mut k = 0;
        while k < pair_slice.len() {
            let mut end = k;
            while end + 1 < pair_slice.len()
                && pair_slice[end + 1].stmt_a == pair_slice[end].stmt_a + 1
                && pair_slice[end + 1].stmt_b == pair_slice[end].stmt_b + 1
            {
                end += 1;
            }

            let stmt_count = end - k + window_size;
            let start_a = pair_slice[k].stmt_a;
            let end_a = start_a + stmt_count - 1;
            let start_b = pair_slice[k].stmt_b;
            let end_b = start_b + stmt_count - 1;

            // Look up actual source line numbers from fn_infos
            let line_a_start = fn_infos[fa].stmt_lines.get(start_a).map_or(0, |l| l.0);
            let line_a_end = fn_infos[fa]
                .stmt_lines
                .get(end_a)
                .map_or(line_a_start, |l| l.1);
            let line_b_start = fn_infos[fb].stmt_lines.get(start_b).map_or(0, |l| l.0);
            let line_b_end = fn_infos[fb]
                .stmt_lines
                .get(end_b)
                .map_or(line_b_start, |l| l.1);

            result.push(FragmentGroup {
                entries: vec![
                    FragmentEntry {
                        function_name: fn_infos[fa].name.clone(),
                        qualified_name: fn_infos[fa].qualified_name.clone(),
                        file: fn_infos[fa].file.clone(),
                        start_line: line_a_start,
                        end_line: line_a_end,
                    },
                    FragmentEntry {
                        function_name: fn_infos[fb].name.clone(),
                        qualified_name: fn_infos[fb].qualified_name.clone(),
                        file: fn_infos[fb].file.clone(),
                        start_line: line_b_start,
                        end_line: line_b_end,
                    },
                ],
                statement_count: stmt_count,
                suppressed: false,
            });

            k = end + 1;
        }

        i = j;
    }
    result
}

// ── FragmentCollector (AST visitor) ─────────────────────────────

/// AST visitor that collects statement windows from all function bodies.
struct FragmentCollector<'a> {
    config: &'a DuplicatesConfig,
    file: String,
    fn_infos: Vec<FnInfo>,
    windows: Vec<WindowEntry>,
    in_test: bool,
    parent_type: Option<String>,
    is_trait_impl: bool,
}

impl super::FileVisitor for FragmentCollector<'_> {
    fn reset_for_file(&mut self, file_path: &str) {
        self.file = file_path.to_string();
        self.in_test = false;
        self.parent_type = None;
        self.is_trait_impl = false;
    }
}

impl FragmentCollector<'_> {
    /// Process a function body: record fn_info and extract statement windows.
    /// Operation: window extraction logic; normalize/hash calls hidden in closure.
    fn process_body(&mut self, name: &str, body: &syn::Block, is_test_fn: bool) {
        let is_test = self.in_test || is_test_fn;
        if self.config.ignore_tests && is_test {
            return;
        }
        if self.config.ignore_trait_impls && self.is_trait_impl {
            return;
        }

        let window_size = self.config.min_statements;
        if body.stmts.len() < window_size {
            return;
        }

        let stmt_lines: Vec<(usize, usize)> = body
            .stmts
            .iter()
            .map(|s| (s.span().start().line, s.span().end().line))
            .collect();

        let qualified_name = self
            .parent_type
            .as_ref()
            .map(|p| format!("{p}::{name}"))
            .unwrap_or_else(|| name.to_string());

        let fn_idx = self.fn_infos.len();
        self.fn_infos.push(FnInfo {
            name: name.to_string(),
            qualified_name,
            file: self.file.clone(),
            stmt_lines,
        });

        // Closure hides own calls to normalize_stmts/structural_hash (lenient mode)
        let compute_hash = |stmts: &[syn::Stmt]| {
            let tokens = crate::normalize::normalize_stmts(stmts);
            let hash = crate::normalize::structural_hash(&tokens);
            (tokens.len(), hash)
        };

        let min_tokens = self.config.min_tokens;
        for i in 0..=body.stmts.len() - window_size {
            let window_stmts = &body.stmts[i..i + window_size];
            let (token_count, hash) = compute_hash(window_stmts);
            if token_count >= min_tokens {
                self.windows.push(WindowEntry {
                    fn_idx,
                    stmt_start: i,
                    hash,
                });
            }
        }
    }
}

impl<'ast> Visit<'ast> for FragmentCollector<'_> {
    fn visit_item_fn(&mut self, node: &'ast syn::ItemFn) {
        let name = node.sig.ident.to_string();
        let is_test = super::has_test_attr(&node.attrs);
        self.process_body(&name, &node.block, is_test);
        syn::visit::visit_item_fn(self, node);
    }

    fn visit_item_impl(&mut self, node: &'ast syn::ItemImpl) {
        let prev_parent = self.parent_type.take();
        let prev_is_trait = self.is_trait_impl;

        self.is_trait_impl = node.trait_.is_some();
        if let syn::Type::Path(tp) = &*node.self_ty {
            if let Some(seg) = tp.path.segments.last() {
                self.parent_type = Some(seg.ident.to_string());
            }
        }

        syn::visit::visit_item_impl(self, node);

        self.parent_type = prev_parent;
        self.is_trait_impl = prev_is_trait;
    }

    fn visit_impl_item_fn(&mut self, node: &'ast syn::ImplItemFn) {
        let name = node.sig.ident.to_string();
        let is_test = super::has_test_attr(&node.attrs);
        self.process_body(&name, &node.block, is_test);
    }

    fn visit_item_mod(&mut self, node: &'ast syn::ItemMod) {
        let prev_in_test = self.in_test;
        if super::has_cfg_test(&node.attrs) {
            self.in_test = true;
        }
        syn::visit::visit_item_mod(self, node);
        self.in_test = prev_in_test;
    }
}

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

    fn parse(code: &str) -> Vec<(String, String, syn::File)> {
        let syntax = syn::parse_file(code).expect("parse failed");
        vec![("test.rs".to_string(), code.to_string(), syntax)]
    }

    fn parse_multi(files: &[(&str, &str)]) -> Vec<(String, String, syn::File)> {
        files
            .iter()
            .map(|(name, code)| {
                let syntax = syn::parse_file(code).expect("parse failed");
                (name.to_string(), code.to_string(), syntax)
            })
            .collect()
    }

    const TEST_MIN_TOKENS: usize = 3;
    const TEST_MIN_STATEMENTS: usize = 3;

    fn low_threshold_config() -> DuplicatesConfig {
        DuplicatesConfig {
            min_tokens: TEST_MIN_TOKENS,
            min_lines: 1,
            min_statements: TEST_MIN_STATEMENTS,
            ..DuplicatesConfig::default()
        }
    }

    #[test]
    fn test_detect_fragments_empty() {
        let parsed = parse("");
        let config = low_threshold_config();
        let groups = detect_fragments(&parsed, &config);
        assert!(groups.is_empty());
    }

    #[test]
    fn test_detect_fragments_no_match() {
        let parsed = parse_multi(&[
            (
                "a.rs",
                r#"
                fn foo() {
                    let x = 1;
                    let y = x + 2;
                    let z = y * x;
                }
            "#,
            ),
            (
                "b.rs",
                r#"
                fn bar() {
                    let a = "hello";
                    let b = a.len();
                    if b > 0 { return; }
                }
            "#,
            ),
        ]);
        let config = low_threshold_config();
        let groups = detect_fragments(&parsed, &config);
        assert!(groups.is_empty(), "Different structures should not match");
    }

    #[test]
    fn test_detect_fragments_matching_statements() {
        let parsed = parse_multi(&[
            (
                "a.rs",
                r#"
                fn foo() {
                    let x = 1;
                    let y = x + 2;
                    let z = y * x;
                }
            "#,
            ),
            (
                "b.rs",
                r#"
                fn bar() {
                    let a = 1;
                    let b = a + 2;
                    let c = b * a;
                }
            "#,
            ),
        ]);
        let config = low_threshold_config();
        let groups = detect_fragments(&parsed, &config);
        assert!(!groups.is_empty(), "Should detect matching fragment");
        assert_eq!(groups[0].entries.len(), 2);
    }

    #[test]
    fn test_detect_fragments_cross_file() {
        let parsed = parse_multi(&[
            (
                "module_a.rs",
                r#"
                fn process_a() {
                    let x = 1;
                    let y = x + 2;
                    let z = y * x;
                    let w = z + 1;
                }
            "#,
            ),
            (
                "module_b.rs",
                r#"
                fn process_b() {
                    let a = 1;
                    let b = a + 2;
                    let c = b * a;
                    let d = c + 1;
                }
            "#,
            ),
        ]);
        let config = low_threshold_config();
        let groups = detect_fragments(&parsed, &config);
        assert!(!groups.is_empty());
        if let Some(g) = groups.first() {
            let files: std::collections::HashSet<&str> =
                g.entries.iter().map(|e| e.file.as_str()).collect();
            assert!(
                files.len() >= 2,
                "Fragment entries should come from different files"
            );
        }
    }

    #[test]
    fn test_detect_fragments_same_function_excluded() {
        let parsed = parse(
            r#"
            fn foo() {
                let x = 1;
                let y = x + 2;
                let z = y * x;
                let a = 1;
                let b = a + 2;
                let c = b * a;
            }
        "#,
        );
        let config = low_threshold_config();
        let groups = detect_fragments(&parsed, &config);
        assert!(
            groups.is_empty(),
            "Same-function duplicates should be excluded"
        );
    }

    #[test]
    fn test_detect_fragments_merges_adjacent() {
        let parsed = parse_multi(&[
            (
                "a.rs",
                r#"
                fn foo() {
                    let x = 1;
                    let y = x + 2;
                    let z = y * x;
                    let w = z + 1;
                }
            "#,
            ),
            (
                "b.rs",
                r#"
                fn bar() {
                    let a = 1;
                    let b = a + 2;
                    let c = b * a;
                    let d = c + 1;
                }
            "#,
            ),
        ]);
        let config = low_threshold_config();
        let groups = detect_fragments(&parsed, &config);
        // Windows [0,1,2] and [1,2,3] both match → merge into 4-statement fragment
        if !groups.is_empty() {
            assert!(
                groups.iter().any(|g| g.statement_count >= 4),
                "Adjacent windows should merge: got counts {:?}",
                groups.iter().map(|g| g.statement_count).collect::<Vec<_>>()
            );
        }
    }

    #[test]
    fn test_detect_fragments_too_few_statements() {
        let parsed = parse_multi(&[
            ("a.rs", "fn foo() { let x = 1; let y = 2; }"),
            ("b.rs", "fn bar() { let a = 1; let b = 2; }"),
        ]);
        let mut config = low_threshold_config();
        config.min_statements = 3;
        let groups = detect_fragments(&parsed, &config);
        assert!(
            groups.is_empty(),
            "Functions with <min_statements should produce no fragments"
        );
    }

    #[test]
    fn test_detect_fragments_below_min_tokens() {
        let parsed = parse_multi(&[
            (
                "a.rs",
                r#"
                fn foo() {
                    let x = 1;
                    let y = 2;
                    let z = 3;
                }
            "#,
            ),
            (
                "b.rs",
                r#"
                fn bar() {
                    let a = 1;
                    let b = 2;
                    let c = 3;
                }
            "#,
            ),
        ]);
        let mut config = low_threshold_config();
        config.min_tokens = 100;
        let groups = detect_fragments(&parsed, &config);
        assert!(
            groups.is_empty(),
            "Windows below min_tokens should be excluded"
        );
    }

    #[test]
    fn test_detect_fragments_test_excluded() {
        let parsed = parse_multi(&[
            (
                "a.rs",
                r#"
                fn prod() {
                    let x = 1;
                    let y = x + 2;
                    let z = y * x;
                }
            "#,
            ),
            (
                "b.rs",
                r#"
                #[cfg(test)]
                mod tests {
                    fn test_helper() {
                        let a = 1;
                        let b = a + 2;
                        let c = b * a;
                    }
                }
            "#,
            ),
        ]);
        let mut config = low_threshold_config();
        config.ignore_tests = true;
        let groups = detect_fragments(&parsed, &config);
        assert!(
            groups.is_empty(),
            "Test functions should be excluded when ignore_tests=true"
        );
    }

    #[test]
    fn test_detect_fragments_test_included() {
        let parsed = parse_multi(&[
            (
                "a.rs",
                r#"
                fn prod() {
                    let x = 1;
                    let y = x + 2;
                    let z = y * x;
                }
            "#,
            ),
            (
                "b.rs",
                r#"
                #[cfg(test)]
                mod tests {
                    fn test_helper() {
                        let a = 1;
                        let b = a + 2;
                        let c = b * a;
                    }
                }
            "#,
            ),
        ]);
        let mut config = low_threshold_config();
        config.ignore_tests = false;
        let groups = detect_fragments(&parsed, &config);
        assert!(
            !groups.is_empty(),
            "Test functions should be included when ignore_tests=false"
        );
    }

    #[test]
    fn test_detect_fragments_entry_has_lines() {
        let parsed = parse_multi(&[
            (
                "a.rs",
                r#"
                fn foo() {
                    let x = 1;
                    let y = x + 2;
                    let z = y * x;
                }
            "#,
            ),
            (
                "b.rs",
                r#"
                fn bar() {
                    let a = 1;
                    let b = a + 2;
                    let c = b * a;
                }
            "#,
            ),
        ]);
        let config = low_threshold_config();
        let groups = detect_fragments(&parsed, &config);
        if !groups.is_empty() {
            for entry in &groups[0].entries {
                assert!(entry.start_line > 0, "start_line should be > 0");
                assert!(entry.end_line >= entry.start_line, "end_line >= start_line");
            }
        }
    }

    #[test]
    fn test_extract_matching_pairs_empty() {
        let pairs = extract_matching_pairs(&[]);
        assert!(pairs.is_empty());
    }

    #[test]
    fn test_merge_into_fragments_empty() {
        let fn_infos: Vec<FnInfo> = vec![];
        let groups = merge_into_fragments(vec![], &fn_infos, 3);
        assert!(groups.is_empty());
    }

    #[test]
    fn test_fragment_group_statement_count() {
        let parsed = parse_multi(&[
            (
                "a.rs",
                r#"
                fn foo() {
                    let x = 1;
                    let y = x + 2;
                    let z = y * x;
                }
            "#,
            ),
            (
                "b.rs",
                r#"
                fn bar() {
                    let a = 1;
                    let b = a + 2;
                    let c = b * a;
                }
            "#,
            ),
        ]);
        let config = low_threshold_config();
        let groups = detect_fragments(&parsed, &config);
        for group in &groups {
            assert!(
                group.statement_count >= 3,
                "Fragment must have at least min_statements"
            );
        }
    }

    #[test]
    fn test_detect_fragments_impl_method() {
        let parsed = parse_multi(&[
            (
                "a.rs",
                r#"
                struct Foo;
                impl Foo {
                    fn method(&self) {
                        let x = 1;
                        let y = x + 2;
                        let z = y * x;
                    }
                }
            "#,
            ),
            (
                "b.rs",
                r#"
                struct Bar;
                impl Bar {
                    fn method(&self) {
                        let a = 1;
                        let b = a + 2;
                        let c = b * a;
                    }
                }
            "#,
            ),
        ]);
        let config = low_threshold_config();
        let groups = detect_fragments(&parsed, &config);
        assert!(
            !groups.is_empty(),
            "Should detect fragments in impl methods"
        );
    }

    #[test]
    fn test_detect_fragments_three_way() {
        let parsed = parse_multi(&[
            (
                "a.rs",
                r#"
                fn func_a() {
                    let x = 1;
                    let y = x + 2;
                    let z = y * x;
                }
            "#,
            ),
            (
                "b.rs",
                r#"
                fn func_b() {
                    let a = 1;
                    let b = a + 2;
                    let c = b * a;
                }
            "#,
            ),
            (
                "c.rs",
                r#"
                fn func_c() {
                    let p = 1;
                    let q = p + 2;
                    let r = q * p;
                }
            "#,
            ),
        ]);
        let config = low_threshold_config();
        let groups = detect_fragments(&parsed, &config);
        // With 3 matching functions, we get pairs: (a,b), (a,c), (b,c)
        assert!(
            groups.len() >= 3,
            "Three matching functions should produce at least 3 pair groups"
        );
    }
}