An integer division is stored in a floating variable, after its fraction is already lost

tx0/c:integer-division-to-floatingrecommendedwarningbug, cwe-682

What it finds

double ratio = done / total; with integer done and total: the division is done in integers and the fraction is gone before the value becomes a double. Converting one operand first, (double)done / total, keeps it.

Both operands must be integer literals or names the file declares with an integer type, and the target a variable the file declares as float or double.

From the rule's source, integer_division_to_floating.rs.

Example from the rule's tests
double ratio(int done, int total){  double r = done / total;tx0 reports: this integer division drops the fraction  double half;  half = 1 / 2;tx0 reports: this integer division drops the fraction  r = (double)done / total;  return r + half;}
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/c:integer-division-to-floating" = "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/c:integer-division-to-floating] reason="..."

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