Skip to content

Comments are indented before case statements in enhanced switch #876

Description

@sKwil

Placing single-line or block comments before case statements in an enhanced switch causes them to be indented. This seems to be in violation of 4.8.6.1 Block comment style.

Auto-formatted code:

public class T {
  public static void main(String[] args) {
    int a = 1;
    switch (a) {
      case 1 -> System.out.println("1");

        // This is a comment before a case statement
      case 2 -> System.out.println("2");

        /*
         * Block comment before a case stement
         */
      case 3 -> System.out.println("3");

        // Comment before the default statement
      default -> System.out.println("N/A");
    }
  }
}

Activity

  1. kevinb9n commented on Jan 25, 2023

    @kevinb9n
    Contributor

    +1 As a general rule I would expect a line containing only a comment to have the same indentation as what comes after it.

  2. cushon commented on Jan 28, 2023

    @cushon
    Collaborator

    I remember some discussion about line comments in non--> switches, where it's a little harder to know what the comment was supposed to be attached to, and it might be part of the preceding statement group:

    case 1:
      doSomething();
      // fall through
    case 2:
    

    ... but that's just context for why we probably ended up with the current behaviour, I agree with revisiting this for -> switches.

  3. kevinb9n commented on Jan 30, 2023

    @kevinb9n
    Contributor

    imho, even the fall through comment isn't conceptually attached to either group, but to the boundary between.

  4. cushon commented on Jan 30, 2023

    @cushon
    Collaborator

    (The internal bug is b/21900318)

    // fall through might not be the best example for a comment that was conceptually attached to the preceding statement group

  5. tbroyer commented on Jan 30, 2023

    @tbroyer
    Contributor

    Fwiw, I'd say // fall through is kind of a special case in that it somehow replaces a break, so it's more at a statement level rather than "attached" to any group; as a statement (kinda), it'd however put it into the preceding statement group.

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