Skip to content

ReplaceUnusedVariablesWithUnderscore produces invalid syntax for typed lambda parameters #1080

@protocol7

Description

@protocol7

What version of OpenRewrite are you using?

  • rewrite-core 8.81.1
  • rewrite-migrate-java 3.34.0

What is the smallest, simplest way to reproduce the problem?

@Test
void typedLambdaParameterRenamedToUnderscoreNeedsParentheses() {
  rewriteRun(
      spec -> spec.recipe(new ReplaceUnusedVariablesWithUnderscore()),
      java(
          """
          package com.helloworld;

          public class Main {
            interface Handler {
              String handle(RequestContext context);
            }

            static class RequestContext {}

            Handler route() {
              return (RequestContext rc) -> {
                return "result";
              };
            }
          }
          """,
          spec -> spec.markers(javaVersion(25))));
}

What did you expect to see?

The explicitly typed lambda parameter should remain parenthesized:

Handler route() {
  return (RequestContext _) -> {
    return "result";
  };
}

What did you see instead?

The recipe emits invalid Java:

Handler route() {
  return RequestContext _ -> {
    return "result";
  };
}

What is the full stack trace of any errors you encountered?

The invalid output later failed Java formatting/parsing with errors like:

error: ')' or ',' expected
              RequestContext _ -> {
                            ^
error: not a statement
              RequestContext _ -> {
                             ^

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    Status

    Done

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions