In this SonarCloud run on my open source github project, I received a bug report saying “unreachable code”. What’s odd is that I have a unit test that asserts that this code path is indeed reachable.
The code in question looks like this:
New code
// At least one prefix matched, but the keys weren't valid
if (prefixMatches == 1)
{
return new ApiKeyResult() { Message = lastAlgorithmFailedMessage };
}
if (prefixMatches == 0)
Change this condition so that it does not always evaluate to 'True'. Some code paths are unreachable.
{
return new ApiKeyResult() { Message = "Key prefix does not match any supported key algorithms." };
}
return
new ApiKeyResult() { Message = "Invalid API key hash." };
It’s possible for my code to generate zero, one, or many matches, and I want it to return different error messages in each case. I have unit tests for each scenario and they all pass. Any idea why SonarCloud thinks this code is unreachable?