Skip to content

[java] CognitiveComplexity: count switch expressions - #6883

Merged
UncleOwen merged 4 commits into
pmd:mainfrom
KurathSec:java-cognitivecomplexity-switch-expression
Jul 24, 2026
Merged

UncleOwen merged 4 commits into
pmd:mainfrom
KurathSec:java-cognitivecomplexity-switch-expression

Conversation

@KurathSec

@KurathSec KurathSec commented Jul 15, 2026 •

Copy link
Copy Markdown
Contributor

Describe the PR

CognitiveComplexity added a structural increment for a switch statement but not for a switch expression (Java 14+). A switch expression therefore added nothing to the score, and because the visitor never opened a nesting level for it, anything nested inside a switch expression also lost its nesting increment.

CognitiveComplexityVisitor overrode visit(ASTSwitchStatement, State) but had no visit(ASTSwitchExpression, State). This adds it, delegating to the same structural(...) helper the switch statement already uses, so both switch forms are scored the same way. CycloVisitor in the same package already handles both forms through the common ASTSwitchLike supertype, so this brings the two metrics back in line.

Added metric test cases for a flat switch expression (value 1) and a switch expression with a nested if (value 3), mirroring the existing switch statement cases. Locally the cognitive complexity metric and rule tests pass; dropping the new visit method again fails exactly these two cases with values 0 and 1, which is the reported behaviour.

Related issues

Ready?

  • Added unit tests for fixed bug/feature
  • Passing all unit tests (ran the cognitive complexity metric and rule tests locally on JDK 21)
  • Complete build ./mvnw clean verify passes (checked automatically by github actions)
  • Added (in-code) documentation (if needed)

CognitiveComplexityVisitor handled ASTSwitchStatement but not
ASTSwitchExpression, so a switch expression received no structural
increment and opened no nesting level. Anything nested inside it also
lost its nesting increment, and the metric came out too low.

Add visit(ASTSwitchExpression), delegating to the same structural()
helper the switch statement uses, so both forms are scored the same way.
CycloVisitor in the same package already handles both through the common
ASTSwitchLike supertype.

Fixes pmd#6881
Copilot AI review requested due to automatic review settings July 15, 2026 16:46

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 the Java Cognitive Complexity metric so switch expressions (Java 14+) are scored consistently with switch statements, including correctly applying the structural increment and nesting level to code inside the switch expression. This aligns Cognitive Complexity behavior with Cyclomatic Complexity’s existing handling of switch-like constructs.

Changes:

  • Add visit(ASTSwitchExpression, State) to CognitiveComplexityVisitor, delegating to the existing structural(...) logic used for switch statements.
  • Add two metric test cases covering a flat switch expression (value 1) and a switch expression with a nested if (value 3).
  • Add a release notes entry referencing the fixed issue (#6881).

Reviewed changes

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

File Description
pmd-java/src/main/java/net/sourceforge/pmd/lang/java/metrics/internal/CognitiveComplexityVisitor.java Adds switch-expression visitation so Cognitive Complexity applies the same structural + nesting behavior as for switch statements.
pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/metrics/impl/xml/CognitiveComplexityTest.xml Adds regression tests ensuring switch expressions contribute complexity and nesting increments correctly.
docs/pages/release_notes.md Documents the fix in the “Fixed Issues” section under java-design.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@pmd-actions-helper

pmd-actions-helper Bot commented Jul 15, 2026 •

Copy link
Copy Markdown
Contributor

Documentation Preview

⚠️ Running pmdtester failed, this message is mainly used to remind the maintainers of PMD.

(comment created at 2026-07-24 14:28:04+00:00 for f0cf5f4)

@UncleOwen UncleOwen left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thanks!

@UncleOwen
UncleOwen merged commit 866fc5a into pmd:main Jul 24, 2026
19 of 21 checks passed
@UncleOwen UncleOwen added this to the 7.27.0 milestone Jul 25, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[java] CognitiveComplexity does not count switch expressions

3 participants