Skip to content

Do not hug a lambda on the first line of an if/while/for/switch/try header #181

Open

Description

@abashev

Upstream: google/google-java-format#1507
Kind: idea | Priority: low

google-java-format#1507 (merged 2026-10-29, copybara export) stops hugging block lambdas that start on the first line of a statement header followed by a body (if, while, for, enhanced for, switch, synchronized, try-with-resources). Otherwise the lambda body lands at the same indent as the statement body and the closing })) { hides where the header ends. Expression lambdas break before the parameter instead. Hugging still applies on continuation lines of the header and in do-while conditions. Upstream adds a zero-width HeaderMark to its layout IR for this.

Why it matters: it is the third recent hugging change after google-java-format#1489 and google-java-format#1499, and the only one that targets the if (x.anyMatch(c -> { readability problem. Our palantir-derived engine formats lambdas differently, so the question is whether we have this problem at all.

Affected files in our repo:

  • open-java-format/src/main/java/com/palantir/javaformat/java/JavaInputAstVisitor.java (lambda and argument handling)
  • open-java-format/src/test/resources/com/palantir/javaformat/java/testdata/palantir-chains-lambdas.input and .output

Suggested action: watch / investigate. Run a block lambda inside an if condition through our formatter; adopt only if the body indent equals the statement body indent. Examples: not available - the upstream golden diff was not usable to derive our output.


Filed from an upstream-watch ticket after manual review.

Activity

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

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions