Skip to content

[java] Parse error with yield lambda inside switch #6364

Closed

Description

@kelunik

Affects PMD Version:

PMD 7.20.0 (fa478ec, 2025-12-30T14:37:52Z)

Description:

Parsing code fails while it is valid Java.

Exception Stacktrace:

[WARN] This analysis could be faster, please consider using Incremental Analysis: https://docs.pmd-code.org/pmd-doc-7.20.0/pmd_userdocs_incremental_analysis.html
../tools/build/Lambda.java	-	ParseException: Parse exception in file '../tools/build/Lambda.java' at line 6, column 27: Encountered <IDENTIFIER: "a">.
Was expecting one of:
    "instanceof" ...
    ")" ...
    "," ...
    "=" ...
    "<" ...
    "?" ...
    "==" ...
    "<=" ...
    ">=" ...
    "!=" ...
    "||" ...
    "&&" ...
    "++" ...
    "--" ...
    "+" ...
    "-" ...
    "*" ...
    "/" ...
    "&" ...
    "|" ...
    "^" ...
    "%" ...
    "+=" ...
    "-=" ...
    "*=" ...
    "/=" ...
    "&=" ...
    "|=" ...
    "^=" ...
    "%=" ...
    "<<=" ...
    ">>=" ...
    ">>>=" ...
    ">" ...
[INFO] An error occurred while executing PMD.

Code Sample demonstrating the issue:

public class Lambda {

   public static void main( String[] args ) {
      foo(switch ( args[0] ) {
         default:
            yield (Lambda a, Lambda b) -> "hello";
      });
   }

   private static void foo(java.util.function.BiFunction<Lambda, Lambda, String> f)  {}        
}

Rules

<?xml version="1.0"?>

<ruleset name="Custom Rules"
    xmlns="http://pmd.sourceforge.net/ruleset/2.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://pmd.sourceforge.net/ruleset/2.0.0 https://pmd.sourceforge.io/ruleset_2_0_0.xsd">

    <description>
        Dead code
    </description>

    <rule ref="category/java/bestpractices.xml/UnusedPrivateMethod">
        <properties>
            <property name="ignoredAnnotations" value="jakarta.annotation.PostConstruct,jakarta.annotation.PreDestroy" />
        </properties>
    </rule>

</ruleset>

Steps to reproduce:

Run pmd check -f text -R deadcode.xml -d testdir. Running on Java 25.

Having a single parameter in the lambda does not trigger the parse error.

Activity

  1. added
    a:bug
    on Dec 30, 2025
  2. adangel commented on Dec 30, 2025

    @adangel
    Member

    The code sample does not compile with java:

    Lambda.java:4: error: no suitable method found for println(<none>)
          System.out.println(switch ( args[0] ) {
                    ^
        method PrintStream.println() is not applicable
          (actual and formal argument lists differ in length)
        method PrintStream.println(boolean) is not applicable
          (argument mismatch; boolean is not a functional interface)
        method PrintStream.println(char) is not applicable
          (argument mismatch; char is not a functional interface)
        method PrintStream.println(int) is not applicable
          (argument mismatch; int is not a functional interface)
        method PrintStream.println(long) is not applicable
          (argument mismatch; long is not a functional interface)
        method PrintStream.println(float) is not applicable
          (argument mismatch; float is not a functional interface)
        method PrintStream.println(double) is not applicable
          (argument mismatch; double is not a functional interface)
        method PrintStream.println(char[]) is not applicable
          (argument mismatch; Array is not a functional interface)
        method PrintStream.println(String) is not applicable
          (argument mismatch; String is not a functional interface)
        method PrintStream.println(Object) is not applicable
          (argument mismatch; Object is not a functional interface)
    1 error
    error: compilation failed
    

    you are yielding a lambda from the switch expression, which is not a suitable argument for println...

    Can you make an example, that compiles?

  3. kelunik commented on Jan 2, 2026

    @kelunik
    ContributorAuthor

    Oops, I've updated the example.

  4. added
    in:grammar
    on Jan 5, 2026
  5. self-assigned this
    on Jan 8, 2026
  6. added a commit that references this issue on Jan 8, 2026
    3e7d559
  7. added this to the 7.21.0 milestone on Jan 8, 2026
  8. changed the title [-][java] Failure to parse lambda expression with multiple parameters inside switch expression[/-] [+][java] Parse error with yield lambda inside switch[/+] on Jan 29, 2026
  9. added 2 commits that reference this issue on Jan 29, 2026
  10. added a commit that references this issue on Feb 10, 2026
    6a1959a
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

a:bugin:grammar

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions