GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — [java] UnnecessaryCast: False positive for long cast before bit-shift operations on int/byte · Issue #6427 · pmd/pmd

[java] UnnecessaryCast: False positive for long cast before bit-shift operations on int/byte #6427

New issue
Copy link
New issue
Copy link
Closed
#6573
Closed
[java] UnnecessaryCast: False positive for long cast before bit-shift operations on int/byte#6427
#6573
Copy link
Labels
a:false-positivePMD flags a piece of code that is not problematic
Milestone
7.25.0

Description

@blacelle
blacelle
opened on Jan 28, 2026
Issue body actions

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

Activity

  1. blacelle
    added
    a:false-positivePMD flags a piece of code that is not problematic
    on Jan 28, 2026
  2. adangel
    changed the title [-][java] UnecessaryCast to long for bit-shift operations on int/byte[/-] [+][java] UnecessaryCast: False positive for long cast before bit-shift operations on int/byte[/+] on Jan 29, 2026
  3. adangel
    added this to the 7.25.0 milestone on May 7, 2026
  4. adangel
    changed the title [-][java] UnecessaryCast: False positive for long cast before bit-shift operations on int/byte[/-] [+][java] UnnecessaryCast: False positive for long cast before bit-shift operations on int/byte[/+] on May 7, 2026
  5. adangel
    added a commit that references this issue on May 7, 2026

    [doc] Update release notes (#6427, #6573)

    7717b22
  6. adangel
    closed this as completedin f261b3aon May 7, 2026
  7. adangel
    added a commit that references this issue on May 7, 2026

    [java] Fix pmd#6427: Add bitwise and/or/xor to BINARY_PROMOTED_OPS (p…

    This issue will close once commit f261b3a is merged into the 'main' branch.
    f261b3a
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    a:false-positivePMD flags a piece of code that is not problematic

    Type

    No type

    Projects

    No projects

      Milestone

      • 7.25.0

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions

      You are visitor 01402775 — GeezerVison does not store the pages you look up.
      Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.