diff --git a/pom.xml b/pom.xml index 7761d689a..820300eec 100644 --- a/pom.xml +++ b/pom.xml @@ -1091,7 +1091,6 @@ .*.checks.coding.DeclarationOrderCheck8293 .*.checks.coding.IllegalInstantiationCheck8197 - .*.checks.coding.ReturnCountCheck5574 .*.checks.coding.VariableDeclarationUsageDistanceCheck9098 .*.checks.header.AbstractHeaderCheck9087 diff --git a/src/main/java/com/puppycrawl/tools/checkstyle/checks/coding/ReturnCountCheck.java b/src/main/java/com/puppycrawl/tools/checkstyle/checks/coding/ReturnCountCheck.java index 357973b81..842270f26 100644 --- a/src/main/java/com/puppycrawl/tools/checkstyle/checks/coding/ReturnCountCheck.java +++ b/src/main/java/com/puppycrawl/tools/checkstyle/checks/coding/ReturnCountCheck.java @@ -152,8 +152,8 @@ public final class ReturnCountCheck extends AbstractFormatCheck { private void visitMethodDef(DetailAST ast) { contextStack.push(context); final DetailAST methodNameAST = ast.findFirstToken(TokenTypes.IDENT); - context = - new Context(!getRegexp().matcher(methodNameAST.getText()).find()); + final boolean check = !getRegexp().matcher(methodNameAST.getText()).find(); + context = new Context(check); } /** diff --git a/src/test/java/com/puppycrawl/tools/checkstyle/checks/coding/ReturnCountCheckTest.java b/src/test/java/com/puppycrawl/tools/checkstyle/checks/coding/ReturnCountCheckTest.java index 175f68e75..32b4a6b20 100644 --- a/src/test/java/com/puppycrawl/tools/checkstyle/checks/coding/ReturnCountCheckTest.java +++ b/src/test/java/com/puppycrawl/tools/checkstyle/checks/coding/ReturnCountCheckTest.java @@ -23,10 +23,13 @@ import static com.puppycrawl.tools.checkstyle.checks.coding.ReturnCountCheck.MSG import java.io.File; +import org.junit.Assert; import org.junit.Test; import com.puppycrawl.tools.checkstyle.BaseCheckTestSupport; import com.puppycrawl.tools.checkstyle.DefaultConfiguration; +import com.puppycrawl.tools.checkstyle.api.DetailAST; +import com.puppycrawl.tools.checkstyle.api.TokenTypes; public class ReturnCountCheckTest extends BaseCheckTestSupport { @Test @@ -102,4 +105,28 @@ public class ReturnCountCheckTest extends BaseCheckTestSupport { verify(checkConfig, new File("src/test/resources-noncompilable/com/puppycrawl/tools/" + "checkstyle/coding/InputReturnCountLambda.java").getCanonicalPath(), expected); } + + @Test + public void testImproperToken() throws Exception { + ReturnCountCheck check = new ReturnCountCheck(); + + DetailAST classDefAst = new DetailAST(); + classDefAst.setType(TokenTypes.CLASS_DEF); + + try { + check.visitToken(classDefAst); + Assert.fail(); + } + catch (IllegalStateException e) { + // it is OK + } + + try { + check.leaveToken(classDefAst); + Assert.fail(); + } + catch (IllegalStateException e) { + // it is OK + } + } }