(
node: Node<'_>,
context: &mut Context<'_>,
issues: &mut Vec<StructuralIssue>,
)
| 1 | use super::*; |
| 2 | |
| 3 | pub(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 | { |
nothing calls this directly
no test coverage detected