omnicoach 0.3.0

Lightweight, high-signal static analysis and command linter for Python and Rust
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
//! Test-function, assertion, and macro analysis for Rust (`ra_ap_syntax`).

use super::attributes::attribute_terminal_name;
use super::bindings::collect_rust_fn_local_names;
use super::effects::expr_contains_call;
use super::names::{
    SELF_KEYWORD, SUPER_KEYWORD, macro_call_terminal_name, resolve_preceding_macro_path,
};
use super::tree::{meaningful_token_tree_elements, syntax_node};
use crate::code_lint::ast::call_clusters::split_at_redefinitions;
use crate::code_lint::ast::calls::{AstCallCandidate, collect_call_candidates};
use crate::code_lint::ast::test_analysis::{
    AssertionCheck, MultiPhaseTest, TestAssertionCount, TestFunctionSetup, TestLogicConstruct,
    TestStatementRole, TestToTestCall, has_multi_phase_interleaving, is_assertion_function_name,
    normalize_statement_text, resolve_assertion_helpers,
};
use crate::code_lint::ast::tree::{AstNode, ParsedFile, span_from_rowan_range};
use crate::diagnostic::SourceSpan;
use ra_ap_syntax::ast::{self, HasAttrs as _, HasModuleItem as _, HasName as _};
use ra_ap_syntax::{AstNode as _, SyntaxElement, SyntaxKind, SyntaxNode, SyntaxToken};
use std::collections::{HashMap, HashSet};

const TEST_IDENTIFIER: &str = "test";

/// Collects byte spans for all inline test items (`#[cfg(test)]` modules/items and `#[test]` functions)
/// within a Rust source file.
#[must_use]
pub fn collect_inline_test_ranges(file: &ParsedFile) -> &[std::ops::Range<usize>] {
    file.rust_inline_test_ranges.get_or_init(|| {
        let Some(parsed) = file.rs_parsed() else {
            return Vec::new();
        };
        let mut ranges = Vec::new();
        collect_inline_test_ranges_rec(parsed.tree().syntax(), &mut ranges);
        ranges
    })
}

fn collect_inline_test_ranges_rec(node: &SyntaxNode, ranges: &mut Vec<std::ops::Range<usize>>) {
    let has_test_attr = node
        .children()
        .filter_map(ast::Attr::cast)
        .any(|attribute| {
            attribute.kind() == ast::AttrKind::Outer
                && (is_conditional_test_attribute(&attribute) || is_test_attribute(&attribute))
        });
    if has_test_attr {
        let start: usize = node.text_range().start().into();
        let end: usize = node.text_range().end().into();
        ranges.push(start..end);
        return;
    }
    for child in node.children() {
        if child.kind() != SyntaxKind::TOKEN_TREE {
            collect_inline_test_ranges_rec(&child, ranges);
        }
    }
}

/// Returns true if `attribute` represents a `#[cfg(test)]` attribute.
#[must_use]
fn is_conditional_test_attribute(attribute: &ast::Attr) -> bool {
    let Some(ast::Meta::CfgMeta(conditional_meta)) = attribute.meta() else {
        return false;
    };
    let Some(ast::CfgPredicate::CfgAtom(atom)) = conditional_meta.cfg_predicate() else {
        return false;
    };
    atom.eq_token().is_none()
        && atom
            .ident_token()
            .is_some_and(|ident| ident.text() == TEST_IDENTIFIER)
}

/// Collects all outermost Rust test functions together with their identifier node, name, and assertion count.
#[must_use]
pub fn collect_test_function_assertion_counts(file: &ParsedFile) -> Vec<TestAssertionCount<'_>> {
    let Some(parsed) = file.rs_parsed() else {
        return Vec::new();
    };
    let tree = parsed.tree();
    let root = tree.syntax();
    let assertion_helpers = collect_rust_assertion_helpers(root);
    let mut test_functions = Vec::new();
    collect_outer_test_functions_rec(root, &mut test_functions);
    test_functions
        .into_iter()
        .filter_map(|function| {
            let name_node = function.name()?;
            let body_node = function.body()?;
            let name = name_node.text().to_string();
            let count = count_rust_assertions(body_node.syntax(), &assertion_helpers);
            Some(TestAssertionCount {
                name_node: AstNode::from_span(
                    file,
                    span_from_rowan_range(name_node.syntax().text_range()),
                ),
                name,
                count,
            })
        })
        .collect()
}

/// Collects control-flow and conditional constructs directly inside outermost Rust test functions.
#[must_use]
pub fn collect_test_logic_constructs(file: &ParsedFile) -> Vec<TestLogicConstruct<'_>> {
    let Some(parsed) = file.rs_parsed() else {
        return Vec::new();
    };
    let mut test_functions = Vec::new();
    collect_outer_test_functions_rec(parsed.tree().syntax(), &mut test_functions);
    let mut out = Vec::new();
    for function in test_functions {
        let Some(name_node) = function.name() else {
            continue;
        };
        let Some(body_node) = function.body() else {
            continue;
        };
        let func_name = name_node.text().to_string();
        collect_rust_test_logic_rec(body_node.syntax(), file, &func_name, &mut out);
    }
    out
}

fn collect_rust_test_logic_rec<'a>(
    node: &SyntaxNode,
    file: &'a ParsedFile,
    func_name: &str,
    out: &mut Vec<TestLogicConstruct<'a>>,
) {
    if (ast::Item::can_cast(node.kind()) && !ast::MacroCall::can_cast(node.kind()))
        || ast::ClosureExpr::can_cast(node.kind())
    {
        return;
    }
    let construct = match node.kind() {
        SyntaxKind::IF_EXPR => Some("an `if` expression"),
        SyntaxKind::MATCH_EXPR => Some("a `match` expression"),
        SyntaxKind::FOR_EXPR => Some("a `for` loop"),
        SyntaxKind::WHILE_EXPR => Some("a `while` loop"),
        SyntaxKind::LOOP_EXPR => Some("a `loop` expression"),
        _ => None,
    };
    if let Some(description) = construct {
        out.push(TestLogicConstruct {
            node: AstNode::from_span(file, span_from_rowan_range(node.text_range())),
            test_name: func_name.to_string(),
            construct: description,
        });
        return;
    }
    for child in node.children() {
        collect_rust_test_logic_rec(&child, file, func_name, out);
    }
}

/// Collects outermost Rust test functions that interleave non-assertion statements between
/// behavioral assertions.
#[must_use]
pub fn collect_multi_phase_test_functions(file: &ParsedFile) -> Vec<MultiPhaseTest<'_>> {
    let Some(parsed) = file.rs_parsed() else {
        return Vec::new();
    };
    let tree = parsed.tree();
    let root = tree.syntax();
    let assertion_helpers = collect_rust_assertion_helpers(root);
    let mut test_functions = Vec::new();
    collect_outer_test_functions_rec(root, &mut test_functions);
    test_functions
        .into_iter()
        .filter_map(|function| {
            let name_node = function.name()?;
            let body_node = function.body()?;
            if !is_multi_phase_rust_test(&body_node, &assertion_helpers) {
                return None;
            }
            Some(MultiPhaseTest {
                name_node: AstNode::from_span(
                    file,
                    span_from_rowan_range(name_node.syntax().text_range()),
                ),
                name: name_node.text().to_string(),
            })
        })
        .collect()
}

fn is_multi_phase_rust_test(
    body_node: &ast::BlockExpr,
    assertion_helpers: &HashSet<String>,
) -> bool {
    let Some(block_statements) = body_node.stmt_list() else {
        return false;
    };
    has_multi_phase_interleaving(rust_executable_block_statements(&block_statements).map(
        |statement_node| {
            if count_rust_assertions(&statement_node, assertion_helpers) > 0 {
                TestStatementRole::Assertion
            } else if is_rust_inspection_binding(&statement_node) {
                TestStatementRole::InspectionOrGuard
            } else {
                TestStatementRole::Action
            }
        },
    ))
}

fn is_rust_inspection_binding(statement_node: &SyntaxNode) -> bool {
    ast::LetStmt::cast(statement_node.clone())
        .and_then(|let_statement| let_statement.initializer())
        .is_some_and(|initializer| is_rust_projection_expr(&initializer))
}

fn is_rust_projection_expr(expr: &ast::Expr) -> bool {
    match expr {
        ast::Expr::PathExpr(_) => true,
        ast::Expr::FieldExpr(field) => field
            .expr()
            .is_some_and(|base| is_rust_projection_expr(&base)),
        ast::Expr::IndexExpr(index) => {
            index
                .base()
                .is_some_and(|base| is_rust_projection_expr(&base))
                && index
                    .index()
                    .is_some_and(|index_expr| !expr_contains_call(&index_expr))
        }
        ast::Expr::RefExpr(reference) => reference
            .expr()
            .is_some_and(|inner| is_rust_projection_expr(&inner)),
        ast::Expr::PrefixExpr(prefix) if prefix.op_kind() == Some(ast::UnaryOp::Deref) => prefix
            .expr()
            .is_some_and(|inner| is_rust_projection_expr(&inner)),
        ast::Expr::ParenExpr(paren) => paren
            .expr()
            .is_some_and(|inner| is_rust_projection_expr(&inner)),
        ast::Expr::TupleExpr(tuple) => {
            let mut has_fields = false;
            for field in tuple.fields() {
                has_fields = true;
                if !is_rust_projection_expr(&field) {
                    return false;
                }
            }
            has_fields
        }
        _ => false,
    }
}

/// Collects Rust test functions grouped by enclosing file root or `mod` scope (segmented on
/// duplicate function names) along with their leading non-assertion setup statements.
#[must_use]
pub fn collect_test_setup_scopes(file: &ParsedFile) -> Vec<Vec<TestFunctionSetup<'_>>> {
    let Some(parsed) = file.rs_parsed() else {
        return Vec::new();
    };
    let assertion_helpers = collect_rust_assertion_helpers(parsed.tree().syntax());
    let mut out = Vec::new();
    let root_items: Vec<ast::Item> = parsed.tree().items().collect();
    collect_rust_test_setup_scope(&root_items, file, &assertion_helpers, &mut out);
    out
}

fn collect_rust_test_setup_scope<'a>(
    items: &[ast::Item],
    file: &'a ParsedFile,
    assertion_helpers: &HashSet<String>,
    out: &mut Vec<Vec<TestFunctionSetup<'a>>>,
) {
    let mut setups = Vec::new();
    for item in items {
        let ast::Item::Fn(function) = item else {
            continue;
        };
        if !is_test_function(function) {
            continue;
        }
        let Some(name_node) = function.name() else {
            continue;
        };
        let Some(body_node) = function.body() else {
            continue;
        };
        setups.push(TestFunctionSetup {
            name_node: AstNode::from_span(
                file,
                span_from_rowan_range(name_node.syntax().text_range()),
            ),
            name: name_node.text().trim().to_owned(),
            leading_setup_statements: extract_rust_leading_setup_statements(
                &body_node,
                file,
                assertion_helpers,
            ),
        });
    }
    out.extend(split_at_redefinitions(setups, |setup| &setup.name));
    for item in items {
        if let ast::Item::Module(module) = item
            && let Some(body_items) = module.item_list()
        {
            let module_items: Vec<ast::Item> = body_items.items().collect();
            collect_rust_test_setup_scope(&module_items, file, assertion_helpers, out);
        }
    }
}

fn extract_rust_leading_setup_statements(
    body_node: &ast::BlockExpr,
    file: &ParsedFile,
    assertion_helpers: &HashSet<String>,
) -> Vec<String> {
    let Some(block_statements) = body_node.stmt_list() else {
        return Vec::new();
    };
    rust_executable_block_statements(&block_statements)
        .take_while(|statement_node| count_rust_assertions(statement_node, assertion_helpers) == 0)
        .map(|statement_node| {
            let span = span_from_rowan_range(statement_node.text_range());
            normalize_statement_text(&file.source[span.start..span.end])
        })
        .collect()
}

/// Collects all Rust assertion macro invocations in `file` with their condition-packing properties.
#[must_use]
pub fn collect_assertion_checks(file: &ParsedFile) -> Vec<AssertionCheck<'_>> {
    let Some(parsed) = file.rs_parsed() else {
        return Vec::new();
    };
    parsed
        .tree()
        .syntax()
        .descendants()
        .filter_map(ast::MacroCall::cast)
        .filter(is_assertion_macro)
        .map(|macro_call| {
            let callee = macro_call_terminal_name(&macro_call);
            let is_bool_assert = is_boolean_assertion_macro_name(&callee);
            let is_comparison_assert = is_comparison_assertion_macro_name(&callee);
            let argument_trees = if is_comparison_assert {
                macro_argument_token_trees(&macro_call)
            } else {
                Vec::new()
            };
            AssertionCheck {
                node: AstNode::from_span(
                    file,
                    span_from_rowan_range(macro_call.syntax().text_range()),
                ),
                has_top_level_logical_and: is_bool_assert && has_top_level_logical_and(&macro_call),
                has_boolean_literal_comparison: argument_trees
                    .iter()
                    .any(is_boolean_literal_collection),
                has_composite_collection_comparison: argument_trees
                    .iter()
                    .filter(|tree| is_multi_element_collection(tree))
                    .count()
                    >= 2,
                callee,
            }
        })
        .collect()
}

/// Returns true if a Rust `MacroCall`'s `token_tree` contains a top-level `&&` logical operator.
fn has_top_level_logical_and(macro_call: &ast::MacroCall) -> bool {
    let Some(token_tree) = macro_call.token_tree() else {
        return false;
    };
    if token_tree_has_direct_logical_and(&token_tree) {
        return true;
    }
    let meaningful = meaningful_token_tree_elements(&token_tree);
    matches!(
        meaningful.as_slice(),
        [SyntaxElement::Node(only_child)]
            if ast::TokenTree::cast(only_child.clone())
                .is_some_and(|inner| token_tree_has_direct_logical_and(&inner))
    )
}

/// Returns true if `token_tree` has a direct `&&` operator among its children.
fn token_tree_has_direct_logical_and(token_tree: &ast::TokenTree) -> bool {
    let elements: Vec<_> = token_tree.syntax().children_with_tokens().collect();
    elements.windows(2).any(|pair| {
        matches!(
            pair,
            [SyntaxElement::Token(first), SyntaxElement::Token(second)]
                if first.kind() == SyntaxKind::AMP && second.kind() == SyntaxKind::AMP
        )
    })
}

/// Extracts the child `TokenTree` arguments inside a Rust `MacroCall`'s `token_tree`.
fn macro_argument_token_trees(macro_call: &ast::MacroCall) -> Vec<ast::TokenTree> {
    let Some(token_tree) = macro_call.token_tree() else {
        return Vec::new();
    };
    meaningful_token_tree_elements(&token_tree)
        .into_iter()
        .filter_map(|element| element.into_node().and_then(ast::TokenTree::cast))
        .collect()
}

/// Returns true if `token_tree` is a standalone Rust macro argument `token_tree` (tuple, array, or
/// `vec![...]`) consisting solely of `>= 2` boolean literals (`true` / `false`).
fn is_boolean_literal_collection(token_tree: &ast::TokenTree) -> bool {
    if !is_standalone_collection_token_tree(token_tree) {
        return false;
    }
    let items = meaningful_token_tree_elements(token_tree);
    items.len() >= 2
        && items.iter().all(|item| {
            matches!(
                item,
                SyntaxElement::Token(token)
                    if matches!(token.kind(), SyntaxKind::TRUE_KW | SyntaxKind::FALSE_KW)
            )
        })
}

/// Returns true if `token_tree` is a standalone Rust tuple, array, or `vec![...]` macro argument
/// `token_tree` containing `>= 2` comma-separated elements (such as `(a, b)` or `[a, b]`).
fn is_multi_element_collection(token_tree: &ast::TokenTree) -> bool {
    if !is_standalone_collection_token_tree(token_tree) {
        return false;
    }
    let has_comma = token_tree
        .syntax()
        .children_with_tokens()
        .any(|element| matches!(element, SyntaxElement::Token(token) if token.kind() == SyntaxKind::COMMA));
    has_comma && meaningful_token_tree_elements(token_tree).len() >= 2
}

/// Returns true if `token_tree` is a standalone tuple, array, or `vec![...]` macro argument (not a
/// call argument list `foo(a, b)` or postfix receiver `(a, b).0`).
fn is_standalone_collection_token_tree(token_tree: &ast::TokenTree) -> bool {
    let Some(last_token) = token_tree.syntax().last_token() else {
        return false;
    };
    let Some(following) = std::iter::successors(last_token.next_token(), SyntaxToken::next_token)
        .find(|candidate| !candidate.kind().is_trivia())
    else {
        return false;
    };
    if !matches!(
        following.kind(),
        SyntaxKind::COMMA | SyntaxKind::R_PAREN | SyntaxKind::R_BRACK | SyntaxKind::R_CURLY
    ) {
        return false;
    }
    if resolve_preceding_macro_path(token_tree.syntax()).is_some_and(|path| path.terminal == "vec")
    {
        return true;
    }
    let Some(first_token) = token_tree.syntax().first_token() else {
        return false;
    };
    let Some(preceding) = std::iter::successors(first_token.prev_token(), SyntaxToken::prev_token)
        .find(|candidate| {
            !candidate.kind().is_trivia()
                && !matches!(candidate.kind(), SyntaxKind::AMP | SyntaxKind::MUT_KW)
        })
    else {
        return false;
    };
    matches!(
        preceding.kind(),
        SyntaxKind::L_PAREN | SyntaxKind::L_BRACK | SyntaxKind::L_CURLY | SyntaxKind::COMMA
    )
}

/// Collects calls in a Rust test file where a function calls a `#[test]` function defined in the
/// same file.
#[must_use]
pub fn collect_test_to_test_calls(file: &ParsedFile) -> Vec<TestToTestCall<'_>> {
    let Some(parsed) = file.rs_parsed() else {
        return Vec::new();
    };
    let mut test_functions: HashMap<SourceSpan, HashSet<String>> = HashMap::new();
    let mut function_locals: HashMap<SourceSpan, HashSet<String>> = HashMap::new();
    for fn_decl in parsed
        .tree()
        .syntax()
        .descendants()
        .filter_map(ast::Fn::cast)
    {
        let Some(name_node) = fn_decl.name() else {
            continue;
        };
        let name_span = span_from_rowan_range(name_node.syntax().text_range());
        function_locals.insert(name_span, collect_rust_fn_local_names(&fn_decl));
        if has_test_attribute(&fn_decl) {
            let module_span = enclosing_rust_module_span(fn_decl.syntax());
            test_functions
                .entry(module_span)
                .or_default()
                .insert(name_node.text().trim().to_owned());
        }
    }
    let mut out = Vec::new();
    for candidate in collect_call_candidates(file) {
        let Some(caller_fn) = candidate.enclosing_functions.first() else {
            continue;
        };
        let Some(LocalRustCallee {
            super_count,
            name: callee_name,
        }) = local_rust_callee(&candidate)
        else {
            continue;
        };
        let is_local = super_count == 0
            && candidate.enclosing_functions.iter().any(|func| {
                function_locals
                    .get(&func.name_span)
                    .is_some_and(|locals| locals.contains(callee_name))
            });
        let Some(target_module) = target_rust_module_span(&candidate.node, super_count) else {
            continue;
        };
        let is_test_target = test_functions
            .get(&target_module)
            .is_some_and(|names| names.contains(callee_name));
        if !is_test_target || is_local {
            continue;
        }
        if super_count == 0 && callee_name == caller_fn.name {
            continue;
        }
        out.push(TestToTestCall {
            node: candidate.node,
            caller: caller_fn.name.clone(),
            callee: candidate.callee,
        });
    }
    out
}

/// Relative module depth (`super::` count) and terminal function name of a local Rust call.
struct LocalRustCallee<'a> {
    super_count: usize,
    name: &'a str,
}

/// Extracts `LocalRustCallee` from a Rust call candidate when the call is not a method call and
/// any leading `::` path segments are relative module qualifiers (`self` or `super`).
fn local_rust_callee<'a>(candidate: &'a AstCallCandidate<'_>) -> Option<LocalRustCallee<'a>> {
    if candidate.method_name.is_some() {
        return None;
    }
    let mut segments = candidate.callee.rsplit("::");
    let terminal = segments.next()?.trim();
    let prefix: Vec<&str> = segments.map(str::trim).collect();
    if terminal.is_empty()
        || !prefix
            .iter()
            .all(|segment| matches!(*segment, SELF_KEYWORD | SUPER_KEYWORD))
    {
        return None;
    }
    let super_count = prefix
        .iter()
        .filter(|&&segment| segment == SUPER_KEYWORD)
        .count();
    Some(LocalRustCallee {
        super_count,
        name: terminal,
    })
}

/// Returns the byte span of the innermost `mod` enclosing `node`, or `0..0` at the file root.
fn enclosing_rust_module_span(node: &SyntaxNode) -> SourceSpan {
    node.ancestors()
        .find_map(ast::Module::cast)
        .map_or(SourceSpan::new(0, 0), |module| {
            span_from_rowan_range(module.syntax().text_range())
        })
}

/// Resolves the target `mod` span (`0..0` for the file root) reached by stepping up `super_count`
/// enclosing `mod` items from `node`.
fn target_rust_module_span(node: &AstNode<'_>, super_count: usize) -> Option<SourceSpan> {
    let syntax = syntax_node(node)?;
    let modules: Vec<ast::Module> = syntax.ancestors().filter_map(ast::Module::cast).collect();
    (super_count <= modules.len()).then(|| {
        modules
            .get(super_count)
            .map_or(SourceSpan::new(0, 0), |module| {
                span_from_rowan_range(module.syntax().text_range())
            })
    })
}

fn collect_outer_test_functions_rec(node: &SyntaxNode, out: &mut Vec<ast::Fn>) {
    if let Some(function) = ast::Fn::cast(node.clone()) {
        if is_test_function(&function) {
            out.push(function);
        }
        return;
    }
    for child in node.children() {
        collect_outer_test_functions_rec(&child, out);
    }
}

/// Collects names of non-test unit-returning free functions in `root` whose bodies execute at
/// least one behavioral assertion transitively.
fn collect_rust_assertion_helpers(root: &SyntaxNode) -> HashSet<String> {
    let candidates = collect_non_test_unit_rust_functions(root);
    resolve_assertion_helpers(
        &candidates,
        |(name, _)| name.as_str(),
        |(_, body), helpers| count_rust_assertions(body.syntax(), helpers) > 0,
    )
}

/// Collects `(name, body)` pairs for non-test, unit-returning free functions in `root`.
fn collect_non_test_unit_rust_functions(root: &SyntaxNode) -> Vec<(String, ast::BlockExpr)> {
    root.descendants()
        .filter_map(ast::Fn::cast)
        .filter(|function| {
            !is_test_function(function)
                && is_unit_rust_function(function)
                && !function
                    .syntax()
                    .ancestors()
                    .any(|ancestor| matches!(ancestor.kind(), SyntaxKind::TRAIT | SyntaxKind::IMPL))
        })
        .filter_map(|function| {
            let name = function.name()?.text().trim().to_owned();
            let body = function.body()?;
            Some((name, body))
        })
        .collect()
}

/// Returns true if `function` is a test function (`#[test]` / `#[rstest]` or named `test` / `test_*`).
#[must_use]
fn is_test_function(function: &ast::Fn) -> bool {
    let is_named_test = function.name().is_some_and(|name_node| {
        let function_name = name_node.text();
        function_name == TEST_IDENTIFIER || function_name.starts_with("test_")
    });
    is_named_test || has_test_attribute(function)
}

/// Returns true if `function` carries an outer test attribute (`#[test]`, `#[tokio::test]`,
/// `#[rstest]`, `#[test_case(...)]`).
#[must_use]
fn has_test_attribute(function: &ast::Fn) -> bool {
    function
        .attrs()
        .any(|attribute| attribute.kind() == ast::AttrKind::Outer && is_test_attribute(&attribute))
}

/// Returns true if `attribute` is a Rust test attribute (`#[test]`, `#[tokio::test]`, `#[rstest]`, `#[test_case(...)]`).
#[must_use]
fn is_test_attribute(attribute: &ast::Attr) -> bool {
    attribute_terminal_name(attribute)
        .is_some_and(|terminal| matches!(terminal.text(), TEST_IDENTIFIER | "rstest" | "test_case"))
}

/// Returns true if `function` has no return type annotation or an explicit `-> ()` unit return type.
fn is_unit_rust_function(function: &ast::Fn) -> bool {
    function.ret_type().is_none_or(|ret_type| {
        matches!(
            ret_type.ty(),
            Some(ast::Type::TupleType(tuple)) if tuple.fields().next().is_none()
        )
    })
}

/// Returns the executable top-level statements and tail expression of `block_statements`,
/// skipping local item declarations other than macro calls.
fn rust_executable_block_statements(
    block_statements: &ast::StmtList,
) -> impl Iterator<Item = SyntaxNode> {
    block_statements
        .statements()
        .filter(|statement| {
            !matches!(
                statement,
                ast::Stmt::Item(item) if !matches!(item, ast::Item::MacroCall(_))
            )
        })
        .map(|statement| statement.syntax().clone())
        .chain(
            block_statements
                .tail_expr()
                .map(|tail| tail.syntax().clone()),
        )
}

/// Recursively counts top-level assertion macro and assertion-helper invocations in a Rust test body.
fn count_rust_assertions(node: &SyntaxNode, assertion_helpers: &HashSet<String>) -> usize {
    if ast::Fn::can_cast(node.kind()) {
        return 0;
    }
    if let Some(macro_call) = ast::MacroCall::cast(node.clone())
        && is_assertion_macro(&macro_call)
    {
        return 1;
    }
    if is_rust_assertion_call_node(node, assertion_helpers) {
        return 1;
    }
    node.children()
        .map(|child| count_rust_assertions(&child, assertion_helpers))
        .sum()
}

/// Returns true if `node` is a Rust function or method call to an `assert_*` / `_assert_*` helper
/// or an unqualified call to a same-file assertion helper in `assertion_helpers`.
fn is_rust_assertion_call_node(node: &SyntaxNode, assertion_helpers: &HashSet<String>) -> bool {
    if let Some(call_expr) = ast::CallExpr::cast(node.clone())
        && let Some(ast::Expr::PathExpr(path_expr)) = call_expr.expr()
        && let Some(path) = path_expr.path()
        && let Some(segment) = path.segment()
        && let Some(name_ref) = segment.name_ref()
    {
        let name = name_ref.text().trim();
        return is_assertion_function_name(name)
            || (path.qualifier().is_none() && assertion_helpers.contains(name));
    }
    if let Some(method_call) = ast::MethodCallExpr::cast(node.clone())
        && let Some(name_ref) = method_call.name_ref()
    {
        return is_assertion_function_name(name_ref.text().trim());
    }
    false
}

/// Returns true if `macro_call` invokes an assertion macro (`assert!`, `assert_*!`, `debug_assert!`, `debug_assert_*!`).
#[must_use]
fn is_assertion_macro(macro_call: &ast::MacroCall) -> bool {
    let terminal = macro_call_terminal_name(macro_call);
    is_boolean_assertion_macro_name(&terminal) || is_comparison_assertion_macro_name(&terminal)
}

#[must_use]
fn is_boolean_assertion_macro_name(name: &str) -> bool {
    matches!(name, "assert" | "debug_assert")
}

#[must_use]
fn is_comparison_assertion_macro_name(name: &str) -> bool {
    name.starts_with("assert_") || name.starts_with("debug_assert_")
}