Please provide
- Operating system: macOS Ventura
- SonarLint plugin version: 8.1.0
- Programming language you’re coding in: Java
- Is connected mode used: Not Connected
If you are using sealed classes and the enhanced switch expressions with pattern matching, SonarLint emits a java:S1301 warning for code like the following:
switch (result)
{
case Result.SuccessfulResult<SearchResponse, ResponseFailure> success ->
resolver.resolve(success.value());
case Result.FailureResult<SearchResponse,ResponseFailure> failure ->
resolver.reject(failure.value());
}
The rule seems to be simply counting the number of case labels and determining that if
statements would be clearer. It is accurate that if
statements can achieve similar results, but I do not agree that the code is any more readable, especially because the enhanced switch statements do not require break statements. I think this rule should be updated to recognize this newer use case and not warn in this common scenario that relies on Maybe/Result monads.