google / error-prone

Catch common Java mistakes as compile-time errors
https://errorprone.info
Apache License 2.0
6.82k stars 741 forks source link

A false negative related to DivZero #2413

Closed ghost closed 3 years ago

ghost commented 3 years ago

Description of the problem / feature request:

A false negative related to DivZero

Feature requests: what underlying problem are you trying to solve with this feature?

An exception caused by this issue's case. Based on the description in the official document, Error Prone should have reported a warning to detect the bug because when var is equal to zero, the program will throw an exception.

Bugs: what's the simplest, easiest way to reproduce this bug? Please provide a minimal example if possible.

public void func() {
    int var = 0;
    int target = 1 / var;  // should report a warning here
}

What version of Error Prone are you using?

error-prone: 2.7.1

tbroyer commented 3 years ago

The DivZero check only matches literal zeros, so I would say this is the expected behavior actually.

cushon commented 3 years ago

This is sort of similar to https://github.com/google/error-prone/issues/2414, our focus is mostly on catching bugs with simple intraprocedural analysis.

Catching the example you provided would be do-able, but would add complexity to the check and may not catch a lot of additional bugs. And continuing to generalize it to handle more cases like that would quickly require dataflow, and then interprocedural analysis.

We're not trying to provide sound guarantees that e.g. an ArithmeticException from dividing by zero can never happen, we're just trying to prevent some simple cases where it definitely will.