diff --git a/src/main/java/com/puppycrawl/tools/checkstyle/checks/coding/OneStatementPerLineCheck.java b/src/main/java/com/puppycrawl/tools/checkstyle/checks/coding/OneStatementPerLineCheck.java index d613fefff..c1b991179 100644 --- a/src/main/java/com/puppycrawl/tools/checkstyle/checks/coding/OneStatementPerLineCheck.java +++ b/src/main/java/com/puppycrawl/tools/checkstyle/checks/coding/OneStatementPerLineCheck.java @@ -117,7 +117,10 @@ public final class OneStatementPerLineCheck extends Check { switch (ast.getType()) { case TokenTypes.SEMI: DetailAST currentStatement = ast; - if (isMultilineStatement(currentStatement)) { + final boolean hasResourcesPrevSibling = + currentStatement.getPreviousSibling() != null + && currentStatement.getPreviousSibling().getType() == TokenTypes.RESOURCES; + if (!hasResourcesPrevSibling && isMultilineStatement(currentStatement)) { currentStatement = ast.getPreviousSibling(); } if (isOnTheSameLine(currentStatement, lastStatementEnd, diff --git a/src/test/java/com/puppycrawl/tools/checkstyle/checks/coding/OneStatementPerLineCheckTest.java b/src/test/java/com/puppycrawl/tools/checkstyle/checks/coding/OneStatementPerLineCheckTest.java index 36919eaa2..b7428855d 100644 --- a/src/test/java/com/puppycrawl/tools/checkstyle/checks/coding/OneStatementPerLineCheckTest.java +++ b/src/test/java/com/puppycrawl/tools/checkstyle/checks/coding/OneStatementPerLineCheckTest.java @@ -79,6 +79,8 @@ public class OneStatementPerLineCheckTest extends BaseCheckTestSupport { "81:10: " + getCheckMessage(MSG_KEY), "90:28: " + getCheckMessage(MSG_KEY), "135:39: " + getCheckMessage(MSG_KEY), + "168:110: " + getCheckMessage(MSG_KEY), + "179:107: " + getCheckMessage(MSG_KEY), }; verify(checkConfig, diff --git a/src/test/resources/com/puppycrawl/tools/checkstyle/checks/coding/InputOneStatementPerLine2.java b/src/test/resources/com/puppycrawl/tools/checkstyle/checks/coding/InputOneStatementPerLine2.java index 9bbd91ff5..161dcdf89 100644 --- a/src/test/resources/com/puppycrawl/tools/checkstyle/checks/coding/InputOneStatementPerLine2.java +++ b/src/test/resources/com/puppycrawl/tools/checkstyle/checks/coding/InputOneStatementPerLine2.java @@ -134,4 +134,50 @@ public class InputOneStatementPerLine2 { ; n++, k--) { var1++; var2++; } } + + /** + * Multiple statements within try-with-resource on a separate line is legal. + * @see OneStatementPerLine: false match with try-with-resources + */ + private void issue2211pass() { + try( + AutoCloseable i = new java.io.StringReader(""); + AutoCloseable k = new java.io.StringReader(""); + ) { + } catch (Exception e1) { + } + } + + /** + * Multiple statements within try-with-resource on a separate line is legal. Added per PR comment: + * @see Please add UT cases + */ + private void issue2211pass2() { + try( AutoCloseable i = new java.io.StringReader(""); + AutoCloseable k = new java.io.StringReader("");) { + } catch (Exception e1) { + } + } + + /** + * Multiple statements within try-with-resource on next line after try is illegal. + * @see OneStatementPerLine: false match with try-with-resources + */ + private void issue2211fail() { + try( + AutoCloseable i = new java.io.StringReader("");AutoCloseable k = new java.io.StringReader(""); + ) { + } catch (Exception e1) { + } + } + + /** + * Multiple statements within try-with-resource on a same line as try is illegal. Added per PR comment: + * @see Please add UT cases + */ + private void issue2211fail2() { + try( AutoCloseable i = new java.io.StringReader("");AutoCloseable k = new java.io.StringReader("");) { + } catch (Exception e1) { + } + } }