Affects PMD Version:
7.20.0
Rule:
UnnecessaryCast
Description:
Code Sample demonstrating the issue:
long l2 = ((long) someInt & 0xFF) << 32;
The cast is necessary as the expression is immediately bit-shifted, which leads to different behavior over an int or a long. For instance, the following demonstrates the assertion is false:
@Test
public void testCastLongIsUseful() {
int someInt = 1;
long withCast = ((long) someInt & 0xFF) << 32;
long withoutCast = (someInt & 0xFF) << 32;
Assertions.assertThat(withCast).isEqualTo(withoutCast);
}
The cast would be unecessary if the expression without cast was guaranteed to be a long (e.g. if 0xFF was actually a long/written as 0xFFL).
Expected outcome:
PMD reports a violation at line 0, but that's wrong. That's a false positive.
Running PMD through: [CLI | Ant | Maven | Gradle | Designer | Other]
maven
Affects PMD Version:
7.20.0
Rule:
UnnecessaryCast
Description:
Code Sample demonstrating the issue:
The cast is necessary as the expression is immediately bit-shifted, which leads to different behavior over an int or a long. For instance, the following demonstrates the assertion is false:
The cast would be unecessary if the expression without cast was guaranteed to be a long (e.g. if
0xFFwas actually a long/written as0xFFL).Expected outcome:
PMD reports a violation at line 0, but that's wrong. That's a false positive.
Running PMD through: [CLI | Ant | Maven | Gradle | Designer | Other]
maven