An integer division is converted to a floating-point number after the fraction is lost

tx0/java:integer-divisionrecommendedwarningbug

What it finds

double ratio = done / total; with two integers divides as integers and drops the fraction before the result becomes a double, so the ratio is 0 or 1. Converting one operand first, (double) done / total, keeps it.

Reported are divisions of two operands whose integral types are known, where the result is converted to float or double: as the value of a variable or assignment of such a type, in a cast to one, or as the argument of Math.ceil, Math.floor, Math.round or Math.rint. A division of two literals that leaves no remainder is left alone.

From the rule's source, integer_division.rs.

Example from the rule's tests
class Ratios {  double ratio(int done, int total, long bytes, double scale) {    double a = done / total;tx0 reports: done / total divides integers    float b = bytes / 1024;tx0 reports: bytes / 1024 divides integers    scale = done / 3;tx0 reports: done / 3 divides integers    double c = (double) (done / total);tx0 reports: done / total divides integers    double d = Math.ceil(done / total);tx0 reports: done / total divides integers    double e = (double) done / total;    int f = done / total;    double g = 10 / 2;    double h = done / scale;    return a + b + c + d + e + f + g + h + scale;  }}
Highlighted lines are the ones tx0 reports, with its message under each; the other lines are left alone.

Turn it on, off or suppress it

The rule runs from the recommended tier up. To turn it on in a lower tier, give it a level in tx0.toml:

tx0.toml
[rules]
"tx0/java:integer-division" = "warning"

To keep one finding, suppress it with a reason. tx0 suppress ID --reason "..." writes the comment on the line before the code:

// tx0: ignore[tx0/java:integer-division] reason="..."

tx0 explain tx0/java:integer-division shows the same in the terminal. Found a false positive? Report it.