We’re using SonarQube version 6.7.5 (build 38563) and the following code gets flagged by the “Dead stores should be removed” rule:
int retriesLeft = maxRetries;
int currentBackoffInMillis = backoffInMillis;
while (true) {
try {
return function.get();
} catch (CommunicationException e) {
if (retriesLeft == 0) {
throw e;
}
onRetryListener.onRetry(e, currentBackoffInMillis);
CommonUtil.sleep(currentBackoffInMillis);
currentBackoffInMillis *= 2;
retriesLeft--;
}
}
It says that the values of retriesLeft and currentBackoffInMillis are not read after they’re written, but the values are obviously used in the body of the catch block.
This false positive is also reproducible with version 4.1.0.201901311043 of the SonarLint extension for Eclipse.
If you’re in connected mode, you’ll be using the version of SonarJava that’s loaded on your server. Try this call: api/plugins/installed (but please don’t give me the whole output ).
I didn’t manage to reproduce the issue, can you try to create code sample reproducing the issue and having all the symbols declared (i.e. its compilable)?
I tried the following snippet, which is imo equivalent and issue is not reported.
abstract class S1854 {
void test() {
int x = 42;
while (true) {
try {
bar();
} catch (Exception e) {
if (x == 0) {
throw e;
}
x--;
}
}
}
abstract void bar();
}