An integer division is stored in a floating-point variable and loses its fraction

tx0/csharp:integer-divisionrecommendedwarningreliability/arithmeticdepth fastbug

What it finds

In double ratio = done / total; with integer operands, the division happens in integers and drops the fraction before the result is converted: 1 / 2 gives 0.0. Casting one operand, as in (double)done / total, keeps it. Only operands whose types the file shows as integral are considered.

From the rule's source, integer_division.rs.

Example from the rule's tests
class Ratio{    void Compute(int done, int total)    {        double ratio = done / total;tx0 reports: done / total divides integers and drops the fraction before it is stored        double half = 1 / 2;tx0 reports: 1 / 2 divides integers and drops the fraction before it is stored        double right = (double)done / total;        int whole = done / total;        ratio = done / 3;tx0 reports: done / 3 divides integers and drops the fraction before it is stored    }}
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/csharp: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/csharp:integer-division] reason="..."

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