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] Fix #7313: Fold casts to boolean and boolean equality by saryn17 · Pull Request #7314 · pmd/pmd

Conversation

@saryn17

@saryn17 saryn17 commented Nov 11, 2026 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

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

  • Fix [java] Casts to boolean and boolean equality are not folded as constant expressions #7313
  • Related [java] Fix #7134, #7135: UnnecessaryBoxing: check whether another overload is applicable without the unboxing #7136

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)

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@saryn17
[java] Fix pmd#7313: Fold casts to boolean and boolean equality
16a0bb1
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 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

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)

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@saryn17 saryn17 mentioned this pull request Nov 11, 2026
[java] Fix #7134, #7135: UnnecessaryBoxing: check whether another overload is applicable without the unboxing #7136
Draft
3 of 4 tasks
UncleOwen
UncleOwen reviewed Nov 13, 2026
View reviewed changes
Comment thread pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ConstantFolder.java Resolved

Uh oh!

There was an error while loading. Please reload this page.

@saryn17
[java] Test boxed Boolean locals in constant folding
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 approved these changes Nov 14, 2026
View reviewed changes

@UncleOwen UncleOwen left a comment

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

Thanks!

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@UncleOwen
UncleOwen merged commit f7b7388 into pmd:main Nov 14, 2026
13 checks passed

Uh oh!

There was an error while loading. Please reload this page.

@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
@saryn17
[java] UnnecessaryBoxing: enable the test case for pmd#7313
14cedf4
pmd#7313 was fixed by pmd#7314.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@UncleOwen UncleOwen UncleOwen approved these changes

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

None yet

Projects

None yet

Milestone

7.30.0

Development

Successfully merging this pull request may close these issues.

[java] Casts to boolean and boolean equality are not folded as constant expressions

Uh oh!

There was an error while loading. Please reload this page.

3 participants

@saryn17 @UncleOwen @adangel
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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.