fix: Go else/else-if branches now count as flat +1 increments#460
Merged
askpt merged 3 commits intoJul 17, 2026
Merged
Conversation
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>
6 tasks
…0716-d5ab3369d8f4cdc8
Co-authored-by: askpt <2493377+askpt@users.noreply.github.com>
Codecov Report❌ Patch coverage is
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. 🚀 New features to boost your workflow:
|
Contributor
There was a problem hiding this comment.
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_statementalternatives soelse/else ifcontribute a flat+1(no nesting penalty) andelse ifbodies are visited without an extra nesting bump. - Add unit tests validating
else,else-ifchains, and correct nesting behavior withinelse-ifbodies. - 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
approved these changes
Jul 17, 2026
askpt
deleted the
repo-assist/fix-go-else-clause-handling-20260716-d5ab3369d8f4cdc8
branch
July 17, 2026 09:48
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
🤖 This is an automated pull request from Repo Assist.
Summary
Fixes a correctness bug in the Go analyzer:
elseandelse ifbranches were not counted correctly according to the SonarSource cognitive complexity specification.Root Cause
In Go's tree-sitter AST,
if_statementcarries its else/else-if branch as a directalternativefield — either anotherif_statement(else-if) or ablock(else) — with no wrappingelse_clausenode (unlike Rust/Python). The Go analyzer was not aware of this and treatedelse ifas a regular nestedif_statement, resulting in:elsebranches were silently ignored — they contributed zero complexity increment instead of the required flat +1.else ifbranches got an inflated nesting penalty — the innerif_statementwas visited at the outerif's bumped nesting level, giving it +1+nesting instead of the required flat +1.For complex nested code, this caused wrong totals. Example:
Fix
Added a
visitAlternative()method that:else/else ifbranchelse if: visits the innerif_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 outerifChanges
src/metricsAnalyzer/languages/goAnalyzer.ts: newvisitAlternative()method;visit()intercepts the alternative field ofif_statementsrc/unit/unit.test.ts: 3 new tests covering plain else, else-if chains, and correct nesting inside else-if bodiesTest Status
Warning
Firewall blocked 1 domain
The following domain was blocked by the firewall during workflow execution:
releaseassets.githubusercontent.comSee Network Configuration for more information.
Add this agentic workflows to your repo
To install this agentic workflow, run