Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read this important communication.

Bug 344396

Summary: [compiler] warn about range problem regarding hex literal
Product: [Eclipse Project] JDT Reporter: Stephan Herrmann <stephan.herrmann>
Component: CoreAssignee: JDT-Core-Inbox <jdt-core-inbox>
Status: NEW --- QA Contact:
Severity: enhancement    
Priority: P3 CC: amj87.iitr, deepakazad
Version: 3.7Keywords: helpwanted
Target Milestone: ---   
Hardware: PC   
OS: Linux   
Whiteboard:

Description Stephan Herrmann CLA 2011-05-01 08:23:30 EDT
I just wrote this subtly silly code

    long crc = crc32.getValue();
    if (crc == 0x42132087 || crc == 0xdb1b9859)
        doSomething();

and now I'm wondering if the compiler could have warned me:
the first comparison works as expected but for the second
constant the value wraps around the int range resulting in
a negative int which is then compared to a long where no
wrapping would have occurred.

The scenario to warn about would be:
- comparing long to in
- int is a hex or oct constant (issue cannot be produced with dec constant)
- the constant represents a negative value (no unary minus involved)
The proposed fix would be:
- append "L" to the constant
Comment 1 Ayushman Jain CLA 2011-05-02 01:50:26 EDT
In that case, it might be relevant to have such a warning not just in comparisons but also in assignments, case statements, etc. no?
Comment 2 Stephan Herrmann CLA 2014-06-15 13:39:53 EDT
Sounds like a good exercise for a new contributor :)