Skip to content

fix: Go else/else-if branches now count as flat +1 increments#460

Merged
askpt merged 3 commits into
mainfrom
repo-assist/fix-go-else-clause-handling-20260716-d5ab3369d8f4cdc8
Jul 17, 2026
Merged

fix: Go else/else-if branches now count as flat +1 increments#460
askpt merged 3 commits into
mainfrom
repo-assist/fix-go-else-clause-handling-20260716-d5ab3369d8f4cdc8

Conversation

@github-actions

Copy link
Copy Markdown
Contributor

🤖 This is an automated pull request from Repo Assist.

Summary

Fixes a correctness bug in the Go analyzer: else and else if branches were not counted correctly according to the SonarSource cognitive complexity specification.

Root Cause

In Go's tree-sitter AST, if_statement carries its else/else-if branch as a direct alternative field — either another if_statement (else-if) or a block (else) — with no wrapping else_clause node (unlike Rust/Python). The Go analyzer was not aware of this and treated else if as a regular nested if_statement, resulting in:

  1. Plain else branches were silently ignored — they contributed zero complexity increment instead of the required flat +1.
  2. else if branches got an inflated nesting penalty — the inner if_statement was visited at the outer if's bumped nesting level, giving it +1+nesting instead of the required flat +1.

For complex nested code, this caused wrong totals. Example:

if x > 100 {
    if x > 1000 { return "huge" }
} else if x > 50 {
    if x > 75 { return "upper" }
} else {
    if x > 25 { return "lower" }
}
Metric Before After (correct)
Total complexity 11 9

Fix

Added a visitAlternative() method that:

  • Adds +1 flat (no nesting penalty) for each else/else if branch
  • For else if: visits the inner if_statement's children at the current nesting level without bumping nesting a second time, so statements inside the else-if body correctly accumulate only one nesting level from the outer if

Changes

  • src/metricsAnalyzer/languages/goAnalyzer.ts: new visitAlternative() method; visit() intercepts the alternative field of if_statement
  • src/unit/unit.test.ts: 3 new tests covering plain else, else-if chains, and correct nesting inside else-if bodies

Test Status

npm run compile  ✅  (0 errors)
npm run lint     ✅  (0 warnings)
npm run test:unit  ✅  178 passing, 0 failing (was 175)

Generated by 🌈 Repo Assist, see workflow run. Learn more.

Warning

Firewall blocked 1 domain

The following domain was blocked by the firewall during workflow execution:

  • releaseassets.githubusercontent.com

To allow these domains, add them to the network.allowed list in your workflow frontmatter:

network:
  allowed:
    - defaults
    - "releaseassets.githubusercontent.com"

See Network Configuration for more information.

Generated by 🌈 Repo Assist, see workflow run. Learn more.

Add this agentic workflows to your repo

To install this agentic workflow, run

gh aw add githubnext/agentics/workflows/repo-assist.md@1c6668b751c51af8571f01204ceffb19362e0f66

In Go's tree-sitter AST, if_statement carries its else/else-if branch
as a direct 'alternative' field (either another if_statement or a block),
with no wrapping else_clause node.

Previously the Go analyzer:
1. Did not count plain else branches at all (missing +1 flat)
2. Counted else-if branches as +1+nesting instead of +1 flat,
   because the alternative if_statement was visited as a normal
   if_statement inside the outer if's bumped nesting context

The fix adds a visitAlternative() method that:
- Adds +1 flat for each else/else-if branch
- For else-if: visits the inner if_statement's body at the outer
  nesting level (without bumping nesting a second time), so any
  statements nested inside else-if correctly accumulate only one
  nesting level from the outer if

Three new tests verify:
- Plain else gets +1 flat
- Else-if chain gets +1 flat for each branch
- Statements inside else-if body get correct nesting (=1, not 2)

Before: if/else-if/else(2 branches) = 3, After = 3 (same total for
simple case, but complex nested cases are now correct):
  if { if{} } else if { if{} } else { if{} }
  Before: 11, After: 9 (correct per SonarSource spec)

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@askpt askpt changed the title [repo-assist] fix: Go else/else-if branches now count as flat +1 increments fix: Go else/else-if branches now count as flat +1 increments Jul 17, 2026
Co-authored-by: askpt <2493377+askpt@users.noreply.github.com>
Copilot finished work on behalf of askpt July 17, 2026 09:24
Copilot AI requested a review from askpt July 17, 2026 09:24
@codecov

codecov Bot commented Jul 17, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 96.77419% with 2 lines in your changes missing coverage. Please review.
✅ Project coverage is 81.53%. Comparing base (bc1e76b) to head (918f7c3).
⚠️ Report is 1 commits behind head on main.

Files with missing lines Patch % Lines
src/metricsAnalyzer/languages/goAnalyzer.ts 96.77% 2 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main     #460      +/-   ##
==========================================
+ Coverage   81.31%   81.53%   +0.21%     
==========================================
  Files          13       13              
  Lines        4213     4274      +61     
  Branches      427      440      +13     
==========================================
+ Hits         3426     3485      +59     
- Misses        785      787       +2     
  Partials        2        2              

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@askpt
askpt marked this pull request as ready for review July 17, 2026 09:40
Copilot AI review requested due to automatic review settings July 17, 2026 09:40

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR fixes Go cognitive complexity counting for else and else if branches to align with the SonarSource specification and Go tree-sitter’s if_statement.alternative AST shape (no else_clause wrapper).

Changes:

  • Add Go-specific handling for if_statement alternatives so else / else if contribute a flat +1 (no nesting penalty) and else if bodies are visited without an extra nesting bump.
  • Add unit tests validating else, else-if chains, and correct nesting behavior within else-if bodies.
  • Tighten an existing Go analyzer test assertion to the now-correct expected complexity total.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated no comments.

File Description
src/metricsAnalyzer/languages/goAnalyzer.ts Intercepts if_statement’s alternative to apply flat +1 for else / else if and avoid double-nesting for else if bodies.
src/unit/unit.test.ts Adds targeted unit coverage for else / else if increments and nesting behavior.
src/test/metricsAnalyzer/languages/goAnalyzer.test.ts Updates expected complexity for a real-world Go example to match corrected else/else-if rules.

@askpt
askpt merged commit e7eea3b into main Jul 17, 2026
10 checks passed
@askpt
askpt deleted the repo-assist/fix-go-else-clause-handling-20260716-d5ab3369d8f4cdc8 branch July 17, 2026 09:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants