Bug 344396 - [compiler] warn about range problem regarding hex literal
Summary: [compiler] warn about range problem regarding hex literal
Status: NEW
Alias: None
Product: JDT
Classification: Eclipse Project
Component: Core (show other bugs)
Version: 3.7   Edit
Hardware: PC Linux
: P3 enhancement (vote)
Target Milestone: ---   Edit
Assignee: JDT-Core-Inbox CLA
QA Contact:
URL:
Whiteboard:
Keywords: helpwanted
Depends on:
Blocks:
 
Reported: 2011-05-01 08:23 EDT by Stephan Herrmann CLA
Modified: 2014-06-15 13:39 EDT (History)
2 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
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 :)