Skip to content

[java] Fix #5732: UnnecessaryCast false positive for package-private members - #6993

Merged
UncleOwen merged 3 commits into
pmd:mainfrom
tanvir-ux:fix-5732-unnecessary-cast-package-private
Aug 30, 2026
Merged

UncleOwen merged 3 commits into
pmd:mainfrom
tanvir-ux:fix-5732-unnecessary-cast-package-private

Conversation

@tanvir-ux

Copy link
Copy Markdown
Contributor

Fixes #5732

Package-private methods and fields are not inherited by subclasses in another package, so a cast to the declaring type is required to access them.

UnnecessaryCast was treating any subtype cast as removable, including ((Super) sub).packagePrivate(). Public members and same-package subclasses are still flagged.

Ready?

  • Added unit tests for fixed bug/feature
  • Passing all unit tests
  • Complete build ./mvnw clean verify passes (checked automatically by github actions)
  • Added (in-code) documentation (if needed)

@pmd-actions-helper

pmd-actions-helper Bot commented Aug 24, 2026 •

Copy link
Copy Markdown
Contributor

Documentation Preview

Compared to main:
This changeset changes 0 violations,
introduces 0 new violations, 0 new errors and 0 new configuration errors,
removes 0 violations, 16 errors and 9 configuration errors.
There are 0 changed duplications, 0 new duplications and 0 removed duplications.
There are 0 changed CPD errors, 0 new CPD errors and 0 removed CPD errors.

Regression Tester Report

(comment created at 2026-08-30 16:59:05+00:00 for b005a82)

…te members

Package-private methods and fields are not inherited by subclasses in another package, so a cast to the declaring type is required to access them.
@tanvir-ux
tanvir-ux force-pushed the fix-5732-unnecessary-cast-package-private branch from 1e3f5be to 1b890ed Compare August 28, 2026 22:17
…yCast

Package-private members are inherited; the cast is required because they
are not accessible on a subtype in a different package. Also drop
redundant unresolved/null checks already handled in visit().
@tanvir-ux

Copy link
Copy Markdown
Contributor Author

Thanks @UncleOwen. Updated the comments to talk about visibility, not inheritance, and dropped the redundant null checks.

@UncleOwen UncleOwen left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

@UncleOwen UncleOwen added this to the 7.28.0 milestone Aug 30, 2026
@UncleOwen
UncleOwen merged commit 4b366e0 into pmd:main Aug 30, 2026
13 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[java] UnnecessaryCast: False positive with package private methods

2 participants