Skip to content

SONARJAVA-6842: Implement S9364: Local variables should not span switch case groups - #6019

Merged
nathsou merged 3 commits into
masterfrom
new-rule/S9364
Aug 25, 2026
Merged

SONARJAVA-6842: Implement S9364: Local variables should not span switch case groups#6019
nathsou merged 3 commits into
masterfrom
new-rule/S9364

Conversation

@nathsou

@nathsou nathsou commented Aug 24, 2026

Copy link
Copy Markdown
Contributor

Summary

  • Detect local variables declared directly in one colon-style switch case group and accessed from later groups.
  • Report later cross-group accesses as secondary locations while avoiding arrow rules, nested declarations, and no-semantic false positives.
  • Add focused statement, expression, boundary, and no-semantic tests plus generated rule metadata.

Links

Test plan

  • mvn -pl java-checks -am test -Dtest=LocalVariablesShouldNotSpanSwitchCaseGroupsCheckTest -Dsurefire.failIfNoSpecifiedTests=false

AI disclosure

  • LLM model used for implementation: gpt-5.6-sol-medium

Prevent local variable declarations from coupling distinct switch case groups.
@nathsou nathsou self-assigned this Aug 24, 2026
@hashicorp-vault-sonar-prod

hashicorp-vault-sonar-prod Bot commented Aug 24, 2026

Copy link
Copy Markdown
Contributor

SONARJAVA-6842

Lock secondary messages and nested-switch duplicate suppression.
@datadog-sonarsource

This comment has been minimized.

CI rejected compound assignments that can execute before their variables are initialized. Use definite assignments for those cross-group accesses and correct the nested-switch secondary offset flagged in review.
@gitar-bot

gitar-bot Bot commented Aug 24, 2026

Copy link
Copy Markdown
Code Review ✅ Approved 1 resolved / 1 findings

Implements rule S9364 to detect local variables spanning colon-style switch case groups, addressing the wrong secondary offset for 'outer' in the nested switch test. No issues found.

✅ 1 resolved
Bug: Wrong secondary offset for 'outer' in nested switch test

📄 java-checks-test-sources/default/src/main/java/checks/LocalVariablesShouldNotSpanSwitchCaseGroupsCheckSample.java:144 📄 java-checks-test-sources/default/src/main/java/checks/LocalVariablesShouldNotSpanSwitchCaseGroupsCheckSample.java:149 📄 java-checks-test-sources/default/src/main/java/checks/LocalVariablesShouldNotSpanSwitchCaseGroupsCheckSample.java:150
The check reports the secondary location at the first access to the symbol in each later group (FirstSymbolAccessVisitor). For outer (declared line 144), the first access in the default group is outer = 1; on line 149, i.e. offset +5 — not +6, which points at line 150 (int nested = 0;, unrelated to outer). The secondary=+6 expectation on line 144 will not match the actual issue and the test should fail. Change it to secondary=+5.

Implementation Status ✅ 2 of 2 objectives covered
SONARJAVA-6842 - 2 of 2 objectives covered

This PR implements rule S9364 to report local variable declarations in colon-style switch statements and expressions when accessed from later distinct case groups, using the declaration identifier as the primary location and the first access in each later case group as a secondary location with the specified message.

✅ 2 covered here
  • ✅ Use the declaration identifier as the primary location and the first access in each later case group as a secondary location with the message 'Accessed from this later case group.'
  • ✅ Implement rule S9364 to report local variable declarations in colon-style switch statements and expressions when accessed from a later distinct case group
Options

Auto-apply is off → Gitar will not commit updates to this branch.
Display: compact → Showing less information.

Comment with these commands to change the behavior for this request:

Auto-apply Compact
gitar auto-apply:on         
gitar display:verbose         

Was this helpful? React with 👍 / 👎 | Gitar

@sonarqube-next

Copy link
Copy Markdown
Contributor

@nathsou
nathsou merged commit 16212ad into master Aug 25, 2026
20 checks passed
@nathsou
nathsou deleted the new-rule/S9364 branch August 25, 2026 08:15
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.

2 participants