- Operating system: Windows 10
- SonarLint plugin version: 7.4.0.60471
- Programming language you’re coding in: Java
- Is connected mode used: No
I am getting an accusation of S1854: Useless Assignment to variable for the following code:
01. short timeOuts = 0;
02.
03. while (!Thread.currentThread().isInterrupted()) {
04. try {
05. Message rawMessage = (Message) socketIn.readObject();
06. timeOuts = 0;
07. // ...
08. } catch (SocketTimeoutException ignored) {
09. timeOuts++;
10. if (timeOuts > 5)
11. throw new IOException();
12. }
13. }
Irrelevant parts of the code were omitted for clarity. It initializes a counter of socket time outs to 0 (L01), and then counts the number of time outs that happen in a row (L08-12). If a message arrives after one or more time outs, the counter is reset to 0 (L06). S1854 is accused on L06, but were that assignment to be removed, the time out counter would never be reset upon receiving a message.