SONARJAVA-6867 Replace if/else chains with switch expressions in pattern matching code - #6054
Open
sonarqube-agent[bot] wants to merge 1 commit into
Open
SONARJAVA-6867 Replace if/else chains with switch expressions in pattern matching code#6054sonarqube-agent[bot] wants to merge 1 commit into
sonarqube-agent[bot] wants to merge 1 commit into
Conversation
Fixed issues: - AaBB4bdV2vS79_8iEWy1 for java:S6880 rule - AaBB4b6D2vS79_8iEWy6 for java:S6880 rule - AaBB4ayF2vS79_8iEWy0 for java:S6880 rule - AaBB4bvV2vS79_8iEWy4 for java:S6880 rule - AaBB4cC32vS79_8iEWy7 for java:S6880 rule Generated by SonarQube Agent (task: 97a02fde-db70-41f7-b2b8-6e16fcbf96b3)
Contributor
Contributor
|
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.




This PR converts five if/else chains that use instanceof checks into modern Java switch expressions with pattern matching. This refactoring improves code clarity, maintainability, and aligns with Java 21+ best practices for type checking and pattern matching.
View Project in SonarCloud
Fixed Issues
java:S6880 - Replace the chain of if/else with a switch expression. • MAJOR • View issue
Location:
java:java-checks/src/main/java/org/sonar/java/checks/TryWithResourcesCheck.java:100Why is this an issue?
Comparing a variable to multiple cases is a frequent operation. This can be done using a sequence of if-else statements. However, for many cases like enums or simple value comparisons, a
switchstatement is the better alternative. With Java 21, theswitchstatement has been significantly improved to support pattern matching and record pattern.What changed
This hunk replaces the beginning of an if/else chain that uses
instanceofchecks (starting withif (tree instanceof NewClassTree newClass)andelse if (tree instanceof MethodInvocationTree mit)) with aswitchexpression using pattern matching (return switch (tree) { case NewClassTree newClass -> ...; case MethodInvocationTree mit -> ...;). This directly addresses the code smell about replacing if/else chains with switch expressions at line 100 of TryWithResourcesCheck.java.java:S6880 - Replace the chain of if/else with a switch expression. • MAJOR • View issue
Location:
java:java-checks/src/main/java/org/sonar/java/checks/helpers/StringUtils.java:63Why is this an issue?
Comparing a variable to multiple cases is a frequent operation. This can be done using a sequence of if-else statements. However, for many cases like enums or simple value comparisons, a
switchstatement is the better alternative. With Java 21, theswitchstatement has been significantly improved to support pattern matching and record pattern.What changed
This hunk replaces an if-else chain using instanceof checks (for String, String[], Collection<?>) with a switch expression using pattern matching. This directly addresses the code smell about replacing if/else chains with switch expressions, as the original code at line 63 of StringUtils.java was flagged for using an if-else chain that should be a switch.
java:S6880 - Replace the chain of if/else with a switch expression. • MAJOR • View issue
Location:
java:java-checks/src/main/java/org/sonar/java/checks/DateTimeConversionsCheck.java:102Why is this an issue?
Comparing a variable to multiple cases is a frequent operation. This can be done using a sequence of if-else statements. However, for many cases like enums or simple value comparisons, a
switchstatement is the better alternative. With Java 21, theswitchstatement has been significantly improved to support pattern matching and record pattern.What changed
This hunk replaces an if-else chain using
instanceofpattern matching with aswitchexpression at line 102 of DateTimeConversionsCheck.java. The original code usedif (result instanceof ParenthesizedTree ...) ... else if (result instanceof TypeCastTree ...) ... else ..., which the static analysis rule flagged as a code smell that should be converted to a switch statement. The new code uses aswitch (result)with pattern matching cases (case ParenthesizedTree,case TypeCastTree,default), which is cleaner and follows the recommended Java 21+ switch pattern matching style. This single hunk addresses the issue about replacing if/else chains with switch expressions, providing clearer code structure and potentially better performance.java:S6880 - Replace the chain of if/else with a switch expression. • MAJOR • View issue
Location:
java:java-checks/src/main/java/org/sonar/java/checks/PatternMatchUsingIfCheck.java:220Why is this an issue?
Comparing a variable to multiple cases is a frequent operation. This can be done using a sequence of if-else statements. However, for many cases like enums or simple value comparisons, a
switchstatement is the better alternative. With Java 21, theswitchstatement has been significantly improved to support pattern matching and record pattern.What changed
This hunk replaces the beginning of an if-else chain that checks
caze instanceof PatternMatchCasewith a switch expression using pattern matching (switch (caze) { case PatternMatchCase patternMatchCase -> { ... }). This directly addresses the code smell about replacing if/else chains with switch expressions at line 220 of PatternMatchUsingIfCheck.java, as the original code usedif (caze instanceof PatternMatchCase)followed byelse ifandelsebranches. The issue is that comparing a variable to multiple cases via if-else chains should be replaced with a switch statement for clearer code and better performance.java:S6880 - Replace the chain of if/else with a switch expression. • MAJOR • View issue
Location:
java:java-frontend/src/main/java/org/sonar/java/model/JSymbolMetadata.java:411Why is this an issue?
Comparing a variable to multiple cases is a frequent operation. This can be done using a sequence of if-else statements. However, for many cases like enums or simple value comparisons, a
switchstatement is the better alternative. With Java 21, theswitchstatement has been significantly improved to support pattern matching and record pattern.What changed
This hunk replaces a chain of if/else statements in JSymbolMetadata.java that check the type of
symbolusing method calls (isVariableSymbol(),isMethodSymbol(),isTypeSymbol(),isPackageSymbol()) with aswitchexpression using pattern matching. The original if-else chain compared a variable against multiple cases, which the static analysis flagged as a code smell recommending replacement with a switch expression for clearer code and better structure. The new switch expression matchessymbolagainstSymbol.VariableSymbol,Symbol.MethodSymbol, andSymbol.TypeSymbolpatterns, with a default case handling the package symbol and unknown cases. This directly addresses the rule that an if/else chain should be replaced by a switch expression, as reported at line 411 of JSymbolMetadata.java.SonarQube Remediation Agent uses AI. Check for mistakes.