6

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?

Linus Fernandes
  • 498
  • 5
  • 30
radai
  • 23,949
  • 10
  • 71
  • 115

2 Answers2

2

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.

Community
  • 1
  • 1
meriton
  • 68,356
  • 14
  • 108
  • 175
0

There is nothing like this currently in Checkstyle.

You can always create your own check, but tracking variables may not come easy. See https://checkstyle.org/writingchecks.html

Also, Checkstyle isn't type aware tool. Knowing the actual type of the variables/fields may be impossible for it to know in certain situations. See https://checkstyle.org/writingchecks.html#Limitations

rveach
  • 2,081
  • 15
  • 25