UnnecessarySemicolonInTryWithResources

Since Checkstyle 8.22

Description

Checks if unnecessary semicolon is used in last resource declaration.

Properties

name description type default value since
allowWhenNoBraceAfterSemicolon Allow unnecessary semicolon if closing parenthesis is not on the same line. boolean true 8.22

Examples

To configure the check:

<module name="Checker">
  <module name="TreeWalker">
    <module name="UnnecessarySemicolonInTryWithResources"/>
  </module>
</module>
        

Example of violations

class Example1 {
  void method() throws IOException {
    try (Reader r1 = new PipedReader();) {} // violation, 'Unnecessary semicolon'
    try (Reader r4 = new PipedReader(); Reader r5 = new PipedReader()
         ;) {} // violation, 'Unnecessary semicolon'
    try (Reader r6 = new PipedReader();
         Reader r7
           = new PipedReader(); // ok, closing parenthesis is not on the same line
    ) {}
  }
}
        

To configure the check to detect unnecessary semicolon if closing parenthesis is not on the same line

<module name="Checker">
  <module name="TreeWalker">
    <module name="UnnecessarySemicolonInTryWithResources">
      <property name="allowWhenNoBraceAfterSemicolon" value="false"/>
    </module>
  </module>
</module>
        

Example of exclusion

class Example2 {
  void method() throws IOException {
    try (Reader r1 = new PipedReader();) {} // violation, 'Unnecessary semicolon'
    try (Reader r4 = new PipedReader(); Reader r5 = new PipedReader()
         ;) {} // violation, 'Unnecessary semicolon'
    try (Reader r6 = new PipedReader();
         Reader r7
           = new PipedReader(); // violation, 'Unnecessary semicolon'
    ) {}
  }
}
        

Example of Usage

Violation Messages

All messages can be customized if the default message doesn't suit you. Please see the documentation to learn how to.

Package

com.puppycrawl.tools.checkstyle.checks.coding

Parent Module

TreeWalker