Skip to content

[javac] Add compiler check for integer division in floating-point contexts#8885

Open
SamCarlberg wants to merge 3 commits into
wpilibsuite:mainfrom
SamCarlberg:javacplugin/integer-division-errors
Open

[javac] Add compiler check for integer division in floating-point contexts#8885
SamCarlberg wants to merge 3 commits into
wpilibsuite:mainfrom
SamCarlberg:javacplugin/integer-division-errors

Conversation

@SamCarlberg
Copy link
Copy Markdown
Member

@SamCarlberg SamCarlberg commented May 10, 2026

Useful for catching bad behavior when doing math or configuring ratios with integers. Errors can be turned off with @SuppressWarnings("IntegerDivision")

double kV = 12 / 6000; // error: integer division in a floating-point context
double kV = 12. / 6000; // OK
double kV = 12 / 6000.; // OK

int kV = 12 / 6000; // OK, but evaluates to 0

double ratio = 42 / 12; // error: integer division in a floating-point context

@SuppressWarnings("IntegerDivision")
double ratio = 42 / 12; // OK, evaluates to 2
Execution failed for task ':developerRobot:compileJava'.
> Compilation failed; see the compiler output below.
  /Users/sam/code/wpilib/allwpilib/developerRobot/src/main/java/wpilib/robot/Robot.java:10: error: integer division in a floating-point context
    double kV = 12 / 6000;
                   ^
  1 error

Also adds the compiler plugin as a dependency to the developerRobot project for dev testing

@SamCarlberg SamCarlberg requested a review from a team as a code owner May 10, 2026 18:13
@SamCarlberg SamCarlberg added the component: javac plugin Java compiler plugin label May 10, 2026
@github-actions github-actions Bot added type: testing Related to testing. build Monorepo organization, Gradle/CMake/Bazel, upstream_utils, etc labels May 10, 2026
@Daniel1464
Copy link
Copy Markdown
Contributor

At least for this PR, i think that the annotation for disabling this check shouldn't be an @SuppressWarnings annotation since that's used to only disable warnings, whereas this integer division check isn't a warning, but an error that prevents the code from compiling. Ideally, it would be a custom annotation, like @EnableIntegerDivision.

Aside from that, though, I'm not sure how I feel about this idea either. It's definitely a really really helpful feature to have, but I'm afraid of getting students into the mindset that every non-FRC java code they write will also check for integer division this way.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

build Monorepo organization, Gradle/CMake/Bazel, upstream_utils, etc component: javac plugin Java compiler plugin type: testing Related to testing.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants