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] Lambda with a parenthesized expression body is treated as void-compatible · Issue #7145 · pmd/pmd

[java] Lambda with a parenthesized expression body is treated as void-compatible #7145

New issue
Copy link
New issue
Copy link
Closed
#7158
Closed
[java] Lambda with a parenthesized expression body is treated as void-compatible#7145
#7158
Copy link
Labels
in:type-resolutionAffects the type resolution code
Milestone
7.29.0

Description

@saryn17
saryn17
opened on Oct 6, 2026
Issue body actions

Affects PMD Version: 7.28.0 (also 7.29.0-SNAPSHOT)

Description:

javac rejects Runnable r = () -> (str()); with lambda body is not compatible with a void functional interface. PMD accepts the lambda for Runnable. In the sample, javac compiles line 10 to g(int, Supplier), where PMD selects g(Object, Runnable).

LambdaMirrorImpl.isExpressionStatement tests the body node with instanceof:

pmd/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/types/internal/infer/ast/LambdaMirrorImpl.java

Lines 153 to 160 in 5c9271c

private static boolean isExpressionStatement(ASTExpression body) {
// statement expression
return body instanceof ASTMethodCall
|| body instanceof ASTConstructorCall
|| body instanceof ASTAssignmentExpression
|| body instanceof ASTUnaryExpression && !((ASTUnaryExpression) body).getOperator().isPure();
}

PMD has no node for parentheses. The body of () -> (str()) is an ASTMethodCall with getParenthesisDepth() == 1, so the test passes and isVoidCompatible returns true. A parenthesized expression is not a statement expression (JLS 14.8), and an expression body is compatible with a void function type only if it is one (JLS 15.27.3). The method does not test ASTExpression.isParenthesized().

Found while working on #7136.

Code Sample demonstrating the issue:

import java.util.function.Supplier;

public class ParenBody {
    static String str() { return "s"; }

    static void g(int x, Supplier<String> s) { }
    static void g(Object x, Runnable r) { }

    void t(Integer i) {
        g(i, () -> (str()));
        g(i, () -> str());
    }
}

Steps to reproduce:

  1. javac ParenBody.java, also with --release 8, then javap -c -p ParenBody. ParenBody.t calls g:(ILjava/util/function/Supplier;)V on line 10 and g:(Ljava/lang/Object;Ljava/lang/Runnable;)V on line 11.
  2. Run PMD on the file with the XPath rule //MethodCall[pmd-java:matchesSig("ParenBody#g(java.lang.Object,java.lang.Runnable)")]. It reports lines 10 and 11. javac agrees with line 11 only.

Running PMD through: Other (PmdAnalysis API)

Activity

  1. saryn17
    added a commit that references this issue on Oct 6, 2026

    [java] UnnecessaryBoxing: rerun the overload resolution without the u…

    15327e0
  2. UncleOwen
    added this to the 7.29.0 milestone on Oct 7, 2026
  3. UncleOwen
    closed this as completedin #7158on Oct 7, 2026
  4. saryn17
    added a commit that references this issue on Oct 18, 2026

    [java] UnnecessaryBoxing: enable the test case for pmd#7145

    1aac0ac
  5. adangel
    added
    in:type-resolutionAffects the type resolution code
    on Oct 29, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    in:type-resolutionAffects the type resolution code

    Type

    No type

    Projects

    No projects

      Milestone

      • 7.29.0

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions

      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.