可疑整数除法的检查样式规则?

Checkstyle rule for suspicious integer division?

是否有一个 checkstyle 规则可以捕获这样的东西:

double result = someInt / someOtherInt;

result 是 double(因此显然需要分数)但右侧会进行整数除法(向下舍入)。

有这样的东西吗?

Checkstyle 目前没有类似的内容。

您始终可以创建自己的支票,但跟踪变量可能并不容易。参见 https://checkstyle.org/writingchecks.html

此外,Checkstyle 不是类型感知工具。在某些情况下,可能无法知道 variables/fields 的实际类型。参见 https://checkstyle.org/writingchecks.html#Limitations

没有,但是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.