Skip to content

Wrapped try-with-resources resource declaration gets unnecessary added semicolon #750

@ZachCallear

Description

@ZachCallear

Prettier-Java 2.7.3

# Options (if any):
(no options)

Input:

  public static void formatTest() throws IOException {
    try (var object = new ByteArrayOutputStream()) {
      var test = 1;
    }
    try (
      var objectWithALongerNameThatCausesABreak = new ByteArrayOutputStream()
    ) {
      var test = 1;
    }
  }

Output:

  public static void formatTest() throws IOException {
    try (var object = new ByteArrayOutputStream()) {
      var test = 1;
    }
    try (
      var objectWithALongerNameThatCausesABreak = new ByteArrayOutputStream();
    ) {
      var test = 1;
    }
  }

Expected behavior:
The semicolon at the end of the resource declaration statement is unnecessary. The "Unnecessary semicolon" inspection in IDEA reports it as a Weak Warning, with no option to disable the inspection for just this particular case. The formatter should, at minimum, probably not add a semicolon in this context. Whether it ought to remove an existing one is more debatable.

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