Repository navigation
[java] CognitiveComplexity: count switch expressions - #6883
Merged
UncleOwen merged 4 commits intoJul 24, 2026
Merged
Conversation
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
Contributor
There was a problem hiding this comment.
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)toCognitiveComplexityVisitor, delegating to the existingstructural(...)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.
Contributor
|
(comment created at 2026-07-24 14:28:04+00:00 for f0cf5f4) |
…exity-switch-expression # Conflicts: # docs/pages/release_notes.md
UncleOwen
reviewed
Jul 24, 2026
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.
Describe the PR
CognitiveComplexityadded a structural increment for aswitchstatement but not for aswitchexpression (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.CognitiveComplexityVisitoroverrodevisit(ASTSwitchStatement, State)but had novisit(ASTSwitchExpression, State). This adds it, delegating to the samestructural(...)helper the switch statement already uses, so both switch forms are scored the same way.CycloVisitorin the same package already handles both forms through the commonASTSwitchLikesupertype, 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 newvisitmethod again fails exactly these two cases with values 0 and 1, which is the reported behaviour.Related issues
Ready?
./mvnw clean verifypasses (checked automatically by github actions)