rustqual 1.1.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
//! Tests for the canonical call-target collector — the pre-pass that
//! turns a `syn::Block` into a `HashSet<String>` of canonical targets,
//! including receiver-type-tracked method calls.

use crate::adapters::analyzers::architecture::call_parity_rule::calls::{
    collect_canonical_calls, FnContext,
};
use crate::adapters::analyzers::architecture::call_parity_rule::workspace_graph::{
    collect_crate_root_modules, collect_local_symbols,
};
use crate::adapters::shared::use_tree::gather_alias_map;
use std::collections::{HashMap, HashSet};

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

fn parse_type(src: &str) -> syn::Type {
    syn::parse_str(src).expect("parse type")
}

/// Build a context from a full file source plus the name of the fn whose
/// body we want to analyse. Picks up the fn's signature + alias map
/// automatically.
struct FileCtx {
    file: syn::File,
    alias_map: HashMap<String, Vec<String>>,
    local_symbols: HashSet<String>,
    crate_root_modules: HashSet<String>,
}

fn load(src: &str) -> FileCtx {
    let file = parse_file(src);
    let alias_map = gather_alias_map(&file);
    let local_symbols = collect_local_symbols(&file);
    // For single-file unit tests the root module set is empty — tests
    // that exercise Rust-2018 absolute imports populate it manually.
    let crate_root_modules = HashSet::new();
    FileCtx {
        file,
        alias_map,
        local_symbols,
        crate_root_modules,
    }
}

fn load_with_roots(src: &str, roots: &[&str]) -> FileCtx {
    let mut fctx = load(src);
    fctx.crate_root_modules = roots.iter().map(|s| s.to_string()).collect();
    fctx
}

/// Convenience — rebuild crate_root_modules from a slice of pseudo-file
/// paths (same shape `build_call_graph` sees) so tests match the real
/// pipeline's derivation.
fn roots_from_paths(paths: &[&str]) -> HashSet<String> {
    let fake: Vec<(&str, &syn::File)> = Vec::new();
    let _ = fake;
    let dummy = parse_file("");
    let refs: Vec<(&str, &syn::File)> = paths.iter().map(|p| (*p, &dummy)).collect();
    collect_crate_root_modules(&refs)
}

fn find_fn<'a>(file: &'a syn::File, name: &str) -> &'a syn::ItemFn {
    file.items
        .iter()
        .find_map(|i| match i {
            syn::Item::Fn(f) if f.sig.ident == name => Some(f),
            _ => None,
        })
        .unwrap_or_else(|| panic!("fn {name} not found"))
}

fn impl_self_ty_name(item_impl: &syn::ItemImpl) -> Option<String> {
    match item_impl.self_ty.as_ref() {
        syn::Type::Path(p) => p.path.segments.last().map(|s| s.ident.to_string()),
        _ => None,
    }
}

fn find_impl_fn<'a>(
    file: &'a syn::File,
    type_name: &str,
    fn_name: &str,
) -> (&'a syn::ItemImpl, &'a syn::ImplItemFn) {
    file.items
        .iter()
        .filter_map(|item| match item {
            syn::Item::Impl(i) if impl_self_ty_name(i).as_deref() == Some(type_name) => Some(i),
            _ => None,
        })
        .find_map(|item_impl| {
            item_impl.items.iter().find_map(|it| match it {
                syn::ImplItem::Fn(f) if f.sig.ident == fn_name => Some((item_impl, f)),
                _ => None,
            })
        })
        .unwrap_or_else(|| panic!("impl {type_name}::{fn_name} not found"))
}

fn sig_params(sig: &syn::Signature) -> Vec<(String, &syn::Type)> {
    sig.inputs
        .iter()
        .filter_map(|arg| match arg {
            syn::FnArg::Typed(pt) => {
                let name = match pt.pat.as_ref() {
                    syn::Pat::Ident(pi) => pi.ident.to_string(),
                    _ => return None,
                };
                Some((name, pt.ty.as_ref()))
            }
            _ => None,
        })
        .collect()
}

fn ctx_for_fn<'a>(fctx: &'a FileCtx, fn_name: &str, importing_file: &'a str) -> FnContext<'a> {
    let f = find_fn(&fctx.file, fn_name);
    FnContext {
        body: &f.block,
        signature_params: sig_params(&f.sig),
        self_type: None,
        alias_map: &fctx.alias_map,
        local_symbols: &fctx.local_symbols,
        crate_root_modules: &fctx.crate_root_modules,
        importing_file,
    }
}

fn canonical_of_impl_self(item: &syn::ItemImpl) -> Option<Vec<String>> {
    if let syn::Type::Path(p) = item.self_ty.as_ref() {
        Some(
            p.path
                .segments
                .iter()
                .map(|s| s.ident.to_string())
                .collect(),
        )
    } else {
        None
    }
}

// ── Basic call resolution ─────────────────────────────────────

#[test]
fn test_collect_direct_qualified_call() {
    let fctx = load(
        r#"
        pub fn cmd_search() {
            crate::application::stats::get_stats(1);
        }
        "#,
    );
    let ctx = ctx_for_fn(&fctx, "cmd_search", "src/cli/handlers.rs");
    let calls = collect_canonical_calls(&ctx);
    assert!(calls.contains("crate::application::stats::get_stats"));
}

#[test]
fn test_collect_unqualified_via_use_alias() {
    let fctx = load(
        r#"
        use crate::application::stats::get_stats;
        pub fn cmd_search() {
            get_stats(1);
        }
        "#,
    );
    let ctx = ctx_for_fn(&fctx, "cmd_search", "src/cli/handlers.rs");
    let calls = collect_canonical_calls(&ctx);
    assert!(calls.contains("crate::application::stats::get_stats"));
}

#[test]
fn test_collect_unqualified_no_alias_is_bare() {
    let fctx = load(
        r#"
        pub fn cmd_search() {
            foo(1);
        }
        "#,
    );
    let ctx = ctx_for_fn(&fctx, "cmd_search", "src/cli/handlers.rs");
    let calls = collect_canonical_calls(&ctx);
    assert!(calls.contains("<bare>:foo"));
}

#[test]
fn test_collect_in_semicolon_separated_macro_descends() {
    // Regression: `vec![expr; n]`-style macros have tokens `expr ; n`
    // which fails the comma-list parser. The block fallback wraps them
    // in braces and extracts stmt-level expressions.
    let fctx = load(
        r#"
        pub fn cmd_search() {
            let _v = vec![compute(x); 3];
        }
        "#,
    );
    let ctx = ctx_for_fn(&fctx, "cmd_search", "src/cli/handlers.rs");
    let calls = collect_canonical_calls(&ctx);
    assert!(
        calls.contains("<bare>:compute"),
        "`;`-separated macro body must still descend, got {calls:?}"
    );
}

#[test]
fn test_collect_in_macro_descends() {
    let fctx = load(
        r#"
        pub fn cmd_search() {
            debug_assert!(validate(1));
        }
        "#,
    );
    let ctx = ctx_for_fn(&fctx, "cmd_search", "src/cli/handlers.rs");
    let calls = collect_canonical_calls(&ctx);
    assert!(calls.contains("<bare>:validate"));
    // The macro itself must not be recorded as a call target.
    assert!(!calls.contains("<macro>:debug_assert"));
}

#[test]
fn test_collect_self_super_prefix() {
    // `self::` inside `src/cli/mod.rs` resolves against module `crate::cli`,
    // so `self::helpers::format` → `crate::cli::helpers::format`. A non-mod
    // file (e.g. `src/cli/handlers.rs`) would resolve to
    // `crate::cli::handlers::helpers::format`, which is the Rust-semantic
    // outcome; the collector defers to `resolve_to_crate_absolute` for
    // this so both cases stay consistent with the file's module path.
    let fctx = load(
        r#"
        pub fn cmd_search() {
            self::helpers::format(1);
        }
        "#,
    );
    let ctx = ctx_for_fn(&fctx, "cmd_search", "src/cli/mod.rs");
    let calls = collect_canonical_calls(&ctx);
    assert!(
        calls.contains("crate::cli::helpers::format"),
        "calls = {:?}",
        calls
    );
}

#[test]
fn test_collect_turbofish_stripped() {
    let fctx = load(
        r#"
        pub fn cmd_search() {
            Box::<u32>::new(42);
        }
        "#,
    );
    let ctx = ctx_for_fn(&fctx, "cmd_search", "src/cli/handlers.rs");
    let calls = collect_canonical_calls(&ctx);
    assert!(calls.contains("<bare>:Box::new"));
}

#[test]
fn test_collect_closure_body_collected() {
    let fctx = load(
        r#"
        pub fn cmd_search() {
            let f = |x: u32| inner_call(x);
            f(1);
        }
        "#,
    );
    let ctx = ctx_for_fn(&fctx, "cmd_search", "src/cli/handlers.rs");
    let calls = collect_canonical_calls(&ctx);
    assert!(calls.contains("<bare>:inner_call"));
}

#[test]
fn test_collect_await_is_not_extra_call() {
    let fctx = load(
        r#"
        pub async fn cmd_search() {
            f(1).await;
        }
        "#,
    );
    let ctx = ctx_for_fn(&fctx, "cmd_search", "src/cli/handlers.rs");
    let calls = collect_canonical_calls(&ctx);
    assert!(calls.contains("<bare>:f"));
    // .await is not a call target
    assert!(!calls.iter().any(|c| c.contains("await")));
}

#[test]
fn test_collect_self_dispatch_in_impl() {
    let fctx = load(
        r#"
        pub struct RlmSession;
        impl RlmSession {
            pub fn search(&self) {
                Self::internal_helper();
            }
        }
        "#,
    );
    let (item, f) = find_impl_fn(&fctx.file, "RlmSession", "search");
    let self_ty = canonical_of_impl_self(item);
    let ctx = FnContext {
        body: &f.block,
        signature_params: sig_params(&f.sig),
        self_type: self_ty,
        alias_map: &fctx.alias_map,
        local_symbols: &fctx.local_symbols,
        crate_root_modules: &fctx.crate_root_modules,
        importing_file: "src/application/session.rs",
    };
    let calls = collect_canonical_calls(&ctx);
    assert!(
        calls.contains("crate::application::session::RlmSession::internal_helper"),
        "calls = {:?}",
        calls
    );
}

// ── Receiver-Type-Tracking ────────────────────────────────────

#[test]
fn test_tracker_let_constructor_binding() {
    let fctx = load(
        r#"
        use crate::app::session::RlmSession;
        pub fn cmd_search(q: u32) {
            let s = RlmSession::open_cwd();
            s.search(q);
        }
        "#,
    );
    let ctx = ctx_for_fn(&fctx, "cmd_search", "src/cli/handlers.rs");
    let calls = collect_canonical_calls(&ctx);
    assert!(
        calls.contains("crate::app::session::RlmSession::search"),
        "calls = {:?}",
        calls
    );
}

#[test]
fn test_tracker_let_type_annotation() {
    let fctx = load(
        r#"
        use crate::app::session::RlmSession;
        pub fn cmd_search(q: u32) {
            let s: RlmSession = make_session();
            s.search(q);
        }
        "#,
    );
    let ctx = ctx_for_fn(&fctx, "cmd_search", "src/cli/handlers.rs");
    let calls = collect_canonical_calls(&ctx);
    assert!(calls.contains("crate::app::session::RlmSession::search"));
}

#[test]
fn test_tracker_fn_param_type() {
    let fctx = load(
        r#"
        use crate::app::session::RlmSession;
        pub fn handle(session: RlmSession) {
            session.search(1);
        }
        "#,
    );
    let ctx = ctx_for_fn(&fctx, "handle", "src/mcp/handlers.rs");
    let calls = collect_canonical_calls(&ctx);
    assert!(calls.contains("crate::app::session::RlmSession::search"));
}

#[test]
fn test_tracker_fn_param_ref_type() {
    let fctx = load(
        r#"
        use crate::app::session::RlmSession;
        pub fn handle(session: &RlmSession) {
            session.search(1);
        }
        "#,
    );
    let ctx = ctx_for_fn(&fctx, "handle", "src/mcp/handlers.rs");
    let calls = collect_canonical_calls(&ctx);
    assert!(calls.contains("crate::app::session::RlmSession::search"));
}

#[test]
fn test_tracker_fn_param_arc_type() {
    let fctx = load(
        r#"
        use crate::app::session::RlmSession;
        use std::sync::Arc;
        pub fn handle(session: Arc<RlmSession>) {
            session.search(1);
        }
        "#,
    );
    let ctx = ctx_for_fn(&fctx, "handle", "src/mcp/handlers.rs");
    let calls = collect_canonical_calls(&ctx);
    assert!(calls.contains("crate::app::session::RlmSession::search"));
}

#[test]
fn test_tracker_fn_param_box_ref_mut_type() {
    let fctx = load(
        r#"
        use crate::app::session::RlmSession;
        pub fn a(session: Box<RlmSession>) { session.search(1); }
        pub fn b(session: &mut RlmSession) { session.search(1); }
        "#,
    );
    for name in &["a", "b"] {
        let ctx = ctx_for_fn(&fctx, name, "src/mcp/handlers.rs");
        let calls = collect_canonical_calls(&ctx);
        assert!(
            calls.contains("crate::app::session::RlmSession::search"),
            "fn {name} calls = {:?}",
            calls
        );
    }
}

#[test]
fn test_tracker_alias_resolved_constructor() {
    let fctx = load(
        r#"
        use crate::app::session::RlmSession;
        pub fn cmd_search() {
            let s = RlmSession::open();
            s.search(1);
        }
        "#,
    );
    let ctx = ctx_for_fn(&fctx, "cmd_search", "src/cli/handlers.rs");
    let calls = collect_canonical_calls(&ctx);
    assert!(calls.contains("crate::app::session::RlmSession::search"));
}

#[test]
fn test_tracker_shadowing_uses_latest() {
    let fctx = load(
        r#"
        use crate::app::session::RlmSession;
        use crate::cli::CliSession;
        pub fn cmd_search() {
            let s = CliSession::new();
            let s = RlmSession::open();
            s.search(1);
        }
        "#,
    );
    let ctx = ctx_for_fn(&fctx, "cmd_search", "src/cli/handlers.rs");
    let calls = collect_canonical_calls(&ctx);
    assert!(calls.contains("crate::app::session::RlmSession::search"));
    assert!(!calls.contains("crate::cli::CliSession::search"));
}

#[test]
fn test_tracker_unknown_receiver_falls_back_to_method_shape() {
    let fctx = load(
        r#"
        pub fn cmd_search(x: UnknownType) {
            x.search(1);
        }
        "#,
    );
    let ctx = ctx_for_fn(&fctx, "cmd_search", "src/cli/handlers.rs");
    let calls = collect_canonical_calls(&ctx);
    assert!(calls.contains("<method>:search"));
    assert!(!calls.iter().any(|c| c.contains("UnknownType::search")));
}

#[test]
fn test_tracker_closure_inherits_parent_bindings() {
    let fctx = load(
        r#"
        use crate::app::session::RlmSession;
        pub fn cmd_search() {
            let s = RlmSession::open();
            let f = || s.search(1);
            f();
        }
        "#,
    );
    let ctx = ctx_for_fn(&fctx, "cmd_search", "src/cli/handlers.rs");
    let calls = collect_canonical_calls(&ctx);
    assert!(calls.contains("crate::app::session::RlmSession::search"));
}

#[test]
fn test_tracker_factory_helper_unresolved_falls_back_to_method_shape() {
    // Documented limitation: no 1-hop return-type inference.
    let fctx = load(
        r#"
        pub fn cmd_search() {
            let s = helpers::open_session();
            s.search(1);
        }
        "#,
    );
    let ctx = ctx_for_fn(&fctx, "cmd_search", "src/cli/handlers.rs");
    let calls = collect_canonical_calls(&ctx);
    assert!(calls.contains("<method>:search"));
}

#[test]
fn test_tracker_in_async_fn() {
    let fctx = load(
        r#"
        use crate::app::session::RlmSession;
        pub async fn handle(s: RlmSession) {
            s.search(1).await;
        }
        "#,
    );
    let ctx = ctx_for_fn(&fctx, "handle", "src/mcp/handlers.rs");
    let calls = collect_canonical_calls(&ctx);
    assert!(calls.contains("crate::app::session::RlmSession::search"));
}

#[test]
fn test_collect_async_block() {
    let fctx = load(
        r#"
        use crate::app::session::RlmSession;
        pub fn cmd_search() {
            let s = RlmSession::open();
            let _fut = async { s.search(1) };
        }
        "#,
    );
    let ctx = ctx_for_fn(&fctx, "cmd_search", "src/cli/handlers.rs");
    let calls = collect_canonical_calls(&ctx);
    assert!(
        calls.contains("crate::app::session::RlmSession::search"),
        "calls = {:?}",
        calls
    );
}

#[test]
fn test_empty_body_yields_no_calls() {
    let fctx = load("pub fn f() {}");
    let ctx = ctx_for_fn(&fctx, "f", "src/cli/handlers.rs");
    let calls = collect_canonical_calls(&ctx);
    assert_eq!(calls, HashSet::<String>::new());
}

#[test]
fn test_local_helper_call_resolves_to_crate_module() {
    // Regression: `helper()` without a `use` statement is a valid Rust
    // same-module call. Must resolve to `crate::<file_module>::helper`
    // so the graph sees the edge — not `<bare>:helper` dead-end.
    let fctx = load(
        r#"
        fn helper() {}
        pub fn cmd_foo() {
            helper();
        }
        "#,
    );
    let ctx = ctx_for_fn(&fctx, "cmd_foo", "src/cli/handlers.rs");
    let calls = collect_canonical_calls(&ctx);
    assert!(
        calls.contains("crate::cli::handlers::helper"),
        "local helper must resolve via file module, got {calls:?}"
    );
    assert!(
        !calls.contains("<bare>:helper"),
        "local helper must not fall back to bare, got {calls:?}"
    );
}

#[test]
fn test_external_call_without_use_still_falls_to_bare() {
    // Conservative: if the first segment isn't in local_symbols (and no
    // `use` aliased it), stay `<bare>:…`. Otherwise external crate or
    // stdlib calls would be wrongly attributed to the local module.
    let fctx = load(
        r#"
        pub fn cmd_foo() {
            not_a_local_symbol();
        }
        "#,
    );
    let ctx = ctx_for_fn(&fctx, "cmd_foo", "src/cli/handlers.rs");
    let calls = collect_canonical_calls(&ctx);
    assert!(
        calls.contains("<bare>:not_a_local_symbol"),
        "unknown fn must stay bare, got {calls:?}"
    );
}

#[test]
fn test_super_aliased_call_normalises_to_crate_rooted() {
    // `use super::stats::get_stats;` expands to `["super","stats","get_stats"]`
    // in the alias map. Without normalisation the canonical would be
    // `super::stats::get_stats`, which never matches graph nodes.
    // Post-alias re-normalisation turns it into `crate::…::get_stats`.
    let fctx = load(
        r#"
        use super::stats::get_stats;
        pub fn cmd_foo() {
            get_stats();
        }
        "#,
    );
    let ctx = ctx_for_fn(&fctx, "cmd_foo", "src/cli/handlers.rs");
    let calls = collect_canonical_calls(&ctx);
    assert!(
        calls.contains("crate::cli::stats::get_stats"),
        "super-aliased call must normalise to crate::, got {calls:?}"
    );
    assert!(
        !calls.iter().any(|c| c.starts_with("super::")),
        "super-rooted canonical must not leak, got {calls:?}"
    );
}

#[test]
fn test_unqualified_local_type_in_signature_resolves() {
    // `struct Session;` declared in this file + `fn f(s: Session)` — Rust
    // doesn't require a `use` for same-file types. Receiver tracking must
    // still resolve `s.search()` via the local-type fallback.
    let fctx = load(
        r#"
        pub struct Session;
        impl Session {
            pub fn search(&self) {}
        }
        pub fn cmd_foo(s: Session) {
            s.search();
        }
        "#,
    );
    let ctx = ctx_for_fn(&fctx, "cmd_foo", "src/application/session.rs");
    let calls = collect_canonical_calls(&ctx);
    assert!(
        calls.contains("crate::application::session::Session::search"),
        "unqualified local-type receiver must resolve, got {calls:?}"
    );
    assert!(
        !calls.contains("<method>:search"),
        "must not fall back to <method>:, got {calls:?}"
    );
}

#[test]
fn test_rust2018_absolute_call_without_use_resolves_to_crate_rooted() {
    // Regression: `app::foo()` called directly (no `use app::foo;`) is
    // also a crate-root module call in Rust 2018+. Must resolve to
    // `crate::app::foo`, mirroring the alias-backed case.
    let fctx = load_with_roots(
        r#"
        pub fn cmd_x() {
            app::foo();
        }
        "#,
        &["app"],
    );
    let ctx = ctx_for_fn(&fctx, "cmd_x", "src/cli/handlers.rs");
    let calls = collect_canonical_calls(&ctx);
    assert!(
        calls.contains("crate::app::foo"),
        "unaliased Rust 2018+ call must crate-prefix, got {calls:?}"
    );
    assert!(
        !calls.iter().any(|c| c == "<bare>:app::foo"),
        "must not fall back to bare, got {calls:?}"
    );
}

#[test]
fn test_rust2018_absolute_import_resolves_to_crate_rooted() {
    // Rust 2018+: `use app::foo;` at the top of a non-root file is the
    // crate-root module `app`, equivalent to `use crate::app::foo;`.
    // When `app` is a known workspace root module, the alias expansion
    // must prepend `crate::` so the call graph matches.
    let fctx = load_with_roots(
        r#"
        use app::foo;
        pub fn cmd_x() {
            foo();
        }
        "#,
        &["app"],
    );
    let ctx = ctx_for_fn(&fctx, "cmd_x", "src/cli/handlers.rs");
    let calls = collect_canonical_calls(&ctx);
    assert!(
        calls.contains("crate::app::foo"),
        "Rust 2018+ absolute import must normalise to crate::, got {calls:?}"
    );
    assert!(
        !calls.iter().any(|c| c == "app::foo"),
        "must not leave unprefixed app::foo, got {calls:?}"
    );
}

#[test]
fn test_collect_crate_root_modules_from_paths() {
    // `src/app/mod.rs`, `src/app/session.rs`, `src/cli/handlers.rs` →
    // {"app", "cli"}. `src/lib.rs` and `src/main.rs` are excluded.
    let roots = roots_from_paths(&[
        "src/app/mod.rs",
        "src/app/session.rs",
        "src/cli/handlers.rs",
        "src/lib.rs",
        "src/main.rs",
    ]);
    assert!(roots.contains("app"));
    assert!(roots.contains("cli"));
    assert!(!roots.contains("lib"));
    assert!(!roots.contains("main"));
}

#[test]
fn test_top_level_self_as_alias_maps_to_current_file() {
    // `use self as fs;` at the top of `src/util/fs_helpers.rs` — `self`
    // at crate-root-adjacent position means the current file's module.
    // Downstream normalisation must resolve `fs::something` to
    // `crate::util::fs_helpers::something`, not leak as a dead-end.
    let fctx = load(
        r#"
        use self as fs;
        pub fn cmd_x() {
            fs::something();
        }
        "#,
    );
    let ctx = ctx_for_fn(&fctx, "cmd_x", "src/util/fs_helpers.rs");
    let calls = collect_canonical_calls(&ctx);
    assert!(
        calls.contains("crate::util::fs_helpers::something"),
        "top-level self-alias must resolve to the current file's module, got {calls:?}"
    );
}

#[test]
fn test_qualified_impl_path_does_not_double_crate() {
    // `impl crate::app::Session { fn search() }` — the impl header
    // already gives a crate-rooted path. The canonical Self-target must
    // be `crate::app::Session::search`, NOT
    // `crate::<file_module>::crate::app::Session::search`.
    let fctx = load(
        r#"
        impl crate::app::Session {
            pub fn search(&self) {
                Self::internal_helper();
            }
        }
        "#,
    );
    let (item, f) = find_impl_fn(&fctx.file, "Session", "search");
    let self_ty = canonical_of_impl_self(item);
    let ctx = FnContext {
        body: &f.block,
        signature_params: sig_params(&f.sig),
        self_type: self_ty,
        alias_map: &fctx.alias_map,
        local_symbols: &fctx.local_symbols,
        crate_root_modules: &fctx.crate_root_modules,
        importing_file: "src/other_file.rs",
    };
    let calls = collect_canonical_calls(&ctx);
    assert!(
        calls.contains("crate::app::Session::internal_helper"),
        "qualified impl path must canonicalise as-is, got {calls:?}"
    );
    assert!(
        !calls.iter().any(|c| c.contains("crate::crate::")),
        "must not double-crate, got {calls:?}"
    );
}