Repository navigation
[java] Fix #6744: ReturnEmptyCollectionRatherThanNull: Analyze possible null return values - #6941
Conversation
|
Compared to main: (comment created at 2026-08-18 11:40:10+00:00 for 44af6d9) |
UncleOwen
left a comment
There was a problem hiding this comment.
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.
fdbf103 to
a9f1d2f
Compare
a9f1d2f to
a2299fb
Compare
|
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 Following local variables also reports 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. |
a2299fb to
c0491ba
Compare
1671578 to
66a90e3
Compare
|
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
left a comment
There was a problem hiding this comment.
The rule itself looks fine (some small suggestions), but please don't make the code worse just because one of the dogfood rules trigger.
66a90e3 to
11bd726
Compare
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]>
11bd726 to
fc96f9a
Compare
|
Thanks! |
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]>
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).
#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]>
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
determinePersistentFieldsin PMD's own source (NonSerializableClassRule), so that method is converted to returnOptionalto keep the dogfood build clean.The analysis reports on the return statement and preserves method, lambda, and nested executable scope boundaries.
Related issues
Ready?
./mvnw clean verifypasses (checked automatically by github actions)