Skip to content

An annotation with arguments on a lambda parameter forces a line break after it #101

Description

@abashev

Formatting a lambda whose parameter carries an annotation with arguments, such as @SuppressWarnings("unused"), breaks the parameter list across lines even when the whole statement fits in 120 columns.

Input

class T {
    BiFunction<Integer, Integer, Integer> g = (final var x, @SuppressWarnings("unused") var y) -> x + y;
}

Output of main (2fbf175), the same on JDK 21, 25 and 27

class T {
    BiFunction<Integer, Integer, Integer> g =
            (final var x,
                    @SuppressWarnings("unused")
                    var y) -> x + y;
}

Expected

The input, unchanged. A lambda parameter is a parameter, and method parameters keep such an annotation on their line: void m(@SuppressWarnings("unused") int y) {} is left alone.

A marker annotation (@Deprecated var x) is already kept inline; it is the arguments that trigger the break. An explicit type instead of var, a block body or a second annotation make no difference.

Cause

JavaInputAstVisitor.visitLambdaExpression declares each parameter with inlineAnnotationDirection(parameter.getModifiers()), which answers VERTICAL for any annotation that has arguments, and a vertical annotation ends in a forced break. That rule is meant for local variable declarations, where

@SuppressWarnings("unchecked")
Function<Object, String> local = o -> (String) o;

is the wanted shape. Method parameters go through visitToDeclare(DeclarationKind.PARAMETER, Direction.HORIZONTAL, ...) instead, so their annotations stay inline.

The same code is in develop of https://github.com/palantir/palantir-java-format and in https://github.com/google/google-java-format, where the method is called fieldAnnotationDirection, so both produce this layout. I found no report of it in either tracker.

No activity

Activity on this issue will appear here.

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

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions