Checkstyle rule for suspicious integer division?

radai :

Is there a checkstyle rule that will catch something like this:

double result = someInt / someOtherInt;

result is double (so clearly fractions are desired) yet the right-hand side would do integer division (rounding down).

Does something like this exist?

meriton :

No, but findbugs can:

ICAST: Integral division result cast to double or float (ICAST_IDIV_CAST_TO_DOUBLE)

This code casts the result of an integral division (e.g., int or long division) operation to double or float. Doing division on integers truncates the result to the integer value closest to zero. The fact that the result was cast to double suggests that this precision should have been retained. What was probably meant was to cast one or both of the operands to double before performing the division.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=305112&siteId=1