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

Suggestion from StatementSwitchToExpressionSwitch's "direct conversion" could be better if case already has braces #4222

Closed
PhilippWendler opened this issue Dec 22, 2023 · 0 comments
Assignees

Comments

@PhilippWendler
Copy link

PhilippWendler commented Dec 22, 2023

On Error Prone 2.24.0 with -Xep:StatementSwitchToExpressionSwitch -XepOpt:StatementSwitchToExpressionSwitch:EnableDirectConversion=true -XepPatchChecks:StatementSwitchToExpressionSwitch -XepPatchLocation:... and the code

public class Test {

  public static void main(String[] args) {
    switch (args.length) {
      case 0:
        {
          System.out.println("0");
          break;
        }
      case 1:
        System.out.println("1");
        break;
      case 2:
        System.out.println("2");
        System.out.println("2");
        break;
    }
  }
}

the suggestion from Error Prone is the following (after formatting):

public class Test {

  public static void main(String[] args) {
    switch (args.length) {
      case 0 -> {
        {
          System.out.println("0");
          break;
        }
      }
      case 1 -> System.out.println("1");
      case 2 -> {
        System.out.println("2");
        System.out.println("2");
      }
    }
  }
}

For case 1 and case 2 this is nice.
For case 0 (which had already braces before) it adds redundant braces and it fails to remove the now unnecessary break;. For converting a large code base it would be nice if the output could be improved in this regard.

@cushon cushon self-assigned this Dec 24, 2023
copybara-service bot pushed a commit that referenced this issue Jan 4, 2024
If a case contains a single block statement, skip past the braces of the block statement.

Enables fixes like:

```
      case 0 -> System.out.println("0");
```

Instead of:

```
      case 0 -> {
        {
          System.out.println("0");
          break;
        }
      }
```

Fixes #4222

PiperOrigin-RevId: 593453909
copybara-service bot pushed a commit that referenced this issue Jan 4, 2024
If a case contains a single block statement, skip past the braces of the block statement.

Enables fixes like:

```
      case 0 -> System.out.println("0");
```

Instead of:

```
      case 0 -> {
        {
          System.out.println("0");
          break;
        }
      }
```

Fixes #4222

PiperOrigin-RevId: 593453909
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.

2 participants