Skip to content

Inline Method produces invalid lambda body when inlining method call in expression lambda #4432

@zmjjcxy8

Description

@zmjjcxy8

Description

When using Inline Method in VS Code Java on a method call inside an expression lambda, the refactoring generates invalid Java code.

Steps to reproduce

  1. Create the following Java file in VS Code :
package org.example;

import java.util.function.Supplier;

public class sdad {
    // Refactoring operation: Inline Method
    static String getValue() {
        return "lambda";
    }

    public static void main(String[] args) {
        Supplier<String> s = () -> getValue(); // inline getValue here
        System.out.println(s.get());
        System.out.println("done");
    }
}
  1. Place the cursor on the invocation getValue inside the lambda expression:
Supplier<String> s = () -> getValue();
  1. Apply Inline Method.

Expected result

The method invocation inside the expression lambda should be replaced with a valid expression:

package org.example;

import java.util.function.Supplier;

public class sdad {
    // Refactoring operation: Inline Method
    static String getValue() {
        return "lambda";
    }

    public static void main(String[] args) {
        Supplier<String> s = () -> "lambda";
        System.out.println(s.get());
        System.out.println("done");
    }
}

Actual result

VS Code Java generates invalid code:

package org.example;

import java.util.function.Supplier;

public class sdad {
    // Refactoring operation: Inline Method
    static String getValue() {
        return "lambda";
    }

    public static void main(String[] args) {
        Supplier<String> s = () -> {"lambda"};
        System.out.println(s.get());
        System.out.println("done");
    }
}

The generated lambda body is:

() -> {"lambda"}

This is invalid Java syntax. A block lambda body cannot contain a bare string literal statement. Therefore, the refactored program fails to compile.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions