fix: correct Go logical operator cognitive complexity (flat +1, not nesting-penalized) - #495
Conversation
…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>
…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 Report❌ Patch coverage is
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. 🚀 New features to boost your workflow:
|
There was a problem hiding this comment.
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 addsthis.nestingto every increment; nesting penalties are applied only wheregetComplexityIncrement()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 &&/` |
🤖 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 appliedbaseIncrement + this.nestingto 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 nestedifwas counting as +2 instead of +1, inflating function complexity.Fix
getComplexityIncrement()now returns the full increment directly:if_statement,for_statement,expression_switch_statement,type_switch_statement,select_statement):1 + this.nestingbinary_expression,func_literal,break_statement,continue_statement,goto_statement,call_expressionfor recover): flat1visit()uses the returned value directly, without addingthis.nestingThis 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:
Test Status
Added two regression tests:
Go: logical operators inside nested if are flat (+1 each, no nesting penalty)Go: logical operators inside doubly-nested ifs are still flat +1Closes #none (standalone bug fix)
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 workflow to your repo
To install this agentic workflow, run