Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Checkstyle rule for suspicious integer division?

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?

like image 954
radai Avatar asked Oct 05 '19 18:10

radai


People also ask

What is the severity level of a Checkstyle check?

Each module has a severity property that a Checkstyle audit assigns to all violations of the check. The default severity level of a check is error . You can use the severity property to control the output of the plain formatter for the command line tool and the ANT task.

What can Checkstyle do with each rule?

Each rule can raise notifications, warnings, and errors. For example, Checkstyle can examine the following: Multiple complexity measurements. Checkstyle is available as a JAR file which can run inside a Java VM or as an Apache Ant task.

How do violation messages work in Checkstyle?

Each check has a section called 'Violation Messages'. This section lists every key the check uses and links to the default message used by checkstyle. A Checker module has a set of Filter submodules to filter audit events, including the violation messages fired by Checks.

What is Checkstyle in Java?

Overview Checkstyle is an open source tool that checks code against a configurable set of rules. In this tutorial, we're going to look at how to integrate Checkstyle into a Java project via Maven and by using IDE plugins.


1 Answers

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.

like image 162
meriton Avatar answered Sep 22 '22 16:09

meriton