Skip to content

fix: correct Go logical operator cognitive complexity (flat +1, not nesting-penalized) - #495

Merged
askpt merged 2 commits into
mainfrom
repo-assist/test-nesting-boolean-ops-20260802-c80d19f4ca35f3e9
Aug 2, 2026
Merged

fix: correct Go logical operator cognitive complexity (flat +1, not nesting-penalized)#495
askpt merged 2 commits into
mainfrom
repo-assist/test-nesting-boolean-ops-20260802-c80d19f4ca35f3e9

Conversation

@github-actions

@github-actions github-actions Bot commented Aug 2, 2026

Copy link
Copy Markdown
Contributor

🤖 This pull request was created by Repo Assist, an automated AI assistant.

Summary

Fixes a bug in the Go cognitive complexity analyzer where binary logical operators (&& and ||) were incorrectly receiving a nesting penalty, making them inconsistent with all other language analyzers and the SonarSource cognitive complexity specification.

Root Cause

The Go analyzer's visit() method applied baseIncrement + this.nesting to all nodes uniformly. However, per the SonarSource specification, only structural control flow constructs (if, for, switch, select) carry a nesting penalty. Binary logical operators should always contribute a flat +1 regardless of nesting depth.

This caused incorrect complexity scores: && inside a nested if was counting as +2 instead of +1, inflating function complexity.

Fix

  • getComplexityIncrement() now returns the full increment directly:
    • Structural nodes (if_statement, for_statement, expression_switch_statement, type_switch_statement, select_statement): 1 + this.nesting
    • All other nodes (binary_expression, func_literal, break_statement, continue_statement, goto_statement, call_expression for recover): flat 1
  • visit() uses the returned value directly, without adding this.nesting

This aligns the Go analyzer with C#, Java, Rust, Python, and TypeScript which all correctly apply flat increments to logical operators.

Impact

Functions with logical operators inside nested control flow will report lower (correct) complexity. For example:

func foo(a, b bool) bool {
    if a {         // +1
        return a && b  // +1 flat (was +2, now corrected)
    }
    return false
}
// Old: complexity = 3, New (correct): complexity = 2

Test Status

npm run compile  ✅  (0 errors)
npm run lint     ✅  (0 warnings)
npm run test:unit  ✅  201 passing (was 199, added 2 regression tests), 0 failing

Added two regression tests:

  1. Go: logical operators inside nested if are flat (+1 each, no nesting penalty)
  2. Go: logical operators inside doubly-nested ifs are still flat +1

Closes #none (standalone bug fix)

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.
Comment /repo-assist to run again

Add this agentic workflow to your repo

To install this agentic workflow, run

gh aw add githubnext/agentics/workflows/repo-assist.md@42c2ab5b4e4c9273534c39259b2e0df7f20f07e9

…esting-penalized)

Binary logical operators (&& and ||) in Go were incorrectly
receiving a nesting penalty. Per the SonarSource cognitive complexity
specification, logical operators contribute a flat +1 regardless of
nesting depth. Only structural control-flow constructs (if, for,
switch, select) carry the nesting penalty.

This fixes the Go analyzer's visit() method to use the increment
returned directly from getComplexityIncrement() rather than adding
this.nesting, and updates getComplexityIncrement() to return the full
structural increment (1 + nesting) for control-flow nodes and flat +1
for all other nodes (logical operators, recover, goto, break/continue).

Adds two regression tests confirming && inside nested if blocks
counts as flat +1, consistent with Java, C#, Rust, and Python.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@askpt askpt changed the title [repo-assist] fix: correct Go logical operator cognitive complexity (flat +1, not nesting-penalized) fix: correct Go logical operator cognitive complexity (flat +1, not nesting-penalized) Aug 2, 2026
…nalyzer (#497)

Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
Co-authored-by: askpt <2493377+askpt@users.noreply.github.com>
@codecov

codecov Bot commented Aug 2, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 95.65217% with 1 line in your changes missing coverage. Please review.
✅ Project coverage is 81.37%. Comparing base (f5b5ede) to head (ab28f91).

Files with missing lines Patch % Lines
src/metricsAnalyzer/languages/goAnalyzer.ts 95.65% 1 Missing ⚠️
Additional details and impacted files
@@           Coverage Diff           @@
##             main     #495   +/-   ##
=======================================
  Coverage   81.37%   81.37%           
=======================================
  Files          13       13           
  Lines        4366     4366           
  Branches      442      442           
=======================================
  Hits         3553     3553           
  Misses        812      812           
  Partials        1        1           

☔ 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 August 2, 2026 17:14
@askpt
askpt self-requested a review as a code owner August 2, 2026 17:14
Copilot AI review requested due to automatic review settings August 2, 2026 17:14

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 scoring so logical operators (&&, ||) contribute a flat +1 (deduplicated per same-operator chain) rather than incorrectly inheriting the current nesting level. This brings the Go analyzer’s behavior in line with the intended “structural constructs get nesting penalties; logical operators do not” model used elsewhere in the extension’s analyzers.

Changes:

  • Updated the Go analyzer so visit() no longer blindly adds this.nesting to every increment; nesting penalties are applied only where getComplexityIncrement() explicitly includes them.
  • Changed Go logical-operator increments to be flat +1 (no nesting penalty), while preserving chain deduping.
  • Updated and added regression tests to ensure nested logical operators remain flat, and adjusted expected complexity totals accordingly.

Reviewed changes

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

File Description
src/metricsAnalyzer/languages/goAnalyzer.ts Moves nesting responsibility into getComplexityIncrement() and makes &&/`
src/unit/unit.test.ts Updates an existing Go logical-operators assertion and adds two regression tests for nested-if cases.
src/test/metricsAnalyzer/metricsAnalyzerFactory.test.ts Adjusts expected Go complexity totals impacted by logical operators no longer being nesting-penalized.
src/test/metricsAnalyzer/languages/goAnalyzer.test.ts Updates Go analyzer expectations where &&/`

@askpt
askpt merged commit fd7d1f7 into main Aug 2, 2026
10 checks passed
@askpt
askpt deleted the repo-assist/test-nesting-boolean-ops-20260802-c80d19f4ca35f3e9 branch August 2, 2026 17:18
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