rustqual 1.2.0

Comprehensive Rust code quality analyzer — seven dimensions: IOSP, Complexity, DRY, SRP, Coupling, Test Quality, Architecture
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
//! Tests for `collect_pub_fns_by_layer` — workspace-wide pub-fn
//! enumeration grouped by architecture layer.

use crate::adapters::analyzers::architecture::call_parity_rule::pub_fns::{
    collect_pub_fns_by_layer, PubFnInfo,
};
use crate::adapters::analyzers::architecture::layer_rule::LayerDefinitions;
use crate::adapters::shared::use_tree::gather_alias_map;
use globset::{Glob, GlobSet, GlobSetBuilder};
use std::collections::{HashMap, HashSet};

/// Build an `aliases_per_file` map from a workspace slice — mirrors
/// what the call-parity entry point computes.
fn aliases_from_files(
    files: &[(&str, &syn::File)],
) -> HashMap<String, HashMap<String, Vec<String>>> {
    files
        .iter()
        .map(|(p, f)| (p.to_string(), gather_alias_map(f)))
        .collect()
}

fn parse(src: &str) -> syn::File {
    syn::parse_str(src).expect("parse file")
}

fn globset(patterns: &[&str]) -> GlobSet {
    let mut b = GlobSetBuilder::new();
    for p in patterns {
        b.add(Glob::new(p).unwrap());
    }
    b.build().unwrap()
}

fn adapter_layers() -> LayerDefinitions {
    LayerDefinitions::new(
        vec![
            "application".to_string(),
            "cli".to_string(),
            "mcp".to_string(),
        ],
        vec![
            ("application".to_string(), globset(&["src/application/**"])),
            ("cli".to_string(), globset(&["src/cli/**"])),
            ("mcp".to_string(), globset(&["src/mcp/**"])),
        ],
    )
}

fn names_for_layer<'ast>(
    by_layer: &std::collections::HashMap<String, Vec<PubFnInfo<'ast>>>,
    layer: &str,
) -> HashSet<String> {
    by_layer
        .get(layer)
        .map(|fns| fns.iter().map(|f| f.fn_name.clone()).collect())
        .unwrap_or_default()
}

#[test]
fn test_collect_pub_fns_in_layer_free_fn() {
    let file = parse(
        r#"
        pub fn cmd_stats() {}
        "#,
    );
    let files = vec![("src/cli/handlers.rs", &file)];
    let by_layer = {
        let aliases = aliases_from_files(&files);
        collect_pub_fns_by_layer(
            &files,
            &aliases,
            &adapter_layers(),
            &HashSet::new(),
            &HashSet::new(),
        )
    };
    let cli = names_for_layer(&by_layer, "cli");
    assert!(cli.contains("cmd_stats"), "cli = {cli:?}");
}

#[test]
fn test_collect_pub_fns_skips_private_fns() {
    let file = parse(
        r#"
        fn helper() {}
        pub fn cmd_stats() {}
        "#,
    );
    let files = vec![("src/cli/handlers.rs", &file)];
    let by_layer = {
        let aliases = aliases_from_files(&files);
        collect_pub_fns_by_layer(
            &files,
            &aliases,
            &adapter_layers(),
            &HashSet::new(),
            &HashSet::new(),
        )
    };
    let cli = names_for_layer(&by_layer, "cli");
    assert!(cli.contains("cmd_stats"));
    assert!(!cli.contains("helper"), "private fn must be skipped");
}

#[test]
fn test_pub_crate_is_treated_as_public_for_intra_crate_layers() {
    // Workspace-internal crates rely on `pub(crate)` for their architecture
    // surface; the check must treat any visibility-modified fn as
    // "visible enough" — only the implicit (no-modifier) case is private.
    let file = parse(
        r#"
        pub(crate) fn cmd_stats() {}
        "#,
    );
    let files = vec![("src/cli/handlers.rs", &file)];
    let by_layer = {
        let aliases = aliases_from_files(&files);
        collect_pub_fns_by_layer(
            &files,
            &aliases,
            &adapter_layers(),
            &HashSet::new(),
            &HashSet::new(),
        )
    };
    let cli = names_for_layer(&by_layer, "cli");
    assert!(cli.contains("cmd_stats"), "pub(crate) must be collected");
}

#[test]
fn test_pub_super_and_pub_in_path_treated_as_public() {
    let file = parse(
        r#"
        pub(super) fn cmd_a() {}
        pub(in crate::cli) fn cmd_b() {}
        "#,
    );
    let files = vec![("src/cli/handlers.rs", &file)];
    let by_layer = {
        let aliases = aliases_from_files(&files);
        collect_pub_fns_by_layer(
            &files,
            &aliases,
            &adapter_layers(),
            &HashSet::new(),
            &HashSet::new(),
        )
    };
    let cli = names_for_layer(&by_layer, "cli");
    assert!(cli.contains("cmd_a"));
    assert!(cli.contains("cmd_b"));
}

#[test]
fn test_collect_pub_fns_collects_pub_impl_methods_for_pub_type() {
    let file = parse(
        r#"
        pub struct Session;
        impl Session {
            pub fn search(&self) {}
            fn helper(&self) {}
        }
        "#,
    );
    let files = vec![("src/application/session.rs", &file)];
    let by_layer = {
        let aliases = aliases_from_files(&files);
        collect_pub_fns_by_layer(
            &files,
            &aliases,
            &adapter_layers(),
            &HashSet::new(),
            &HashSet::new(),
        )
    };
    let app = names_for_layer(&by_layer, "application");
    assert!(app.contains("search"), "pub impl method must be collected");
    assert!(
        !app.contains("helper"),
        "private impl method must be skipped"
    );
}

#[test]
fn test_collect_pub_fns_recognises_impl_across_files() {
    // Regression: `pub struct Session` in one file, `impl Session { pub
    // fn search() }` in another — both sides resolve to the same
    // canonical via the impl-file's `use` statement, so Check B sees
    // the impl methods as adapter surface. (Without the `use`, the
    // impl wouldn't compile in real Rust either.)
    let decl_file = parse("pub struct Session;");
    let impl_file = parse(
        r#"
        use crate::application::session::Session;
        impl Session {
            pub fn search(&self) {}
        }
        "#,
    );
    let files = vec![
        ("src/application/session.rs", &decl_file),
        ("src/application/session_impls.rs", &impl_file),
    ];
    let by_layer = {
        let aliases = aliases_from_files(&files);
        collect_pub_fns_by_layer(
            &files,
            &aliases,
            &adapter_layers(),
            &HashSet::new(),
            &HashSet::new(),
        )
    };
    let app = names_for_layer(&by_layer, "application");
    assert!(
        app.contains("search"),
        "cross-file impl on pub type must be collected, got {app:?}"
    );
}

#[test]
fn test_collect_pub_fns_skips_impl_methods_on_private_type() {
    // Conservative: if the enclosing `impl Type { ... }` is for a private
    // (no-modifier) type, its pub methods aren't really reachable from
    // outside the file, so they're excluded from the call-parity scope.
    let file = parse(
        r#"
        struct Session;
        impl Session {
            pub fn search(&self) {}
        }
        "#,
    );
    let files = vec![("src/application/session.rs", &file)];
    let by_layer = {
        let aliases = aliases_from_files(&files);
        collect_pub_fns_by_layer(
            &files,
            &aliases,
            &adapter_layers(),
            &HashSet::new(),
            &HashSet::new(),
        )
    };
    let app = names_for_layer(&by_layer, "application");
    assert!(
        !app.contains("search"),
        "impl on private type must be skipped"
    );
}

#[test]
fn test_collect_pub_fns_groups_by_layer() {
    let cli_file = parse("pub fn cmd_stats() {}");
    let mcp_file = parse("pub fn handle_stats() {}");
    let app_file = parse("pub fn get_stats() {}");
    let files = vec![
        ("src/cli/handlers.rs", &cli_file),
        ("src/mcp/handlers.rs", &mcp_file),
        ("src/application/stats.rs", &app_file),
    ];
    let by_layer = {
        let aliases = aliases_from_files(&files);
        collect_pub_fns_by_layer(
            &files,
            &aliases,
            &adapter_layers(),
            &HashSet::new(),
            &HashSet::new(),
        )
    };
    assert_eq!(
        names_for_layer(&by_layer, "cli"),
        ["cmd_stats".to_string()].into()
    );
    assert_eq!(
        names_for_layer(&by_layer, "mcp"),
        ["handle_stats".to_string()].into()
    );
    assert_eq!(
        names_for_layer(&by_layer, "application"),
        ["get_stats".to_string()].into()
    );
}

#[test]
fn test_collect_pub_fns_skips_cfg_test_files() {
    let file = parse("pub fn cmd_stats() {}");
    let files = vec![("src/cli/handlers.rs", &file)];
    let mut cfg_test = HashSet::new();
    cfg_test.insert("src/cli/handlers.rs".to_string());
    let aliases = aliases_from_files(&files);
    let by_layer = collect_pub_fns_by_layer(
        &files,
        &aliases,
        &adapter_layers(),
        &cfg_test,
        &HashSet::new(),
    );
    assert!(
        names_for_layer(&by_layer, "cli").is_empty(),
        "cfg-test file must be skipped wholesale"
    );
}

#[test]
fn test_collect_pub_fns_skips_test_attr_fns() {
    let file = parse(
        r#"
        #[test]
        pub fn not_a_handler() {}
        pub fn cmd_stats() {}
        "#,
    );
    let files = vec![("src/cli/handlers.rs", &file)];
    let by_layer = {
        let aliases = aliases_from_files(&files);
        collect_pub_fns_by_layer(
            &files,
            &aliases,
            &adapter_layers(),
            &HashSet::new(),
            &HashSet::new(),
        )
    };
    let cli = names_for_layer(&by_layer, "cli");
    assert!(cli.contains("cmd_stats"));
    assert!(!cli.contains("not_a_handler"), "#[test] fn must be skipped");
}

#[test]
fn test_collect_pub_fns_skips_unmatched_files() {
    // File not covered by any layer — its pub fns are not part of the
    // call-parity scope (neither as adapter member nor as target).
    let file = parse("pub fn free_floating() {}");
    let files = vec![("src/utils/misc.rs", &file)];
    let by_layer = {
        let aliases = aliases_from_files(&files);
        collect_pub_fns_by_layer(
            &files,
            &aliases,
            &adapter_layers(),
            &HashSet::new(),
            &HashSet::new(),
        )
    };
    for layer in ["application", "cli", "mcp"] {
        assert!(
            names_for_layer(&by_layer, layer).is_empty(),
            "layer {layer} must be empty"
        );
    }
}

#[test]
fn test_collect_pub_fns_skips_pub_fn_inside_private_inline_mod() {
    // `mod private { pub fn helper() {} }` — `helper` is `pub` but
    // its parent `mod private` has inherited visibility, so the fn
    // isn't reachable from outside the parent module. Must not be
    // recorded as adapter / target surface.
    let file = parse(
        r#"
        mod private {
            pub fn helper() {}
        }
        pub fn visible_top() {}
        "#,
    );
    let files = vec![("src/cli/handlers.rs", &file)];
    let by_layer = {
        let aliases = aliases_from_files(&files);
        collect_pub_fns_by_layer(
            &files,
            &aliases,
            &adapter_layers(),
            &HashSet::new(),
            &HashSet::new(),
        )
    };
    let cli = names_for_layer(&by_layer, "cli");
    assert!(
        cli.contains("visible_top"),
        "top-level pub fn must be recorded, got {cli:?}"
    );
    assert!(
        !cli.contains("helper"),
        "pub fn inside private inline mod must be skipped, got {cli:?}"
    );
}

#[test]
fn test_collect_pub_fns_treats_pub_self_as_private() {
    // `pub(self) fn helper()` is semantically private — equivalent to
    // inherited visibility. Must not be recorded.
    let file = parse(
        r#"
        pub(self) fn helper() {}
        pub fn visible() {}
        "#,
    );
    let files = vec![("src/cli/handlers.rs", &file)];
    let by_layer = {
        let aliases = aliases_from_files(&files);
        collect_pub_fns_by_layer(
            &files,
            &aliases,
            &adapter_layers(),
            &HashSet::new(),
            &HashSet::new(),
        )
    };
    let cli = names_for_layer(&by_layer, "cli");
    assert!(
        cli.contains("visible"),
        "plain `pub fn` must be recorded, got {cli:?}"
    );
    assert!(
        !cli.contains("helper"),
        "`pub(self) fn` is private-equivalent and must be skipped, got {cli:?}"
    );
}

#[test]
fn test_collect_pub_fns_skips_impl_method_on_type_in_private_inline_mod() {
    // `mod private { pub struct Hidden; impl Hidden { pub fn op() {} } }`
    // — `Hidden` is pub but only inside a private mod, so its
    // workspace-visible-types entry must NOT register, and the impl
    // method `op` must not appear as adapter surface.
    let file = parse(
        r#"
        mod private {
            pub struct Hidden;
            impl Hidden {
                pub fn op(&self) {}
            }
        }
        "#,
    );
    let files = vec![("src/cli/handlers.rs", &file)];
    let by_layer = {
        let aliases = aliases_from_files(&files);
        collect_pub_fns_by_layer(
            &files,
            &aliases,
            &adapter_layers(),
            &HashSet::new(),
            &HashSet::new(),
        )
    };
    let cli = names_for_layer(&by_layer, "cli");
    assert!(
        !cli.contains("op"),
        "impl method on type in private mod must be skipped, got {cli:?}"
    );
}

#[test]
fn test_collect_pub_fns_records_impl_in_private_mod_for_public_type() {
    // `pub struct Session` at file level, but its `impl` block lives
    // inside a private inline mod via `super::Session`. Rust treats
    // `s.diff()` as callable from any caller that can name `Session`,
    // so the public type's pub inherent methods must be recorded as
    // adapter surface — even though the impl block itself sits in a
    // private mod.
    let file = parse(
        r#"
        pub struct Session;
        mod methods {
            impl super::Session {
                pub fn diff(&self) {}
            }
        }
        "#,
    );
    let files = vec![("src/application/session.rs", &file)];
    let by_layer = {
        let aliases = aliases_from_files(&files);
        collect_pub_fns_by_layer(
            &files,
            &aliases,
            &adapter_layers(),
            &HashSet::new(),
            &HashSet::new(),
        )
    };
    let app = names_for_layer(&by_layer, "application");
    assert!(
        app.contains("diff"),
        "impl in private mod for public type must be recorded, got {app:?}"
    );
}

#[test]
fn test_collect_pub_fns_records_impl_via_nested_pub_use_export_path() {
    // `pub mod outer { pub use self::private::Hidden; }` re-exports
    // `Hidden` at `crate::file::outer::Hidden`. An impl written
    // against the export path must be recognised — visible_canonicals
    // needs both the source path *and* the export path so impl
    // resolution doesn't miss it.
    let file = parse(
        r#"
        pub mod outer {
            mod private {
                pub struct Hidden;
            }
            pub use self::private::Hidden;
        }
        impl outer::Hidden {
            pub fn op(&self) {}
        }
        "#,
    );
    let files = vec![("src/cli/handlers.rs", &file)];
    let by_layer = {
        let aliases = aliases_from_files(&files);
        collect_pub_fns_by_layer(
            &files,
            &aliases,
            &adapter_layers(),
            &HashSet::new(),
            &HashSet::new(),
        )
    };
    let cli = names_for_layer(&by_layer, "cli");
    assert!(
        cli.contains("op"),
        "impl on nested-mod re-export path must be recorded, got {cli:?}"
    );
}

#[test]
fn test_collect_pub_fns_records_impl_via_chained_type_alias() {
    // `type Inner = private::Hidden; pub type Public = Inner;` —
    // the alias chain must be followed to the source type, otherwise
    // visible_canonicals only contains `Inner` and the impl on
    // `Hidden` stays out of scope.
    let file = parse(
        r#"
        mod private {
            pub struct Hidden;
            impl Hidden {
                pub fn op(&self) {}
            }
        }
        type Inner = private::Hidden;
        pub type Public = Inner;
        "#,
    );
    let files = vec![("src/cli/handlers.rs", &file)];
    let by_layer = {
        let aliases = aliases_from_files(&files);
        collect_pub_fns_by_layer(
            &files,
            &aliases,
            &adapter_layers(),
            &HashSet::new(),
            &HashSet::new(),
        )
    };
    let cli = names_for_layer(&by_layer, "cli");
    assert!(
        cli.contains("op"),
        "alias chain target's impl method must be recorded, got {cli:?}"
    );
}

#[test]
fn test_collect_pub_fns_does_not_promote_bare_local_arc() {
    // `use crate::wrap::Arc; pub type Public = Arc<private::Hidden>;`
    // — bare `Arc` is shadowed by the local `use`. Visibility must
    // canonicalise first and refuse to auto-peel local Arcs.
    let file = parse(
        r#"
        mod wrap { pub struct Arc<T>(T); }
        use crate::wrap::Arc;
        mod private {
            pub struct Hidden;
            impl Hidden {
                pub fn op(&self) {}
            }
        }
        pub type Public = Arc<private::Hidden>;
        "#,
    );
    let files = vec![("src/cli/handlers.rs", &file)];
    let by_layer = {
        let aliases = aliases_from_files(&files);
        collect_pub_fns_by_layer(
            &files,
            &aliases,
            &adapter_layers(),
            &HashSet::new(),
            &HashSet::new(),
        )
    };
    let cli = names_for_layer(&by_layer, "cli");
    assert!(
        !cli.contains("op"),
        "bare Arc shadowed by local must not auto-peel, got {cli:?}"
    );
}

#[test]
fn test_collect_pub_fns_peels_qualified_user_wrapper() {
    // `pub type Public = axum::extract::State<private::Hidden>;` with
    // `transparent_wrappers = ["State"]`. External `axum::*` paths
    // can't be canonicalised, so the visibility pass must fall back
    // to last-segment matching for user-transparent wrappers.
    let file = parse(
        r#"
        mod private {
            pub struct Hidden;
            impl Hidden {
                pub fn op(&self) {}
            }
        }
        pub type Public = axum::extract::State<private::Hidden>;
        "#,
    );
    let files = vec![("src/cli/handlers.rs", &file)];
    let mut wrappers = HashSet::new();
    wrappers.insert("State".to_string());
    let by_layer = {
        let aliases = aliases_from_files(&files);
        collect_pub_fns_by_layer(
            &files,
            &aliases,
            &adapter_layers(),
            &HashSet::new(),
            &wrappers,
        )
    };
    let cli = names_for_layer(&by_layer, "cli");
    assert!(
        cli.contains("op"),
        "fully-qualified user wrapper must peel via leaf, got {cli:?}"
    );
}

#[test]
fn test_collect_pub_fns_does_not_promote_qualified_local_arc() {
    // `pub type Public = wrap::Arc<private::Hidden>;` — `wrap::Arc`
    // is a *local* wrapper, not stdlib. Direct dispatch on the leaf
    // `Arc` must NOT peel; otherwise Check B would require coverage
    // for methods on `private::Hidden` that aren't actually exposed.
    let file = parse(
        r#"
        mod wrap { pub struct Arc<T>(T); }
        mod private {
            pub struct Hidden;
            impl Hidden {
                pub fn op(&self) {}
            }
        }
        pub type Public = wrap::Arc<private::Hidden>;
        "#,
    );
    let files = vec![("src/cli/handlers.rs", &file)];
    let by_layer = {
        let aliases = aliases_from_files(&files);
        collect_pub_fns_by_layer(
            &files,
            &aliases,
            &adapter_layers(),
            &HashSet::new(),
            &HashSet::new(),
        )
    };
    let cli = names_for_layer(&by_layer, "cli");
    assert!(
        !cli.contains("op"),
        "qualified local Arc must not auto-peel as stdlib Arc, got {cli:?}"
    );
}

#[test]
fn test_collect_pub_fns_does_not_promote_local_wrapper_alias() {
    // `use crate::wrap::Arc as Shared;` aliases a *local* wrapper
    // type — its canonical (`crate::wrap::Arc`) doesn't start with
    // std/core/alloc, so the visibility pass must NOT auto-peel it
    // when it appears in `pub type Public = Shared<…>`. Only stdlib
    // wrappers are auto-peeled; user-configured wrappers stay
    // last-segment based.
    let file = parse(
        r#"
        use crate::wrap::Arc as Shared;
        mod private {
            pub struct Hidden;
            impl Hidden {
                pub fn op(&self) {}
            }
        }
        pub type Public = Shared<private::Hidden>;
        "#,
    );
    let files = vec![("src/cli/handlers.rs", &file)];
    let by_layer = {
        let aliases = aliases_from_files(&files);
        collect_pub_fns_by_layer(
            &files,
            &aliases,
            &adapter_layers(),
            &HashSet::new(),
            &HashSet::new(),
        )
    };
    let cli = names_for_layer(&by_layer, "cli");
    assert!(
        !cli.contains("op"),
        "local wrapper alias must not auto-peel as stdlib Arc, got {cli:?}"
    );
}

#[test]
fn test_collect_pub_fns_records_impl_via_renamed_stdlib_wrapper() {
    // `use std::sync::Arc as Shared; pub type Public = Shared<private::Hidden>;`
    // — the visibility pass must follow the import alias when peeling
    // wrappers, otherwise `Shared` is treated as a non-wrapper and
    // `private::Hidden` never enters `visible_canonicals`.
    let file = parse(
        r#"
        use std::sync::Arc as Shared;
        mod private {
            pub struct Hidden;
            impl Hidden {
                pub fn op(&self) {}
            }
        }
        pub type Public = Shared<private::Hidden>;
        "#,
    );
    let files = vec![("src/cli/handlers.rs", &file)];
    let by_layer = {
        let aliases = aliases_from_files(&files);
        collect_pub_fns_by_layer(
            &files,
            &aliases,
            &adapter_layers(),
            &HashSet::new(),
            &HashSet::new(),
        )
    };
    let cli = names_for_layer(&by_layer, "cli");
    assert!(
        cli.contains("op"),
        "renamed stdlib wrapper alias must peel in visibility pass, got {cli:?}"
    );
}

#[test]
fn test_collect_pub_fns_records_impl_via_pub_type_alias_through_user_wrapper() {
    // `pub type Public = State<private::Hidden>;` with a
    // user-configured transparent wrapper `State`. The visibility
    // pass must consult the same wrapper set the receiver resolver
    // uses, otherwise Check B drops the public target.
    let file = parse(
        r#"
        mod private {
            pub struct Hidden;
            impl Hidden {
                pub fn op(&self) {}
            }
        }
        pub type Public = State<private::Hidden>;
        "#,
    );
    let files = vec![("src/cli/handlers.rs", &file)];
    let mut wrappers = HashSet::new();
    wrappers.insert("State".to_string());
    let by_layer = {
        let aliases = aliases_from_files(&files);
        collect_pub_fns_by_layer(
            &files,
            &aliases,
            &adapter_layers(),
            &HashSet::new(),
            &wrappers,
        )
    };
    let cli = names_for_layer(&by_layer, "cli");
    assert!(
        cli.contains("op"),
        "user-wrapper alias target's impl method must be recorded, got {cli:?}"
    );
}

#[test]
fn test_collect_pub_fns_records_impl_via_pub_type_alias_through_wrapper() {
    // `pub type Public = Box<private::Hidden>;` — the alias target
    // is wrapped in a Deref-transparent smart pointer. Receiver
    // resolution peels Box/Arc/Rc/Cow, so the visible-types pass
    // must do the same to reach the inner `private::Hidden` and
    // recognise its impl methods as adapter surface.
    let file = parse(
        r#"
        mod private {
            pub struct Hidden;
            impl Hidden {
                pub fn op(&self) {}
            }
        }
        pub type Public = Box<private::Hidden>;
        "#,
    );
    let files = vec![("src/cli/handlers.rs", &file)];
    let by_layer = {
        let aliases = aliases_from_files(&files);
        collect_pub_fns_by_layer(
            &files,
            &aliases,
            &adapter_layers(),
            &HashSet::new(),
            &HashSet::new(),
        )
    };
    let cli = names_for_layer(&by_layer, "cli");
    assert!(
        cli.contains("op"),
        "wrapper-alias target's impl method must be recorded, got {cli:?}"
    );
}

#[test]
fn test_collect_pub_fns_records_impl_via_pub_type_alias() {
    // `pub type Public = private::Hidden;` exposes a hidden source
    // type's methods through the alias. Receiver-type inference
    // already resolves `Public` to its target, so the only piece
    // missing for Check B was visibility — register the target's
    // canonical alongside the alias path.
    let file = parse(
        r#"
        mod private {
            pub struct Hidden;
            impl Hidden {
                pub fn op(&self) {}
            }
        }
        pub type Public = private::Hidden;
        "#,
    );
    let files = vec![("src/cli/handlers.rs", &file)];
    let by_layer = {
        let aliases = aliases_from_files(&files);
        collect_pub_fns_by_layer(
            &files,
            &aliases,
            &adapter_layers(),
            &HashSet::new(),
            &HashSet::new(),
        )
    };
    let cli = names_for_layer(&by_layer, "cli");
    assert!(
        cli.contains("op"),
        "impl on pub-type-alias target must be recorded, got {cli:?}"
    );
}

#[test]
fn test_collect_pub_fns_records_renamed_reexport_impl_methods() {
    // `pub use private::Hidden as PublicHidden;` re-exports the
    // source type under a new name. The impl uses the original
    // `Hidden`, so visibility must resolve through the re-export
    // path — short-name matching against `PublicHidden` would miss
    // the impl. Recording must work via the source-canonical path
    // that both sides agree on.
    let file = parse(
        r#"
        mod private {
            pub struct Hidden;
        }
        impl private::Hidden {
            pub fn op(&self) {}
        }
        pub use private::Hidden as PublicHidden;
        "#,
    );
    let files = vec![("src/cli/handlers.rs", &file)];
    let by_layer = {
        let aliases = aliases_from_files(&files);
        collect_pub_fns_by_layer(
            &files,
            &aliases,
            &adapter_layers(),
            &HashSet::new(),
            &HashSet::new(),
        )
    };
    let cli = names_for_layer(&by_layer, "cli");
    assert!(
        cli.contains("op"),
        "renamed re-export must still expose impl method, got {cli:?}"
    );
}

#[test]
fn test_collect_pub_fns_records_pub_use_reexport_with_qualified_impl() {
    // `pub use private::Hidden;` with the impl at file level
    // (qualified `impl private::Hidden { … }`) — the re-export
    // resolves to the source-canonical `crate::file::private::Hidden`
    // and registers in `visible_canonicals`. The impl resolves to
    // the same canonical, so the methods record. With canonical-path
    // matching, impls *inside* `mod private` for the same re-exported
    // type also record correctly (the mod's own visibility no longer
    // gates impl methods).
    let file = parse(
        r#"
        mod private {
            pub struct Hidden;
        }
        impl private::Hidden {
            pub fn op(&self) {}
        }
        pub use private::Hidden;
        "#,
    );
    let files = vec![("src/cli/handlers.rs", &file)];
    let by_layer = {
        let aliases = aliases_from_files(&files);
        collect_pub_fns_by_layer(
            &files,
            &aliases,
            &adapter_layers(),
            &HashSet::new(),
            &HashSet::new(),
        )
    };
    let cli = names_for_layer(&by_layer, "cli");
    assert!(
        cli.contains("op"),
        "re-exported type with file-level impl must be recorded, got {cli:?}"
    );
}

#[test]
fn test_collect_pub_fns_skips_impl_methods_under_short_name_collision() {
    // Two distinct types named `Session` (one public, one in a
    // private inline mod) must NOT collide. The canonical-path-based
    // `visible_canonicals` set keys on full paths, so
    // `crate::cli::handlers::api::Session` and
    // `crate::cli::handlers::internal::Session` are distinct entries
    // — only the former is recorded (private mod's recursion is
    // skipped during the collection pass).
    let file = parse(
        r#"
        pub mod api {
            pub struct Session;
            impl Session {
                pub fn run(&self) {}
            }
        }
        mod internal {
            pub struct Session;
            impl Session {
                pub fn cleanup(&self) {}
            }
        }
        "#,
    );
    let files = vec![("src/cli/handlers.rs", &file)];
    let by_layer = {
        let aliases = aliases_from_files(&files);
        collect_pub_fns_by_layer(
            &files,
            &aliases,
            &adapter_layers(),
            &HashSet::new(),
            &HashSet::new(),
        )
    };
    let cli = names_for_layer(&by_layer, "cli");
    assert!(
        cli.contains("run"),
        "public-mod impl method must be recorded, got {cli:?}"
    );
    assert!(
        !cli.contains("cleanup"),
        "private-mod impl method must not leak via short-name collision, got {cli:?}"
    );
}