MCPcopy Create free account
hub / github.com/atomicdotdev/atomic / inspect

Function inspect

atomic-audit/src/substantive/branching.rs:3–99  ·  view source on GitHub ↗
(
    node: Node<'_>,
    context: &mut Context<'_>,
    issues: &mut Vec<StructuralIssue>,
)

Source from the content-addressed store, hash-verified

1use super::*;
2
3pub(super) fn inspect(
4 node: Node<'_>,
5 context: &mut Context<'_>,
6 issues: &mut Vec<StructuralIssue>,
7) -> Result<(), &'static str> {
8 let rust = context.language == Language::Rust;
9 if (rust && node.kind() == "if_expression") || (!rust && node.kind() == "if_statement") {
10 if let (Some(condition), Some(left), Some(right)) = (
11 node.child_by_field_name("condition"),
12 node.child_by_field_name("consequence"),
13 node.child_by_field_name("alternative"),
14 ) {
15 let right = if right.kind() == "else_clause" {
16 code_children(right).first().copied()
17 } else {
18 Some(right)
19 };
20 if let Some(right) = right {
21 // `if let` can bind a name differently from the else scope.
22 // Identical spelling would not establish identical references.
23 let binds_names = rust
24 && context
25 .normalized(condition)?
26 .iter()
27 .any(|token| token.kind == "let");
28 if !binds_names
29 && matches!(right.kind(), "block" | "statement_block")
30 && !code_children(left).is_empty()
31 && context.normalized(left)? == context.normalized(right)?
32 {
33 issues.push(context.issue(Rule::DuplicatedBehavior, node, format!("The nonempty alternatives at lines {} and {} have identical syntax tokens (comments excluded, literal contents preserved). The conditional does not select different branch code; check for a copied or mistaken body. Condition effects still matter.", left.start_position().row + 1, right.start_position().row + 1)));
34 }
35 }
36 }
37 if rust {
38 if let (Some(condition), Some(left)) = (
39 node.child_by_field_name("condition"),
40 node.child_by_field_name("consequence"),
41 ) {
42 if condition.kind() == "let_condition" {
43 if let Some(pattern) = condition.child_by_field_name("pattern") {
44 if error_pattern(pattern, context)? && success(tail(left), context)? {
45 issues.push(context.issue(Rule::FailureMasking, node, "An Err-pattern branch ends by constructing Ok(...). The observed error is converted into a successful result; verify that callers explicitly expect degraded success rather than failure propagation."));
46 }
47 }
48 }
49 }
50 }
51 }
52 if rust && node.kind() == "match_arm" {
53 if let (Some(pattern), Some(value)) = (
54 node.child_by_field_name("pattern"),
55 node.child_by_field_name("value"),
56 ) {
57 if error_pattern(pattern, context)?
58 && success(tail(value), context)?
59 && !optional_not_found(pattern, tail(value), context)?
60 {

Callers

nothing calls this directly

Calls 15

code_childrenFunction · 0.85
error_patternFunction · 0.85
tailFunction · 0.85
optional_not_foundFunction · 0.85
issueMethod · 0.80
lastMethod · 0.80
compactMethod · 0.80
successFunction · 0.70
textFunction · 0.50
childrenFunction · 0.50
kindMethod · 0.45
iterMethod · 0.45

Tested by

no test coverage detected