rustqual 1.4.2

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
use crate::adapters::shared::cfg_test_files::collect_cfg_test_file_paths;

// A `(path, code)` workspace and the paths expected present/absent in the
// resulting cfg-test set.
type ClassifyCase = (
    &'static str,
    &'static [(&'static str, &'static str)],
    &'static [&'static str],
    &'static [&'static str],
);

#[test]
fn cfg_test_classification_propagates_and_honours_package_roots() {
    let cases: &[ClassifyCase] = &[
        // Propagation bug: a test-file's OWN sub-module must inherit cfg-test
        // status. Previously only tests/mod.rs was tagged; golden.rs was
        // treated as production and its helper falsely reported as dead code.
        (
            "cfg-test status propagates transitively through a mod chain",
            &[
                ("src/parent.rs", "#[cfg(test)]\nmod tests;"),
                ("src/parent/tests/mod.rs", "mod golden;"),
                (
                    "src/parent/tests/golden.rs",
                    "pub fn helper() -> u32 { 42 }",
                ),
            ],
            &["src/parent/tests/mod.rs", "src/parent/tests/golden.rs"],
            &[],
        ),
        // Edge case: a real package whose path contains an EARLIER `tests`
        // segment (`fixtures/tests/retry/`). The owner of its own `tests/` is
        // the package root; the outer `fixtures/tests/other.rs` (no package
        // root there) must still NOT be classified.
        (
            "a package nested under an outer `tests` segment classifies its own tests/",
            &[
                ("fixtures/tests/retry/src/lib.rs", "pub fn run() {}"),
                (
                    "fixtures/tests/retry/tests/integration.rs",
                    "#[test] fn it() {}",
                ),
                ("fixtures/tests/other.rs", "pub fn outer() {}"),
            ],
            &["fixtures/tests/retry/tests/integration.rs"],
            &["fixtures/tests/other.rs"],
        ),
    ];
    for (label, files, present, absent) in cases {
        let parsed: Vec<(String, String, syn::File)> = files
            .iter()
            .map(|(p, c)| (p.to_string(), c.to_string(), syn::parse_file(c).unwrap()))
            .collect();
        let result = collect_cfg_test_file_paths(&parsed);
        for p in *present {
            assert!(
                result.contains(*p),
                "case {label}: {p} must be cfg-test: {result:?}"
            );
        }
        for a in *absent {
            assert!(
                !result.contains(*a),
                "case {label}: {a} must NOT be cfg-test: {result:?}"
            );
        }
    }
}

#[test]
fn cfg_test_propagation_does_not_tag_unrelated_files() {
    // Negative case: a regular `mod foo;` in a production file must
    // not become cfg-test just because the fix now propagates through
    // cfg-test parents.
    let prod_code = r#"
        mod helpers;
    "#;
    let helpers_code = r#"
        pub fn util() {}
    "#;
    let parsed = vec![
        (
            "src/prod.rs".to_string(),
            prod_code.to_string(),
            syn::parse_file(prod_code).unwrap(),
        ),
        (
            "src/prod/helpers.rs".to_string(),
            helpers_code.to_string(),
            syn::parse_file(helpers_code).unwrap(),
        ),
    ];
    let result = collect_cfg_test_file_paths(&parsed);
    assert!(
        !result.contains("src/prod/helpers.rs"),
        "production sub-module must not be cfg-test: {result:?}"
    );
}

#[test]
fn per_crate_tests_dir_recognized_as_integration_test() {
    // Cargo compiles `<pkg_root>/tests/*.rs` as integration-test
    // binaries. In a workspace the package root is a sub-directory
    // (`crates/<name>/`), so the integration-test files live at
    // `crates/<name>/tests/*.rs` — not at the workspace-root `tests/`.
    // These must be recognised as test-only just like the top-level
    // layout, otherwise their test-attributed entry points are falsely
    // reported as dead code.
    let code = r#"
        #[tokio::test]
        async fn end_to_end() {}
    "#;
    let lib = "pub fn run() {}";
    let parsed = vec![
        (
            "crates/sv-utility-retry/src/lib.rs".to_string(),
            lib.to_string(),
            syn::parse_file(lib).unwrap(),
        ),
        (
            "crates/sv-utility-retry/tests/integration.rs".to_string(),
            code.to_string(),
            syn::parse_file(code).unwrap(),
        ),
    ];
    let result = collect_cfg_test_file_paths(&parsed);
    assert!(
        result.contains("crates/sv-utility-retry/tests/integration.rs"),
        "per-crate tests/ integration file must be cfg-test: {result:?}"
    );
}

#[test]
fn workspace_root_tests_dir_recognized_as_integration_test() {
    // The single-crate layout: integration tests at the analysis-root
    // `tests/` directory, with the crate's own `src/` present so the
    // root ("") qualifies as a package root.
    let code = "#[test] fn it_works() {}";
    let lib = "pub fn run() {}";
    let parsed = vec![
        (
            "src/lib.rs".to_string(),
            lib.to_string(),
            syn::parse_file(lib).unwrap(),
        ),
        (
            "tests/integration.rs".to_string(),
            code.to_string(),
            syn::parse_file(code).unwrap(),
        ),
    ];
    let result = collect_cfg_test_file_paths(&parsed);
    assert!(
        result.contains("tests/integration.rs"),
        "analysis-root tests/ integration file must be cfg-test: {result:?}"
    );
}

#[test]
fn tests_dir_only_classified_at_a_real_package_root() {
    // Review: a `tests/` directory counts as Cargo integration tests only
    // when its parent is a real package root — i.e. that directory also
    // holds a `src/` tree in the parsed file set. A `tests/` directory
    // anywhere else (fixtures, tooling, data) is production code and must
    // NOT enter the shared cfg-test set, or findings get hidden across
    // IOSP/SRP/architecture/DRY/TQ.
    let crate_lib = "pub fn run() {}";
    let it = "#[test] fn it() {}";
    let fixture = "pub fn support() {}";
    let tool = "pub fn helper() {}";
    let parsed = vec![
        (
            "crates/x/src/lib.rs".to_string(),
            crate_lib.to_string(),
            syn::parse_file(crate_lib).unwrap(),
        ),
        (
            "crates/x/tests/it.rs".to_string(),
            it.to_string(),
            syn::parse_file(it).unwrap(),
        ),
        (
            "fixtures/tests/support.rs".to_string(),
            fixture.to_string(),
            syn::parse_file(fixture).unwrap(),
        ),
        (
            "tools/shared/tests/helpers.rs".to_string(),
            tool.to_string(),
            syn::parse_file(tool).unwrap(),
        ),
    ];
    let result = collect_cfg_test_file_paths(&parsed);
    assert!(
        result.contains("crates/x/tests/it.rs"),
        "tests/ at a real package root (has sibling src/) must be cfg-test: {result:?}"
    );
    assert!(
        !result.contains("fixtures/tests/support.rs"),
        "tests/ with no sibling src/ must NOT be cfg-test: {result:?}"
    );
    assert!(
        !result.contains("tools/shared/tests/helpers.rs"),
        "nested non-package-root tests/ must NOT be cfg-test: {result:?}"
    );
}

#[test]
fn tests_dir_next_to_src_without_crate_root_file_not_classified() {
    // The package-root signal is Cargo's crate-root file (`src/lib.rs` or
    // `src/main.rs`), derived from the parsed set — NOT the mere presence
    // of a `src/` segment. A directory with a `src/` subtree but no
    // crate-root file is not a package, so its sibling `tests/` is
    // production code, not integration tests. Guards the case of a
    // deeply nested coincidental `src/`+`tests/` pair.
    let internal = "pub fn data() {}"; // under src/, but not a crate root
    let support = "pub fn support() {}";
    let parsed = vec![
        (
            "vendor/widget/src/internal.rs".to_string(),
            internal.to_string(),
            syn::parse_file(internal).unwrap(),
        ),
        (
            "vendor/widget/tests/support.rs".to_string(),
            support.to_string(),
            syn::parse_file(support).unwrap(),
        ),
    ];
    let result = collect_cfg_test_file_paths(&parsed);
    assert!(
        !result.contains("vendor/widget/tests/support.rs"),
        "tests/ next to a src/ without a crate-root file must NOT be cfg-test: {result:?}"
    );
}

#[test]
fn tests_dir_next_to_crate_root_file_classified() {
    // The flip side: a `src/lib.rs` (or `src/main.rs`) marks a real
    // package root, so its sibling `tests/` is integration tests.
    let lib = "pub fn run() {}";
    let it = "#[test] fn it() {}";
    let parsed = vec![
        (
            "vendor/widget/src/lib.rs".to_string(),
            lib.to_string(),
            syn::parse_file(lib).unwrap(),
        ),
        (
            "vendor/widget/tests/support.rs".to_string(),
            it.to_string(),
            syn::parse_file(it).unwrap(),
        ),
    ];
    let result = collect_cfg_test_file_paths(&parsed);
    assert!(
        result.contains("vendor/widget/tests/support.rs"),
        "tests/ next to a crate-root file must be cfg-test: {result:?}"
    );
}

#[test]
fn tests_dir_next_to_bin_only_crate_classified() {
    // A binary-only package (autobins in `src/bin/`, no `lib.rs`/`main.rs`)
    // is a real crate root, so its sibling `tests/` are integration tests.
    let bin = "fn main() {}";
    let it = "#[test] fn it() {}";
    let parsed = vec![
        (
            "crates/cli/src/bin/tool.rs".to_string(),
            bin.to_string(),
            syn::parse_file(bin).unwrap(),
        ),
        (
            "crates/cli/tests/it.rs".to_string(),
            it.to_string(),
            syn::parse_file(it).unwrap(),
        ),
    ];
    let result = collect_cfg_test_file_paths(&parsed);
    assert!(
        result.contains("crates/cli/tests/it.rs"),
        "tests/ next to a src/bin/ autobinary crate must be cfg-test: {result:?}"
    );
}

#[test]
fn is_integration_test_path_matches_package_root_tests_only() {
    use crate::adapters::shared::cfg_test_files::is_integration_test_path;
    use std::collections::HashSet;
    // Package roots derived from the file set: the analysis-root crate
    // (""), a workspace member, and a member whose name contains "src".
    let roots: HashSet<String> = ["", "crates/x", "crates/my-src-tool"]
        .iter()
        .map(|s| s.to_string())
        .collect();
    // `tests/` directly under a package root → integration test.
    assert!(is_integration_test_path("tests/it.rs", &roots));
    assert!(is_integration_test_path("crates/x/tests/it.rs", &roots));
    // A member whose name contains "src" is matched by its real root.
    assert!(is_integration_test_path(
        "crates/my-src-tool/tests/it.rs",
        &roots
    ));
    // `tests/` whose owning directory is not a package root → not an
    // integration test (fixtures, tooling, nested unit-test submodules).
    assert!(!is_integration_test_path(
        "fixtures/tests/support.rs",
        &roots
    ));
    assert!(!is_integration_test_path(
        "tools/shared/tests/helpers.rs",
        &roots
    ));
    assert!(!is_integration_test_path("src/foo/tests/bar.rs", &roots));
    assert!(!is_integration_test_path(
        "crates/x/src/foo/tests/bar.rs",
        &roots
    ));
    // No `tests/` segment at all.
    assert!(!is_integration_test_path("src/lib.rs", &roots));
}

#[test]
fn nested_src_tests_dir_not_blanket_classified_as_integration() {
    // Regression guard for the review finding: `contains("/tests/")` was
    // too broad and classified ANY nested `tests` directory as test-only.
    // A plain production file under `src/**/tests/` that is NOT reached
    // via `#[cfg(test)] mod` must not be auto-classified — otherwise real
    // findings get hidden across dimensions.
    let code = "pub fn connect() {}";
    let parsed = vec![(
        "src/database/tests/connection.rs".to_string(),
        code.to_string(),
        syn::parse_file(code).unwrap(),
    )];
    let result = collect_cfg_test_file_paths(&parsed);
    assert!(
        !result.contains("src/database/tests/connection.rs"),
        "production file under src/**/tests/ must not be auto cfg-test: {result:?}"
    );
}

#[test]
fn src_tests_dir_reached_via_cfg_test_mod_still_classified() {
    // The flip side: when a `src/**/tests/` file IS reached through a
    // `#[cfg(test)] mod tests;` chain it must still be cfg-test — that
    // path comes from the mod-chain detector, not the integration-path
    // heuristic, so tightening the heuristic must not regress it.
    let parent_code = r#"
        #[cfg(test)]
        mod tests;
    "#;
    let child_code = "pub fn helper() -> u32 { 42 }";
    let parsed = vec![
        (
            "src/database.rs".to_string(),
            parent_code.to_string(),
            syn::parse_file(parent_code).unwrap(),
        ),
        (
            "src/database/tests.rs".to_string(),
            child_code.to_string(),
            syn::parse_file(child_code).unwrap(),
        ),
    ];
    let result = collect_cfg_test_file_paths(&parsed);
    assert!(
        result.contains("src/database/tests.rs"),
        "src test file reached via #[cfg(test)] mod must still be cfg-test: {result:?}"
    );
}

#[test]
fn collect_cfg_test_file_paths_basic() {
    let parent_code = r#"
        #[cfg(test)]
        mod helpers;
    "#;
    let child_code = "pub fn h() {}";
    let parent_ast = syn::parse_file(parent_code).unwrap();
    let child_ast = syn::parse_file(child_code).unwrap();
    let parsed = vec![
        (
            "src/lib.rs".to_string(),
            parent_code.to_string(),
            parent_ast,
        ),
        (
            "src/helpers.rs".to_string(),
            child_code.to_string(),
            child_ast,
        ),
    ];
    let result = collect_cfg_test_file_paths(&parsed);
    assert!(
        result.contains("src/helpers.rs"),
        "Should detect src/helpers.rs as cfg-test file"
    );
}

// ── Companion-file detection ─────────────────────────────────────

#[test]
fn inner_cfg_test_file_attribute_marks_file_as_cfg_test() {
    // `#![cfg(test)]` as an inner attribute at the top of a file is
    // the Rust-level "this whole file is test-only" signal. rustqual
    // must recognize it on its own — without requiring a parent
    // `#[cfg(test)] mod foo;` elsewhere.
    let code = r#"
        #![cfg(test)]
        pub fn helper() -> u32 { 42 }
    "#;
    let parsed = vec![(
        "src/foo_tests.rs".to_string(),
        code.to_string(),
        syn::parse_file(code).unwrap(),
    )];
    let result = collect_cfg_test_file_paths(&parsed);
    assert!(
        result.contains("src/foo_tests.rs"),
        "inner `#![cfg(test)]` must tag the file as cfg-test: {result:?}"
    );
}

#[test]
fn path_attribute_on_cfg_test_mod_redirects_to_target_file() {
    // Companion-file pattern common in test-co-location:
    //
    //   // src/foo.rs (production):
    //   #[cfg(test)]
    //   #[path = "foo_tests.rs"]
    //   mod tests;
    //
    // The convention-based resolver would look for src/foo/tests.rs
    // or src/foo/tests/mod.rs — neither exists. The `#[path]` points
    // to the sibling file and must be honored.
    let parent_code = r#"
        #[cfg(test)]
        #[path = "foo_tests.rs"]
        mod tests;
    "#;
    let companion_code = r#"
        pub fn helper() -> u32 { 42 }
    "#;
    let parsed = vec![
        (
            "src/foo.rs".to_string(),
            parent_code.to_string(),
            syn::parse_file(parent_code).unwrap(),
        ),
        (
            "src/foo_tests.rs".to_string(),
            companion_code.to_string(),
            syn::parse_file(companion_code).unwrap(),
        ),
    ];
    let result = collect_cfg_test_file_paths(&parsed);
    assert!(
        result.contains("src/foo_tests.rs"),
        "`#[path]` on a cfg-test mod must redirect to the target file: {result:?}"
    );
}

#[test]
fn path_attribute_resolves_relative_to_parent_dir() {
    // `#[path]` is relative to the *directory containing the parent
    // file* — mimic the Rust compiler semantics.
    //
    //   src/ingest/code/rust.rs:
    //     #[cfg(test)] #[path = "rust_tests.rs"] mod tests;
    //
    //   src/ingest/code/rust_tests.rs  ← target
    let parent_code = r#"
        #[cfg(test)]
        #[path = "rust_tests.rs"]
        mod tests;
    "#;
    let companion_code = r#"
        pub fn helper() -> u32 { 42 }
    "#;
    let parsed = vec![
        (
            "src/ingest/code/rust.rs".to_string(),
            parent_code.to_string(),
            syn::parse_file(parent_code).unwrap(),
        ),
        (
            "src/ingest/code/rust_tests.rs".to_string(),
            companion_code.to_string(),
            syn::parse_file(companion_code).unwrap(),
        ),
    ];
    let result = collect_cfg_test_file_paths(&parsed);
    assert!(
        result.contains("src/ingest/code/rust_tests.rs"),
        "relative `#[path]` must resolve against the parent file's directory: {result:?}"
    );
}

#[test]
fn nested_file_under_src_bin_is_not_an_autobinary_crate_root() {
    // Only a file DIRECTLY in `src/bin/` is an autobinary crate root; a deeper
    // `src/bin/<sub>/<name>.rs` is just a module of that binary, not a package
    // root. So a sibling `tests/` next to such a nested file must NOT be
    // classified as an integration test. (Kills the `&&` → `||` mutant in
    // `bin_crate_root_owner`, which would accept the nested file as a root.)
    let parsed = vec![
        (
            "myapp/src/bin/sub/deep.rs".to_string(),
            "fn main() {}".to_string(),
            syn::parse_file("fn main() {}").unwrap(),
        ),
        (
            "myapp/tests/it.rs".to_string(),
            "#[test] fn it() {}".to_string(),
            syn::parse_file("#[test] fn it() {}").unwrap(),
        ),
    ];
    let result = collect_cfg_test_file_paths(&parsed);
    assert!(
        !result.contains("myapp/tests/it.rs"),
        "tests/ next to a NESTED src/bin file (not a real crate root) must not \
         be classified as integration: {result:?}"
    );
}

#[test]
fn inline_mod_does_not_propagate_cfg_test_to_same_named_external_file() {
    // `propagate_cfg_test_through_plain_mods` only follows EXTERNAL `mod foo;`
    // declarations. An INLINE `mod foo { … }` in a cfg-test file must NOT drag a
    // coincidentally same-named external file into the cfg-test set. (Kills the
    // `is_any_ext_mod(m)` → `true` match-guard mutant, which would resolve
    // inline mods too.)
    let lib = "#[cfg(test)]\nmod c;\npub fn run() {}";
    let c = "mod m { pub fn x() -> u32 { 1 } }"; // INLINE mod m
    let m = "pub fn y() -> u32 { 2 }"; // external file; only reachable as `c::m` if external
    let parsed = vec![
        (
            "pkg/src/lib.rs".to_string(),
            lib.to_string(),
            syn::parse_file(lib).unwrap(),
        ),
        (
            "pkg/src/c.rs".to_string(),
            c.to_string(),
            syn::parse_file(c).unwrap(),
        ),
        (
            "pkg/src/c/m.rs".to_string(),
            m.to_string(),
            syn::parse_file(m).unwrap(),
        ),
    ];
    let result = collect_cfg_test_file_paths(&parsed);
    assert!(
        result.contains("pkg/src/c.rs"),
        "c.rs is reached via `#[cfg(test)] mod c;` and must be cfg-test: {result:?}"
    );
    assert!(
        !result.contains("pkg/src/c/m.rs"),
        "an INLINE `mod m` must not propagate cfg-test to the external file \
         `c/m.rs`: {result:?}"
    );
}