rustqual 1.2.4

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
//! Tests for Check A (adapter-must-delegate).
//!
//! Each test sets up a small multi-file workspace via `build_workspace`,
//! compiles layers + call-parity config, and asserts findings emitted
//! by `check_no_delegation`.
//!
//! Suppression via `// qual:allow(architecture)` is covered by the
//! golden-example integration test in Task 5 — it piggy-backs on the
//! existing `mark_architecture_suppressions` pipeline and doesn't need
//! a separate unit test here.

use super::support::{
    build_workspace, cli_mcp_config, empty_cfg_test, run_check_a, three_layer, Workspace,
};
use crate::adapters::analyzers::architecture::{MatchLocation, ViolationKind};

fn assert_no_delegation_fn_names(findings: &[MatchLocation]) -> Vec<String> {
    findings
        .iter()
        .filter_map(|f| match &f.kind {
            ViolationKind::CallParityNoDelegation { fn_name, .. } => Some(fn_name.clone()),
            _ => None,
        })
        .collect()
}

// ── Basic direct / inline cases ───────────────────────────────

#[test]
fn test_adapter_fn_direct_delegation_passes() {
    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 layers = three_layer();
    let cp = cli_mcp_config(3);
    let findings = run_check_a(&ws, &layers, &cp, &empty_cfg_test());
    assert!(
        assert_no_delegation_fn_names(&findings).is_empty(),
        "direct delegation should pass, got {findings:?}"
    );
}

#[test]
fn test_adapter_fn_inline_impl_fails() {
    let ws = build_workspace(&[
        ("src/application/stats.rs", "pub fn get_stats() {}"),
        (
            "src/cli/handlers.rs",
            r#"
            pub fn cmd_stats() {
                let _ = 42;
            }
            "#,
        ),
    ]);
    let findings = run_check_a(&ws, &three_layer(), &cli_mcp_config(3), &empty_cfg_test());
    let names = assert_no_delegation_fn_names(&findings);
    assert!(
        names.contains(&"cmd_stats".to_string()),
        "inline fn should be flagged, got {names:?}"
    );
}

#[test]
fn test_adapter_fn_transitive_delegation_via_helper_passes() {
    let ws = build_workspace(&[
        ("src/application/stats.rs", "pub fn get_stats() {}"),
        (
            "src/cli/helpers.rs",
            r#"
            use crate::application::stats::get_stats;
            pub fn prepare() {
                get_stats();
            }
            "#,
        ),
        (
            "src/cli/handlers.rs",
            r#"
            use crate::cli::helpers::prepare;
            pub fn cmd_stats() {
                prepare();
            }
            "#,
        ),
    ]);
    let findings = run_check_a(&ws, &three_layer(), &cli_mcp_config(3), &empty_cfg_test());
    let names = assert_no_delegation_fn_names(&findings);
    assert!(
        !names.contains(&"cmd_stats".to_string()),
        "transitive delegation at depth 2 should pass, got {names:?}"
    );
}

#[test]
fn test_adapter_fn_transitive_depth_exceeds_limit_fails() {
    // Chain: cmd_stats → h1 → h2 → h3 → h4 → get_stats (5 hops).
    // With call_depth=3 we only explore 3 edges deep → target not reached.
    let ws = build_workspace(&[
        ("src/application/stats.rs", "pub fn get_stats() {}"),
        (
            "src/cli/helpers.rs",
            r#"
            use crate::application::stats::get_stats;
            pub fn h4() { get_stats(); }
            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_stats() { h1(); }
            "#,
        ),
    ]);
    let findings = run_check_a(&ws, &three_layer(), &cli_mcp_config(3), &empty_cfg_test());
    let names = assert_no_delegation_fn_names(&findings);
    assert!(
        names.contains(&"cmd_stats".to_string()),
        "depth-exceeding chain should flag cmd_stats, got {names:?}"
    );
}

#[test]
fn test_call_depth_1_only_direct_calls() {
    // cmd_stats calls helper() which calls get_stats.
    // call_depth=1: only direct calls count → helper is not in target → fail.
    let ws = build_workspace(&[
        ("src/application/stats.rs", "pub fn get_stats() {}"),
        (
            "src/cli/helpers.rs",
            r#"
            use crate::application::stats::get_stats;
            pub fn helper() { get_stats(); }
            "#,
        ),
        (
            "src/cli/handlers.rs",
            r#"
            use crate::cli::helpers::helper;
            pub fn cmd_stats() { helper(); }
            "#,
        ),
    ]);
    let findings = run_check_a(&ws, &three_layer(), &cli_mcp_config(1), &empty_cfg_test());
    let names = assert_no_delegation_fn_names(&findings);
    assert!(
        names.contains(&"cmd_stats".to_string()),
        "call_depth=1 should flag cmd_stats (helper is not target), got {names:?}"
    );
}

#[test]
fn test_adapter_fn_method_call_does_not_count() {
    // Adapter calls `disp.run(x)` — method call on unknown type, stays
    // `<method>:run` = layer-unknown = no delegation.
    let ws = build_workspace(&[
        ("src/application/dispatch.rs", "pub fn run_it() {}"),
        (
            "src/cli/handlers.rs",
            r#"
            pub fn cmd_stats(disp: UnknownType) {
                disp.run();
            }
            "#,
        ),
    ]);
    let findings = run_check_a(&ws, &three_layer(), &cli_mcp_config(3), &empty_cfg_test());
    let names = assert_no_delegation_fn_names(&findings);
    assert!(
        names.contains(&"cmd_stats".to_string()),
        "method call on unknown type must not count as delegation"
    );
}

#[test]
fn test_adapter_fn_cross_adapter_call_does_not_count() {
    // CLI calls an MCP fn (peer, not target) → no delegation credit.
    let ws = build_workspace(&[
        ("src/application/stats.rs", "pub fn get_stats() {}"),
        (
            "src/mcp/handlers.rs",
            r#"
            use crate::application::stats::get_stats;
            pub fn handle_stats() { get_stats(); }
            "#,
        ),
        (
            "src/cli/handlers.rs",
            r#"
            use crate::mcp::handlers::handle_stats;
            pub fn cmd_stats() { handle_stats(); }
            "#,
        ),
    ]);
    let findings = run_check_a(&ws, &three_layer(), &cli_mcp_config(1), &empty_cfg_test());
    // At depth 1, cmd_stats only reaches handle_stats (in mcp, not app). → fail.
    let names = assert_no_delegation_fn_names(&findings);
    assert!(
        names.contains(&"cmd_stats".to_string()),
        "cross-adapter call at depth 1 must not count, got {names:?}"
    );
}

#[test]
fn test_adapter_fn_cross_adapter_call_blocked_even_at_deeper_depth() {
    // Even at greater call_depth the CLI walk must not inherit MCP's
    // application touchpoints. CLI never crosses into the target
    // layer itself — cmd_stats must therefore still flag NoDelegation.
    let ws = build_workspace(&[
        ("src/application/stats.rs", "pub fn get_stats() {}"),
        (
            "src/mcp/handlers.rs",
            r#"
            use crate::application::stats::get_stats;
            pub fn handle_stats() { get_stats(); }
            "#,
        ),
        (
            "src/cli/handlers.rs",
            r#"
            use crate::mcp::handlers::handle_stats;
            pub fn cmd_stats() { handle_stats(); }
            "#,
        ),
    ]);
    let findings = run_check_a(&ws, &three_layer(), &cli_mcp_config(2), &empty_cfg_test());
    let names = assert_no_delegation_fn_names(&findings);
    assert!(
        names.contains(&"cmd_stats".to_string()),
        "peer-adapter walks must not inherit touchpoints; expected cmd_stats in {names:?}"
    );
}

#[test]
fn test_adapter_fn_cfg_test_file_skipped() {
    let ws = build_workspace(&[
        ("src/application/stats.rs", "pub fn get_stats() {}"),
        (
            "src/cli/handlers.rs",
            r#"
            pub fn cmd_stats() {
                let _ = 42;
            }
            "#,
        ),
    ]);
    let mut cfg_test = std::collections::HashSet::new();
    cfg_test.insert("src/cli/handlers.rs".to_string());
    let findings = run_check_a(&ws, &three_layer(), &cli_mcp_config(3), &cfg_test);
    assert!(
        findings.is_empty(),
        "cfg-test adapter file must not produce findings, got {findings:?}"
    );
}

#[test]
fn test_adapter_fn_not_in_any_adapter_layer_ignored() {
    // Fn in a layer NOT listed as an adapter → not checked.
    let ws = build_workspace(&[
        ("src/application/stats.rs", "pub fn get_stats() {}"),
        (
            "src/application/api.rs",
            r#"
            pub fn internal_api() {
                let _ = 42;
            }
            "#,
        ),
    ]);
    let findings = run_check_a(&ws, &three_layer(), &cli_mcp_config(3), &empty_cfg_test());
    assert!(
        findings.is_empty(),
        "non-adapter-layer fn must not be checked"
    );
}

#[test]
fn test_finding_line_is_fn_sig_line() {
    let src = "\n\n\npub fn cmd_stats() { let _ = 42; }\n";
    let ws = build_workspace(&[
        ("src/application/stats.rs", "pub fn get_stats() {}"),
        ("src/cli/handlers.rs", src),
    ]);
    let findings = run_check_a(&ws, &three_layer(), &cli_mcp_config(3), &empty_cfg_test());
    let finding = findings
        .iter()
        .find(|f| matches!(f.kind, ViolationKind::CallParityNoDelegation { .. }))
        .expect("expected a CallParityNoDelegation finding");
    // `pub fn cmd_stats` is on line 4 (1-indexed) given 3 leading newlines.
    assert_eq!(
        finding.line, 4,
        "line must anchor on fn sig, got {finding:?}"
    );
    assert_eq!(finding.file, "src/cli/handlers.rs");
}

#[test]
fn test_unparseable_impl_self_type_does_not_collapse_with_free_fns() {
    // Regression: `impl Trait for &dyn Something { fn search() }`'s
    // self-type can't be canonicalised. Previously it was pushed as
    // `Vec::new()`, which made `search` canonicalise to
    // `crate::<file>::search` — colliding with a same-named free fn
    // in the same file and silently polluting the graph. The skip
    // must leave the free fn's node intact.
    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 search() { get_stats(); }
            impl dyn std::fmt::Debug {
                // Not a real impl this analyser understands — self-type
                // isn't a plain path, so every method inside must be
                // skipped, not recorded as `crate::cli::handlers::*`.
                pub fn search(&self) {}
            }
            pub fn cmd_x() { search(); }
            "#,
        ),
    ]);
    let findings = run_check_a(&ws, &three_layer(), &cli_mcp_config(3), &empty_cfg_test());
    let names = assert_no_delegation_fn_names(&findings);
    assert!(
        !names.contains(&"cmd_x".to_string()),
        "free-fn `search` must remain the node `cmd_x` reaches via delegation, got {names:?}"
    );
}

#[test]
fn test_convergent_graph_does_not_double_enqueue() {
    // Regression guard: if `WalkState::enqueue_unvisited` only checks
    // visited at dequeue (not enqueue), a convergent graph can queue
    // the same node many times. Here 3 helpers all fan out to both
    // `app::a` and `app::b`, and the same callees reach `app::common`.
    // The walk must still terminate (and delegation must resolve)
    // without blowing up the queue.
    let ws = build_workspace(&[
        (
            "src/application/common.rs",
            r#"
            pub fn common() {}
            pub fn a() { common(); }
            pub fn b() { common(); }
            "#,
        ),
        (
            "src/cli/helpers.rs",
            r#"
            use crate::application::common::{a, b};
            pub fn h1() { a(); b(); }
            pub fn h2() { a(); b(); }
            pub fn h3() { a(); b(); }
            "#,
        ),
        (
            "src/cli/handlers.rs",
            r#"
            use crate::cli::helpers::{h1, h2, h3};
            pub fn cmd_x() { h1(); h2(); h3(); }
            "#,
        ),
    ]);
    let findings = run_check_a(&ws, &three_layer(), &cli_mcp_config(3), &empty_cfg_test());
    let names = assert_no_delegation_fn_names(&findings);
    assert!(
        !names.contains(&"cmd_x".to_string()),
        "convergent delegation must still resolve, got {names:?}"
    );
}

// ── Deprecated-handler exclusion (v1.2.1) ─────────────────────

#[test]
fn check_a_skips_deprecated_handler() {
    // A deprecated adapter pub-fn that doesn't delegate would normally
    // fire Check A. Marked `#[deprecated]`, it must be skipped — alias
    // being phased out shouldn't drag the parity report.
    let ws = build_workspace(&[
        ("src/application/stats.rs", "pub fn get_stats() {}"),
        (
            "src/cli/handlers.rs",
            r#"
            #[deprecated]
            pub fn cmd_old() { let _ = 42; }
            "#,
        ),
    ]);
    let findings = run_check_a(&ws, &three_layer(), &cli_mcp_config(3), &empty_cfg_test());
    let names = assert_no_delegation_fn_names(&findings);
    assert!(
        !names.contains(&"cmd_old".to_string()),
        "deprecated handler should be excluded from Check A, got {names:?}"
    );
}