Skip to content

[java] Fix #7313: Fold casts to boolean and boolean equality - #7314

Merged
UncleOwen merged 2 commits into
pmd:mainfrom
saryn17:fix-7313
Nov 14, 2026
Merged

UncleOwen merged 2 commits into
pmd:mainfrom
saryn17:fix-7313

Conversation

@saryn17

@saryn17 saryn17 commented Nov 11, 2026 •

Copy link
Copy Markdown
Contributor

Describe the PR

ConstantFolder now folds a cast to boolean and the operators == and != with two boolean operands, which JLS 15.29 allows in constant expressions. A cast to Boolean still has no constant value. This changes getConstFoldingResult(), getConstValue(), isCompileTimeConstant() and the XPath attribute @CompileTimeConstant for such expressions.

With this change, the block lambda bodies in the sample of #7313 are value-compatible, and the three pool.submit(..) calls select submit(Callable), as with javac.

New cases are in testCasts and testEquality of ConstantExpressionsTests and in ConstValuesKotlinTest, and one block lambda body is added in LambdaInferenceTest. On main (4a1d37d), the new isConst cases, the lambda body and the ConstValuesKotlinTest case fail. The notConst cases pass. The disabled case of UnnecessaryBoxing.xml in #7136 that waits for #7313 passes with this change.

Outside the PR, I ran all Java rules except the documentation category before and after this change, without auxclasspath. The input was 2,145 files of the JDK 21 sources (java.base, java.desktop, java.xml) and the 914 main source files of the PMD modules other than pmd-core and pmd-java. Four ANTLR-generated files among them have @SuppressWarnings("all"), so PMD reports nothing for them. The reports are the same. A second rule prints the method selected at each method call, constructor call, explicit constructor call and method reference. Its output is also the same, for 162,950 sites in the JDK files and 12,712 in the PMD modules. Without auxclasspath, the overload resolution fails at 67 and 5,242 of them.

I also ran the rules on a small sample with these expressions in field initializers, conditions, a return and conditional expressions. The only difference is a new FinalFieldCouldBeStatic report for private final boolean castField = (boolean) true;. javac gives that field a ConstantValue attribute.

Ran mvnw.cmd -B -ntp -pl :pmd-java verify with Java 21 on Windows 11. Of 10,799 tests, none failed or had errors, and 21 were skipped.

Related issues

Ready?

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

JLS 15.29 allows casts to primitive types and the operators == and !=
in constant expressions. ConstantFolder folded a cast only to a
numeric type or String. It folded == and != only for numeric and char
operands. So (boolean) true and true == false had no constant value.

A cast to boolean now keeps a boolean operand value, and == and !=
compare two boolean values. A cast to Boolean still has no constant
value.
@pmd-actions-helper

pmd-actions-helper Bot commented Nov 11, 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, 0 errors and 0 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-11-14 02:48:17+00:00 for 5a8917a)

A comparison or a cast of final Boolean locals has a folded value, but
it is not a compile-time constant. A comparison of final boolean locals
is a compile-time constant.

@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 merged commit f7b7388 into pmd:main Nov 14, 2026
13 checks passed
@adangel adangel added this to the 7.30.0 milestone Nov 14, 2026
saryn17 added a commit to saryn17/pmd that referenced this pull request Nov 16, 2026
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] Casts to boolean and boolean equality are not folded as constant expressions

3 participants