rustqual 1.2.3

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
//! Tests for `compute_touchpoints` — the boundary-only forward BFS that
//! computes, for one adapter handler, the set of target-layer canonicals
//! it reaches before stopping at the boundary.
//!
//! These tests exercise the algorithm via small multi-file workspaces
//! built through `support::compute_touchpoints_for`, which mirrors the
//! `run_check_a/b` style used elsewhere.

use super::support::{
    build_workspace, cli_mcp_config, compute_touchpoints_for, empty_cfg_test, ports_app_cli_mcp,
    three_layer,
};
use std::collections::HashSet;

fn assert_set(actual: HashSet<String>, expected: &[&str]) {
    let expected_set: HashSet<String> = expected.iter().map(|s| s.to_string()).collect();
    assert_eq!(
        actual, expected_set,
        "touchpoint set mismatch — actual={actual:?} expected={expected_set:?}"
    );
}

// ── Direct call ──────────────────────────────────────────────────

#[test]
fn touchpoints_direct_call() {
    let ws = build_workspace(&[
        ("src/application/session.rs", "pub fn search() {}"),
        (
            "src/cli/handlers.rs",
            r#"
            use crate::application::session::search;
            pub fn cmd_search() { search(); }
            "#,
        ),
    ]);
    let touchpoints = compute_touchpoints_for(
        &ws,
        &three_layer(),
        &cli_mcp_config(3),
        "cmd_search",
        &empty_cfg_test(),
    );
    assert_set(touchpoints, &["crate::application::session::search"]);
}

// ── Adapter helper traversed before boundary ─────────────────────

#[test]
fn touchpoints_via_adapter_helper() {
    let ws = build_workspace(&[
        ("src/application/session.rs", "pub fn search() {}"),
        (
            "src/cli/helpers.rs",
            r#"
            use crate::application::session::search;
            pub fn format_query() { search(); }
            "#,
        ),
        (
            "src/cli/handlers.rs",
            r#"
            use crate::cli::helpers::format_query;
            pub fn cmd_search() { format_query(); }
            "#,
        ),
    ]);
    let touchpoints = compute_touchpoints_for(
        &ws,
        &three_layer(),
        &cli_mcp_config(3),
        "cmd_search",
        &empty_cfg_test(),
    );
    assert_set(touchpoints, &["crate::application::session::search"]);
}

// ── No delegation: handler stays in adapter layer ────────────────

#[test]
fn touchpoints_no_delegation() {
    let ws = build_workspace(&[
        ("src/application/session.rs", "pub fn search() {}"),
        (
            "src/cli/helpers.rs",
            r#"
            pub fn adapter_helper2() {}
            pub fn adapter_helper() { adapter_helper2(); }
            "#,
        ),
        (
            "src/cli/handlers.rs",
            r#"
            use crate::cli::helpers::adapter_helper;
            pub fn cmd_local_only() { adapter_helper(); }
            "#,
        ),
    ]);
    let touchpoints = compute_touchpoints_for(
        &ws,
        &three_layer(),
        &cli_mcp_config(3),
        "cmd_local_only",
        &empty_cfg_test(),
    );
    assert!(
        touchpoints.is_empty(),
        "no-delegation handler should produce empty touchpoint set, got {touchpoints:?}"
    );
}

// ── Branch with two distinct target functions ────────────────────

#[test]
fn touchpoints_branch_two_targets() {
    let ws = build_workspace(&[
        (
            "src/application/session.rs",
            r#"
            pub fn foo() {}
            pub fn bar() {}
            "#,
        ),
        (
            "src/cli/handlers.rs",
            r#"
            use crate::application::session::{foo, bar};
            pub fn cmd_branch(cond: bool) {
                if cond { foo(); } else { bar(); }
            }
            "#,
        ),
    ]);
    let touchpoints = compute_touchpoints_for(
        &ws,
        &three_layer(),
        &cli_mcp_config(3),
        "cmd_branch",
        &empty_cfg_test(),
    );
    assert_set(
        touchpoints,
        &[
            "crate::application::session::foo",
            "crate::application::session::bar",
        ],
    );
}

// ── Loop: same target multiple call sites → single touchpoint ────

#[test]
fn touchpoints_loop_same_target() {
    let ws = build_workspace(&[
        ("src/application/session.rs", "pub fn process(_x: u32) {}"),
        (
            "src/cli/handlers.rs",
            r#"
            use crate::application::session::process;
            pub fn cmd_batch() {
                for x in 0..10 { process(x); }
            }
            "#,
        ),
    ]);
    let touchpoints = compute_touchpoints_for(
        &ws,
        &three_layer(),
        &cli_mcp_config(3),
        "cmd_batch",
        &empty_cfg_test(),
    );
    assert_set(touchpoints, &["crate::application::session::process"]);
}

// ── Stop at boundary: target callees not in the set ──────────────

#[test]
fn touchpoints_stop_at_target_boundary() {
    let ws = build_workspace(&[
        (
            "src/application/session.rs",
            r#"
            use crate::application::middleware::record_operation;
            pub fn search() { record_operation(); }
            "#,
        ),
        (
            "src/application/middleware.rs",
            r#"
            pub fn record_operation() {}
            "#,
        ),
        (
            "src/cli/handlers.rs",
            r#"
            use crate::application::session::search;
            pub fn cmd_search() { search(); }
            "#,
        ),
    ]);
    let touchpoints = compute_touchpoints_for(
        &ws,
        &three_layer(),
        &cli_mcp_config(3),
        "cmd_search",
        &empty_cfg_test(),
    );
    // Only session::search — the boundary semantic stops here. The
    // application-internal `record_operation` MUST NOT appear in the set.
    assert_set(touchpoints, &["crate::application::session::search"]);
}

// ── Depth limit: chain too deep returns empty ────────────────────

#[test]
fn touchpoints_call_depth_exceeded() {
    // cmd → h1 → h2 → h3 → h4 → search (5 hops). depth=3 stops short.
    let ws = build_workspace(&[
        ("src/application/session.rs", "pub fn search() {}"),
        (
            "src/cli/helpers.rs",
            r#"
            use crate::application::session::search;
            pub fn h4() { search(); }
            pub fn h3() { h4(); }
            pub fn h2() { h3(); }
            pub fn h1() { h2(); }
            "#,
        ),
        (
            "src/cli/handlers.rs",
            r#"
            use crate::cli::helpers::h1;
            pub fn cmd_deep() { h1(); }
            "#,
        ),
    ]);
    let touchpoints = compute_touchpoints_for(
        &ws,
        &three_layer(),
        &cli_mcp_config(3),
        "cmd_deep",
        &empty_cfg_test(),
    );
    assert!(
        touchpoints.is_empty(),
        "depth=3 should not reach a 5-hop target, got {touchpoints:?}"
    );
}

// ── Depth limit: chain just inside limit returns target ──────────

#[test]
fn touchpoints_call_depth_just_inside() {
    // cmd → h1 → h2 → search (3 hops). depth=3 reaches target.
    let ws = build_workspace(&[
        ("src/application/session.rs", "pub fn search() {}"),
        (
            "src/cli/helpers.rs",
            r#"
            use crate::application::session::search;
            pub fn h2() { search(); }
            pub fn h1() { h2(); }
            "#,
        ),
        (
            "src/cli/handlers.rs",
            r#"
            use crate::cli::helpers::h1;
            pub fn cmd_depth3() { h1(); }
            "#,
        ),
    ]);
    let touchpoints = compute_touchpoints_for(
        &ws,
        &three_layer(),
        &cli_mcp_config(3),
        "cmd_depth3",
        &empty_cfg_test(),
    );
    assert_set(touchpoints, &["crate::application::session::search"]);
}

// ── Trait-dispatch anchor ────────────────────────────────────────

#[test]
fn touchpoints_trait_dispatch_collapses_to_anchor() {
    // CLI handler calls `dyn Handler.handle()` where Handler has THREE
    // overriding impls in the application layer. Touchpoint set must be
    // ONE anchor `<Trait>::<method>`, not three impl edges. Otherwise
    // Check C fires multi-touchpoint for what is semantically a single
    // boundary call.
    let ws = build_workspace(&[
        (
            "src/application/handler.rs",
            r#"
            pub trait Handler { fn handle(&self); }
            pub struct LoggingHandler;
            impl Handler for LoggingHandler { fn handle(&self) {} }
            pub struct MetricsHandler;
            impl Handler for MetricsHandler { fn handle(&self) {} }
            pub struct AuditHandler;
            impl Handler for AuditHandler { fn handle(&self) {} }
            "#,
        ),
        (
            "src/cli/handlers.rs",
            r#"
            use crate::application::handler::Handler;
            pub fn cmd_dispatch(h: &dyn Handler) { h.handle(); }
            "#,
        ),
    ]);
    let touchpoints = compute_touchpoints_for(
        &ws,
        &three_layer(),
        &cli_mcp_config(3),
        "cmd_dispatch",
        &empty_cfg_test(),
    );
    assert_set(
        touchpoints,
        &["crate::application::handler::Handler::handle"],
    );
}

#[test]
fn touchpoints_trait_anchor_recognized_when_trait_lives_in_ports_layer() {
    // Hexagonal/Ports&Adapters case: trait declared in `ports` layer,
    // impls in `application` (target) layer. Dispatch emits anchor
    // `crate::ports::Handler::handle` (anchor lives in ports). Walker
    // must still register the anchor as a target boundary because
    // its impls reach the target layer — otherwise Check A would
    // falsely fire "no delegation" for a CLI command that legitimately
    // crosses into the target via trait dispatch.
    let ws = build_workspace(&[
        (
            "src/ports/handler.rs",
            "pub trait Handler { fn handle(&self); }",
        ),
        (
            "src/application/logging.rs",
            r#"
            use crate::ports::handler::Handler;
            pub struct LoggingHandler;
            impl Handler for LoggingHandler { fn handle(&self) {} }
            "#,
        ),
        (
            "src/cli/handlers.rs",
            r#"
            use crate::ports::handler::Handler;
            pub fn cmd_dispatch(h: &dyn Handler) { h.handle(); }
            "#,
        ),
    ]);
    let mut config = cli_mcp_config(3);
    config.target = "application".to_string();
    let touchpoints = compute_touchpoints_for(
        &ws,
        &ports_app_cli_mcp(),
        &config,
        "cmd_dispatch",
        &empty_cfg_test(),
    );
    assert_set(touchpoints, &["crate::ports::handler::Handler::handle"]);
}

#[test]
fn touchpoints_skip_anchor_declared_in_peer_adapter_layer() {
    // Trait declared in peer-adapter layer (`mcp`), with an overriding
    // impl in the target layer (`application`). CLI handler calls
    // `dyn mcp::Handler.handle()`. The anchor lives in `mcp` (peer
    // adapter), so the walker MUST refuse to register it as a target
    // boundary — otherwise CLI inherits MCP's reachability into
    // application via the anchor and fakes adapter delegation.
    let ws = build_workspace(&[
        (
            "src/mcp/handler.rs",
            "pub trait Handler { fn handle(&self); }",
        ),
        (
            "src/application/logging.rs",
            r#"
            use crate::mcp::handler::Handler;
            pub struct LoggingHandler;
            impl Handler for LoggingHandler { fn handle(&self) {} }
            "#,
        ),
        (
            "src/cli/handlers.rs",
            r#"
            use crate::mcp::handler::Handler;
            pub fn cmd_via_dyn_peer(h: &dyn Handler) { h.handle(); }
            "#,
        ),
    ]);
    let touchpoints = compute_touchpoints_for(
        &ws,
        &three_layer(),
        &cli_mcp_config(3),
        "cmd_via_dyn_peer",
        &empty_cfg_test(),
    );
    assert_set(touchpoints, &[]);
}

#[test]
fn touchpoints_reject_phantom_inherited_default_concrete_canonical() {
    // Adapter calls AppHandler::handle (UFCS) where the impl is
    // empty `impl Handler for AppHandler {}`. The fabricated
    // canonical `crate::application::logging::AppHandler::handle`
    // is added as an edge sink and `layer_of` caches `application`
    // for it, but no graph node exists with that body (the default
    // lives on the trait in ports). The walker must NOT treat the
    // phantom as a target boundary — otherwise Check A passes on a
    // touchpoint that B/D can't enumerate consistently.
    let ws = build_workspace(&[
        (
            "src/ports/handler.rs",
            "pub trait Handler { fn handle(&self) {} }",
        ),
        (
            "src/application/logging.rs",
            r#"
            use crate::ports::handler::Handler;
            pub struct AppHandler;
            impl Handler for AppHandler {}
            "#,
        ),
        (
            "src/cli/handlers.rs",
            r#"
            use crate::application::logging::AppHandler;
            pub fn cmd_log() { AppHandler::handle(&AppHandler); }
            "#,
        ),
    ]);
    let tps = compute_touchpoints_for(
        &ws,
        &ports_app_cli_mcp(),
        &cli_mcp_config(3),
        "cmd_log",
        &empty_cfg_test(),
    );
    let phantom = "crate::application::logging::AppHandler::handle";
    assert!(
        !tps.contains(phantom),
        "phantom inherited-default canonical must NOT be registered as touchpoint (no real fn node); got {tps:?}"
    );
}

#[test]
fn touchpoints_route_inherited_default_concrete_to_anchor() {
    // Target-layer trait with default body + inherited impl in target.
    // Adapter calls via UFCS on the concrete impl. The concrete
    // canonical is phantom (rejected by the walker), but the anchor
    // IS a valid target capability (default body lives in target).
    // The call-site emission must route this case to the anchor so
    // the capability surfaces in the adapter's touchpoint set.
    let ws = build_workspace(&[
        (
            "src/application/handler.rs",
            "pub trait Handler { fn handle(&self) {} }",
        ),
        (
            "src/application/logging.rs",
            r#"
            use crate::application::handler::Handler;
            pub struct AppHandler;
            impl Handler for AppHandler {}
            "#,
        ),
        (
            "src/cli/handlers.rs",
            r#"
            use crate::application::logging::AppHandler;
            pub fn cmd_log() { AppHandler::handle(&AppHandler); }
            "#,
        ),
    ]);
    let tps = compute_touchpoints_for(
        &ws,
        &ports_app_cli_mcp(),
        &cli_mcp_config(3),
        "cmd_log",
        &empty_cfg_test(),
    );
    let anchor = "crate::application::handler::Handler::handle";
    assert!(
        tps.contains(anchor),
        "inherited-default concrete call must route to the trait anchor when the anchor is a target capability; got {tps:?}"
    );
}

#[test]
fn touchpoints_skip_rewrite_when_multiple_traits_share_default_method_name() {
    // Ambiguity guard: if the impl-type implements two traits with the
    // SAME default method name, Rust requires UFCS disambiguation
    // (`<X as T1>::handle(&x)`). The phantom canonical alone doesn't
    // tell us which trait was selected, so the edge-rewrite pass must
    // NOT pick the first map-iteration match — that would make
    // touchpoints depend on HashMap iteration order. Conservative
    // choice: leave the canonical phantom (walker phantom-gate will
    // suppress it), so neither anchor is incorrectly registered.
    let ws = build_workspace(&[
        (
            "src/application/handler.rs",
            r#"
            pub trait Greeting { fn handle(&self) {} }
            pub trait Logging { fn handle(&self) {} }
            "#,
        ),
        (
            "src/application/logging.rs",
            r#"
            use crate::application::handler::{Greeting, Logging};
            pub struct AppHandler;
            impl Greeting for AppHandler {}
            impl Logging for AppHandler {}
            "#,
        ),
        (
            "src/cli/handlers.rs",
            r#"
            use crate::application::logging::AppHandler;
            pub fn cmd_log() { AppHandler::handle(&AppHandler); }
            "#,
        ),
    ]);
    let tps = compute_touchpoints_for(
        &ws,
        &ports_app_cli_mcp(),
        &cli_mcp_config(3),
        "cmd_log",
        &empty_cfg_test(),
    );
    let greeting_anchor = "crate::application::handler::Greeting::handle";
    let logging_anchor = "crate::application::handler::Logging::handle";
    assert!(
        !tps.contains(greeting_anchor) && !tps.contains(logging_anchor),
        "ambiguous inherited-default rewrite must leave the call unresolved (non-deterministic otherwise); got {tps:?}"
    );
}

#[test]
fn touchpoints_recognise_real_target_fn_node() {
    // Sanity check: a real `pub fn` in target IS a boundary. Gates
    // the round-6 phantom filter to the right side — we must not
    // accidentally drop legitimate concrete target fns.
    let ws = build_workspace(&[
        ("src/application/stats.rs", "pub fn get_stats() {}"),
        (
            "src/cli/handlers.rs",
            r#"
            use crate::application::stats::get_stats;
            pub fn cmd_stats() { get_stats(); }
            "#,
        ),
    ]);
    let tps = compute_touchpoints_for(
        &ws,
        &ports_app_cli_mcp(),
        &cli_mcp_config(3),
        "cmd_stats",
        &empty_cfg_test(),
    );
    let real = "crate::application::stats::get_stats";
    assert!(
        tps.contains(real),
        "real target-layer pub fn must be a touchpoint; got {tps:?}"
    );
    let ws = build_workspace(&[
        (
            "src/mcp/handler.rs",
            "pub trait Handler { fn handle(&self); }",
        ),
        (
            "src/application/logging.rs",
            r#"
            use crate::mcp::handler::Handler;
            pub struct LoggingHandler;
            impl Handler for LoggingHandler { fn handle(&self) {} }
            "#,
        ),
        (
            "src/cli/handlers.rs",
            r#"
            use crate::mcp::handler::Handler;
            pub fn cmd_via_dyn_peer(h: &dyn Handler) { h.handle(); }
            "#,
        ),
    ]);
    let touchpoints = compute_touchpoints_for(
        &ws,
        &three_layer(),
        &cli_mcp_config(3),
        "cmd_via_dyn_peer",
        &empty_cfg_test(),
    );
    assert_set(touchpoints, &[]);
}

// ── Peer-adapter blocking ────────────────────────────────────────

#[test]
fn touchpoints_do_not_traverse_peer_adapter() {
    // CLI handler `cmd_via_mcp` calls into an MCP handler `mcp_search`,
    // which itself crosses into the application layer. The CLI walk
    // must NOT inherit MCP's touchpoint — otherwise Check A/B/D would
    // be masked even though the CLI adapter never crossed into the
    // target itself.
    let ws = build_workspace(&[
        ("src/application/session.rs", "pub fn search() {}"),
        (
            "src/mcp/handlers.rs",
            r#"
            use crate::application::session::search;
            pub fn mcp_search() { search(); }
            "#,
        ),
        (
            "src/cli/handlers.rs",
            r#"
            use crate::mcp::handlers::mcp_search;
            pub fn cmd_via_mcp() { mcp_search(); }
            "#,
        ),
    ]);
    let touchpoints = compute_touchpoints_for(
        &ws,
        &three_layer(),
        &cli_mcp_config(3),
        "cmd_via_mcp",
        &empty_cfg_test(),
    );
    assert_set(touchpoints, &[]);
}