The analyzer has detected that the value of the postfix operation is not used. Most likely, either the operation is superfluous, or a prefix operation should be used instead of a postfix operation.
Example:
int calculateSomething() {
int value = getSomething();
....
return value++;
}
In this example, there is a local variable 'value'. The method is expected to return its incremented value. However, according to JLS:
The value of the postfix increment expression is the value of the variable before the new value is stored.
Thus, the '++' operator will have no effect on the value returned by the 'calculateSomething' method. Possible corrected option:
int calculateSomething() {
int value = getSomething();
....
return ++value;
}
The following option of corrected code emphasizes even better that the returned value must be greater by one:
int calculateSomething() {
int value = getSomething();
....
return value + 1;
}
We recommend using the second option, as it is easier to comprehend.
Another synthetic example:
void foo() {
int value = getSomething();
bar(value++);
bar(value++);
bar(value++);
}
Each time the 'bar' method is called with an argument greater by one. The last increment does not make sense, since the increased value of the variable is not used further. However, there is no error here, since the last increment is written for aesthetic reasons. No warning will be issued if a variable is incremented sequentially more than two times in a row.
However, we still recommend writing as follows:
void foo() {
int value = getSomething();
bar(value++);
bar(value++);
bar(value);
}
Another possible option:
void foo() {
int value = getSomething();
bar(value + 0);
bar(value + 1);
bar(value + 2);
}
This diagnostic is classified as: