Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Comments are indented before case statements in enhanced switch #876

Closed
sKwil opened this issue Jan 3, 2023 · 5 comments · Fixed by #1112
Closed

Comments are indented before case statements in enhanced switch #876

sKwil opened this issue Jan 3, 2023 · 5 comments · Fixed by #1112

Comments

@sKwil
Copy link

sKwil commented Jan 3, 2023

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");
    }
  }
}
@kevinb9n
Copy link
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.

@cushon
Copy link
Collaborator

cushon commented Jan 28, 2023

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.

@kevinb9n
Copy link
Contributor

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

@cushon
Copy link
Collaborator

cushon commented Jan 30, 2023

(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

@tbroyer
Copy link
Contributor

tbroyer commented Jan 30, 2023

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
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants