From 1e6dc8f4eab264568db73a663e9c0d9528d2ffe1 Mon Sep 17 00:00:00 2001 From: liscju Date: Mon, 20 Jul 2015 14:27:44 +0200 Subject: [PATCH] Fix RightCurlyCheck with same option not to rise expression in single-line blocks - issue #1416 --- .../LeftCurlyRightCurlyTest.java | 13 +++++++++++++ .../RightCurlyInputSame.java | 9 +++++++++ .../checks/blocks/RightCurlyCheck.java | 4 +++- .../checks/blocks/RightCurlyCheckTest.java | 14 ++++++++------ .../InputRightCurlySameForOneLiners.java | 18 ++++++++++++++++++ 5 files changed, 51 insertions(+), 7 deletions(-) create mode 100644 src/it/resources/com/google/checkstyle/test/chapter4formatting/rule412nonemptyblocks/RightCurlyInputSame.java create mode 100644 src/test/resources/com/puppycrawl/tools/checkstyle/InputRightCurlySameForOneLiners.java diff --git a/src/it/java/com/google/checkstyle/test/chapter4formatting/rule412nonemptyblocks/LeftCurlyRightCurlyTest.java b/src/it/java/com/google/checkstyle/test/chapter4formatting/rule412nonemptyblocks/LeftCurlyRightCurlyTest.java index 71dea5d65..1697c0c34 100644 --- a/src/it/java/com/google/checkstyle/test/chapter4formatting/rule412nonemptyblocks/LeftCurlyRightCurlyTest.java +++ b/src/it/java/com/google/checkstyle/test/chapter4formatting/rule412nonemptyblocks/LeftCurlyRightCurlyTest.java @@ -112,4 +112,17 @@ public class LeftCurlyRightCurlyTest extends BaseCheckTestSupport { Integer[] warnList = builder.getLinesWithWarn(filePath); verify(newCheckConfig, filePath, expected, warnList); } + + @Test + public void rightCurlyTestSame() throws Exception { + DefaultConfiguration newCheckConfig = createCheckConfig(RightCurlyCheck.class); + newCheckConfig.addAttribute("option", RightCurlyOption.SAME.toString()); + + final String[] expected = { + }; + + String filePath = builder.getFilePath("RightCurlyInputSame"); + Integer[] warnList = builder.getLinesWithWarn(filePath); + verify(newCheckConfig, filePath, expected, warnList); + } } diff --git a/src/it/resources/com/google/checkstyle/test/chapter4formatting/rule412nonemptyblocks/RightCurlyInputSame.java b/src/it/resources/com/google/checkstyle/test/chapter4formatting/rule412nonemptyblocks/RightCurlyInputSame.java new file mode 100644 index 000000000..a4dae053d --- /dev/null +++ b/src/it/resources/com/google/checkstyle/test/chapter4formatting/rule412nonemptyblocks/RightCurlyInputSame.java @@ -0,0 +1,9 @@ +package com.google.checkstyle.test.chapter4formatting.rule412nonemptyblocks; + +public class RightCurlyInputSame { + public static void main(String[] args) { + boolean after = false; + try { + } finally { after = true; } + } +} diff --git a/src/main/java/com/puppycrawl/tools/checkstyle/checks/blocks/RightCurlyCheck.java b/src/main/java/com/puppycrawl/tools/checkstyle/checks/blocks/RightCurlyCheck.java index b18f63160..5fde8bf13 100644 --- a/src/main/java/com/puppycrawl/tools/checkstyle/checks/blocks/RightCurlyCheck.java +++ b/src/main/java/com/puppycrawl/tools/checkstyle/checks/blocks/RightCurlyCheck.java @@ -78,6 +78,7 @@ import com.puppycrawl.tools.checkstyle.checks.CheckUtils; * @author o_sukhodolsky * @author maxvetrenko * @author Andrei Selkin + * @author liscju */ public class RightCurlyCheck extends AbstractOptionCheck { /** @@ -199,7 +200,8 @@ public class RightCurlyCheck extends AbstractOptionCheck { String violation = ""; if (bracePolicy == RightCurlyOption.SAME - && !hasLineBreakBefore(rcurly)) { + && !hasLineBreakBefore(rcurly) + && lcurly.getLineNo() != rcurly.getLineNo()) { violation = MSG_KEY_LINE_BREAK_BEFORE; } else if (shouldCheckLastRcurly) { diff --git a/src/test/java/com/puppycrawl/tools/checkstyle/checks/blocks/RightCurlyCheckTest.java b/src/test/java/com/puppycrawl/tools/checkstyle/checks/blocks/RightCurlyCheckTest.java index 4141acaf4..31318b2fc 100644 --- a/src/test/java/com/puppycrawl/tools/checkstyle/checks/blocks/RightCurlyCheckTest.java +++ b/src/test/java/com/puppycrawl/tools/checkstyle/checks/blocks/RightCurlyCheckTest.java @@ -46,7 +46,6 @@ public class RightCurlyCheckTest extends BaseCheckTestSupport { "40:13: " + getCheckMessage(MSG_KEY_LINE_SAME, "}", 13), "44:13: " + getCheckMessage(MSG_KEY_LINE_SAME, "}", 13), "93:27: " + getCheckMessage(MSG_KEY_LINE_BREAK_BEFORE, "}", 27), - "97:54: " + getCheckMessage(MSG_KEY_LINE_BREAK_BEFORE, "}", 54), }; verify(checkConfig, getPath("InputLeftCurlyOther.java"), expected); } @@ -60,11 +59,18 @@ public class RightCurlyCheckTest extends BaseCheckTestSupport { "40:13: " + getCheckMessage(MSG_KEY_LINE_SAME, "}", 13), "44:13: " + getCheckMessage(MSG_KEY_LINE_SAME, "}", 13), "93:27: " + getCheckMessage(MSG_KEY_LINE_BREAK_BEFORE, "}", 27), - "97:54: " + getCheckMessage(MSG_KEY_LINE_BREAK_BEFORE, "}", 54), }; verify(checkConfig, getPath("InputLeftCurlyOther.java"), expected); } + @Test + public void testSameOmitOneLiners() throws Exception { + checkConfig.addAttribute("option", RightCurlyOption.SAME.toString()); + final String[] expected = { + }; + verify(checkConfig, getPath("InputRightCurlySameForOneLiners.java"), expected); + } + @Test public void testAlone() throws Exception { checkConfig.addAttribute("option", RightCurlyOption.ALONE.toString()); @@ -125,10 +131,6 @@ public class RightCurlyCheckTest extends BaseCheckTestSupport { @Test public void testForceLineBreakBefore2() throws Exception { final String[] expected = { - "24:33: " + getCheckMessage(MSG_KEY_LINE_BREAK_BEFORE, "}", 33), - "32:44: " + getCheckMessage(MSG_KEY_LINE_BREAK_BEFORE, "}", 44), - "32:63: " + getCheckMessage(MSG_KEY_LINE_BREAK_BEFORE, "}", 63), - "52:48: " + getCheckMessage(MSG_KEY_LINE_BREAK_BEFORE, "}", 48), }; verify(checkConfig, getPath("InputRightCurlyLineBreakBefore.java"), expected); } diff --git a/src/test/resources/com/puppycrawl/tools/checkstyle/InputRightCurlySameForOneLiners.java b/src/test/resources/com/puppycrawl/tools/checkstyle/InputRightCurlySameForOneLiners.java new file mode 100644 index 000000000..ed58a098b --- /dev/null +++ b/src/test/resources/com/puppycrawl/tools/checkstyle/InputRightCurlySameForOneLiners.java @@ -0,0 +1,18 @@ +//////////////////////////////////////////////////////////////////////////////// +// Test case file for checkstyle. +// Created: 2015 +//////////////////////////////////////////////////////////////////////////////// +package com.puppycrawl.tools.checkstyle; + +/** + * Test case for RightCurly with option SAME to omit oneliners + * @see https://github.com/checkstyle/checkstyle/issues/1416 + * @author liscju + */ +public class InputRightCurlySameForOneLiners { + public static void main(String[] args) { + boolean after = false; + try { + } finally { after = true; } + } +}