Skip to content

LambdaIsMethodReference mutator could introduce side effects #847

Open
@mches

Description

An expression lambda like s -> b.append("/").append(s) can't be converted to a method reference because b.append("/") has a side effect

String a = null;
StringBuilder b = new StringBuilder();
Optional.ofNullable(a).ifPresent(s -> b.append("/").append(s));
System.out.println(b);

Output:


↓↓↓

String a = null;
StringBuilder b = new StringBuilder();
Optional.ofNullable(a).ifPresent(b.append("/")::append);
System.out.println(b);

Output:

/

Seems safer to assume a call chain produces a side effect and give up on converting such a lambda expression to method reference.

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions