Skip to content

[java] Fix #6744: ReturnEmptyCollectionRatherThanNull: Analyze possible null return values - #6941

Merged
UncleOwen merged 3 commits into
pmd:mainfrom
fudianchn:fix/returnemptycollection-ternary-null-6744
Aug 18, 2026
Merged

UncleOwen merged 3 commits into
pmd:mainfrom
fudianchn:fix/returnemptycollection-ternary-null-6744

Conversation

@fudianchn

@fudianchn fudianchn commented Aug 5, 2026 •

Copy link
Copy Markdown
Contributor

Describe the PR

Replaces the syntax-specific XPath change with a Java rule that analyzes the value reaching each return statement.

The rule reports one violation when an array, Collection, or Map return value has an explicit null source through a null literal, a conditional value branch, cast, switch yield, or plain assignment. A bare returned local variable is followed through its reaching definitions and reported when any of them assigns an explicit null source. The null literal is ignored when it appears only in a condition, a switch selector, or an array element. Unknown values (method calls, field reads and parameters) are not treated as null without an explicit null source.

Following local variables also reports determinePersistentFields in PMD's own source (NonSerializableClassRule), so that method is converted to return Optional to keep the dogfood build clean.

The analysis reports on the return statement and preserves method, lambda, and nested executable scope boundaries.

Related issues

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 9, 2026 •

Copy link
Copy Markdown
Contributor

Documentation Preview

Compared to main:
This changeset changes 698 violations,
introduces 131 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-18 11:40:10+00:00 for 44af6d9)

@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.

I don't think that's the way to go here. There's a million-and-one different ways to write a constant expression that evaluates to null. Hardcoding one of them doesn't solve anything.

What we should do is analyze what is being returned and check if it could be null.

@fudianchn
fudianchn marked this pull request as draft August 11, 2026 05:32
@fudianchn
fudianchn force-pushed the fix/returnemptycollection-ternary-null-6744 branch from fdbf103 to a9f1d2f Compare August 11, 2026 07:06
@fudianchn fudianchn changed the title [java] Fix #6744: ReturnEmptyCollectionRatherThanNull flags ternary-wrapped null [java] Fix #6744: Analyze possible null return values Aug 11, 2026
@fudianchn
fudianchn force-pushed the fix/returnemptycollection-ternary-null-6744 branch from a9f1d2f to a2299fb Compare August 11, 2026 07:56
@fudianchn
fudianchn marked this pull request as ready for review August 11, 2026 08:17
@fudianchn

fudianchn commented Aug 11, 2026 •

Copy link
Copy Markdown
Contributor Author

Reworked this into a Java rule that analyzes the value reaching each return statement, instead of matching one ternary shape.

The rule reports the return when the value it produces has an explicit null source: a null literal, a value branch of a conditional, the operand of a cast, a switch yield, or the right-hand side of a plain assignment. A bare return localVar; is followed through the reaching definitions of the local: it is reported when any of them assigns an explicit null source, and definitions the dataflow cannot fully determine are treated as non-null. The condition of a conditional and the selector of a switch only select a value, so they are not examined, and a local nested inside a conditional or switch branch is not traced through its definitions there. Method calls, field reads and parameters are not treated as null without an explicit null source. Each return is reported at most once, on the return statement.

Following local variables also reports determinePersistentFields in PMD's own source (NonSerializableClassRule), so this PR converts it to return Optional and keeps the dogfood build clean.

Verified with ReturnEmptyCollectionRatherThanNullTest (38 tests, 0 failures); the regression tester delta is interpreted below.

Regression tester (vs main): 131 new violations of this rule (77 on openjdk-11, 48 on spring-framework, 6 on checkstyle), all from the now-detected conditional/cast/switch/assignment/local-variable null-return shapes. No new errors, and no behavioral change in other rules: the only other diffs are 8 UnusedReturnValue entries whose message text alone changed (javac generic capture numbering). The changed violations of this rule (395 on openjdk-11, 190 on spring-framework, 113 on checkstyle) are the report location moving from the null literal to the return statement on the same line.

This covers the local-variable case of #6695 and the conditional reassignment of #3880. A null reaching the return through a method call, field read or parameter is still not reported.

@fudianchn
fudianchn marked this pull request as draft August 12, 2026 00:37
@fudianchn
fudianchn force-pushed the fix/returnemptycollection-ternary-null-6744 branch from a2299fb to c0491ba Compare August 12, 2026 16:32
@fudianchn
fudianchn marked this pull request as ready for review August 12, 2026 16:46
@fudianchn
fudianchn force-pushed the fix/returnemptycollection-ternary-null-6744 branch 3 times, most recently from 1671578 to 66a90e3 Compare August 15, 2026 14:01
@fudianchn

Copy link
Copy Markdown
Contributor Author

Rebased onto current main (release-notes conflict resolved, single commit 66a90e3) since the previous rework; ReturnEmptyCollectionRatherThanNullTest: 38/38 pass on the new head. Ready for re-review.

@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.

The rule itself looks fine (some small suggestions), but please don't make the code worse just because one of the dogfood rules trigger.

@fudianchn
fudianchn marked this pull request as draft August 18, 2026 00:46
@fudianchn
fudianchn force-pushed the fix/returnemptycollection-ternary-null-6744 branch from 66a90e3 to 11bd726 Compare August 18, 2026 00:46
@fudianchn
fudianchn marked this pull request as ready for review August 18, 2026 00:47
@fudianchn
fudianchn marked this pull request as draft August 18, 2026 01:57
Replace the XPath rule with a Java rule that analyzes the value reaching
each return statement. Reports an explicit null source (null literal,
conditional value branch, cast, switch yield, plain assignment) for an
array, Collection, or Map return, and for a bare returned local variable
follows its reaching definitions to an explicit null source (pmd#3880, pmd#6695).
Reaching definitions are only followed for a directly returned local, not
for a variable inside a conditional or switch branch, so a null guard such
as x != null ? x : other is not reinterpreted.

The extended analysis now flags a null return in PMD's own source:
NonSerializableClassRule#determinePersistentFields uses null as a "no
serialPersistentFields declaration" sentinel. That is a false positive of
the rule, so the method is annotated @nullable and suppressed with
@SuppressWarnings. The pmd-main check analyzes with the released PMD,
for which that suppression is unnecessary, so UnnecessaryWarningSuppression
is excluded for the class until 7.27.0 is released.

Signed-off-by: 付典 <[email protected]>
@fudianchn
fudianchn force-pushed the fix/returnemptycollection-ternary-null-6744 branch from 11bd726 to fc96f9a Compare August 18, 2026 02:05
@fudianchn
fudianchn marked this pull request as ready for review August 18, 2026 02:05
@UncleOwen UncleOwen added this to the 7.27.0 milestone Aug 18, 2026
@UncleOwen
UncleOwen merged commit 5d65ca4 into pmd:main Aug 18, 2026
13 checks passed
@UncleOwen

Copy link
Copy Markdown
Member

Thanks!

@adangel adangel changed the title [java] Fix #6744: Analyze possible null return values [java] Fix #6744: ReturnEmptyCollectionRatherThanNull: Analyze possible null return values Aug 18, 2026
arimu1 added a commit to arimu1/pmd that referenced this pull request Aug 21, 2026
Add UnnecessaryCast regression test for raw casts in INVOCATION context,
remove temporary PMD.UnnecessaryCast suppressions, and add pmd-main exclude
entries until the next release (following pmd#6941 pattern).

Co-authored-by: Cursor <[email protected]>
arimu1 added a commit to arimu1/pmd that referenced this pull request Aug 21, 2026
Add UnnecessaryCast regression test for raw casts in INVOCATION context,
remove temporary PMD.UnnecessaryCast suppressions, and add pmd-main exclude
entries until the next release (following pmd#6941 pattern).
UncleOwen pushed a commit that referenced this pull request Aug 21, 2026
#6982)

* [java] Fix #6943: UnnecessaryCast false positives related to generics

* [java] Address #6982 review: INVOCATION test and dogfood excludes

Add UnnecessaryCast regression test for raw casts in INVOCATION context,
remove temporary PMD.UnnecessaryCast suppressions, and add pmd-main exclude
entries until the next release (following #6941 pattern).

---------

Co-authored-by: arimu1 <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

2 participants