| Summary: | [compiler] warn about range problem regarding hex literal | ||
|---|---|---|---|
| Product: | [Eclipse Project] JDT | Reporter: | Stephan Herrmann <stephan.herrmann> |
| Component: | Core | Assignee: | JDT-Core-Inbox <jdt-core-inbox> |
| Status: | NEW --- | QA Contact: | |
| Severity: | enhancement | ||
| Priority: | P3 | CC: | amj87.iitr, deepakazad |
| Version: | 3.7 | Keywords: | helpwanted |
| Target Milestone: | --- | ||
| Hardware: | PC | ||
| OS: | Linux | ||
| Whiteboard: | |||
In that case, it might be relevant to have such a warning not just in comparisons but also in assignments, case statements, etc. no? Sounds like a good exercise for a new contributor :) |
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