From 93ee62e11dc0c6d721ca8517c55f80ed5e63e52e Mon Sep 17 00:00:00 2001 From: Erik Silkensen <94125+esilkensen@users.noreply.github.com> Date: Fri, 15 Feb 2019 23:27:25 -0700 Subject: [PATCH] Issue #6301: ArrayTypeStyle support method definitions --- .../rule4832nocstylearray/ArrayTypeStyleTest.java | 5 +++-- .../InputArrayTypeStyle.java | 7 ++++++- .../checkstyle/checks/ArrayTypeStyleCheck.java | 15 ++++++++------- .../checks/ArrayTypeStyleCheckTest.java | 2 ++ .../arraytypestyle/InputArrayTypeStyle.java | 7 ++++++- 5 files changed, 25 insertions(+), 11 deletions(-) diff --git a/src/it/java/com/google/checkstyle/test/chapter4formatting/rule4832nocstylearray/ArrayTypeStyleTest.java b/src/it/java/com/google/checkstyle/test/chapter4formatting/rule4832nocstylearray/ArrayTypeStyleTest.java index 2124ee91be45..15796322f637 100644 --- a/src/it/java/com/google/checkstyle/test/chapter4formatting/rule4832nocstylearray/ArrayTypeStyleTest.java +++ b/src/it/java/com/google/checkstyle/test/chapter4formatting/rule4832nocstylearray/ArrayTypeStyleTest.java @@ -41,8 +41,9 @@ public void testArrayTypeStyle() throws Exception { "15:44: " + getCheckMessage(ArrayTypeStyleCheck.class, MSG_KEY), "21:20: " + getCheckMessage(ArrayTypeStyleCheck.class, MSG_KEY), "22:23: " + getCheckMessage(ArrayTypeStyleCheck.class, MSG_KEY), - "41:16: " + getCheckMessage(ArrayTypeStyleCheck.class, MSG_KEY), - "42:19: " + getCheckMessage(ArrayTypeStyleCheck.class, MSG_KEY), + "41:33: " + getCheckMessage(ArrayTypeStyleCheck.class, MSG_KEY), + "46:16: " + getCheckMessage(ArrayTypeStyleCheck.class, MSG_KEY), + "47:19: " + getCheckMessage(ArrayTypeStyleCheck.class, MSG_KEY), }; final Configuration checkConfig = getModuleConfig("ArrayTypeStyle"); diff --git a/src/it/resources/com/google/checkstyle/test/chapter4formatting/rule4832nocstylearray/InputArrayTypeStyle.java b/src/it/resources/com/google/checkstyle/test/chapter4formatting/rule4832nocstylearray/InputArrayTypeStyle.java index ed2bb0690590..82210605b1cf 100644 --- a/src/it/resources/com/google/checkstyle/test/chapter4formatting/rule4832nocstylearray/InputArrayTypeStyle.java +++ b/src/it/resources/com/google/checkstyle/test/chapter4formatting/rule4832nocstylearray/InputArrayTypeStyle.java @@ -29,7 +29,7 @@ public class Test public Test[] getTests() - { // we shouldn't check methods because there is no alternatives. + { return null; } @@ -37,6 +37,11 @@ public Test[] getNewTest() //ok { return null; } + + public Test getOldTest()[] //warn + { + return null; + } } int[] array[] = new int [2][2]; //warn int array2[][][] = new int[3][3][3]; //warn diff --git a/src/main/java/com/puppycrawl/tools/checkstyle/checks/ArrayTypeStyleCheck.java b/src/main/java/com/puppycrawl/tools/checkstyle/checks/ArrayTypeStyleCheck.java index 17523ce6771d..60fe7657967d 100644 --- a/src/main/java/com/puppycrawl/tools/checkstyle/checks/ArrayTypeStyleCheck.java +++ b/src/main/java/com/puppycrawl/tools/checkstyle/checks/ArrayTypeStyleCheck.java @@ -61,17 +61,18 @@ public int[] getRequiredTokens() { @Override public void visitToken(DetailAST ast) { final DetailAST typeAST = ast.getParent(); - if (typeAST.getType() == TokenTypes.TYPE - // Do not check method's return type. - // We have no alternatives here. - && typeAST.getParent().getType() != TokenTypes.METHOD_DEF) { + if (typeAST.getType() == TokenTypes.TYPE) { final DetailAST variableAST = typeAST.getNextSibling(); if (variableAST != null) { - final boolean isJavaStyle = - variableAST.getLineNo() > ast.getLineNo() + final boolean isMethod = typeAST.getParent().getType() == TokenTypes.METHOD_DEF; + final boolean isJavaStyle = variableAST.getLineNo() > ast.getLineNo() || variableAST.getColumnNo() - ast.getColumnNo() > -1; - if (isJavaStyle != javaStyle) { + // force all methods to be Java style (C functions can't return arrays) + final boolean isMethodViolation = isMethod && !isJavaStyle; + final boolean isVariableViolation = !isMethod && isJavaStyle != javaStyle; + + if (isMethodViolation || isVariableViolation) { log(ast, MSG_KEY); } } diff --git a/src/test/java/com/puppycrawl/tools/checkstyle/checks/ArrayTypeStyleCheckTest.java b/src/test/java/com/puppycrawl/tools/checkstyle/checks/ArrayTypeStyleCheckTest.java index fcada1c91b28..f2a3aa06895a 100644 --- a/src/test/java/com/puppycrawl/tools/checkstyle/checks/ArrayTypeStyleCheckTest.java +++ b/src/test/java/com/puppycrawl/tools/checkstyle/checks/ArrayTypeStyleCheckTest.java @@ -54,6 +54,7 @@ public void testJavaStyleOn() "14:23: " + getCheckMessage(MSG_KEY), "15:18: " + getCheckMessage(MSG_KEY), "21:44: " + getCheckMessage(MSG_KEY), + "45:33: " + getCheckMessage(MSG_KEY), }; verify(checkConfig, getPath("InputArrayTypeStyle.java"), expected); } @@ -69,6 +70,7 @@ public void testJavaStyleOff() "17:39: " + getCheckMessage(MSG_KEY), "23:18: " + getCheckMessage(MSG_KEY), "31:20: " + getCheckMessage(MSG_KEY), + "45:33: " + getCheckMessage(MSG_KEY), }; verify(checkConfig, getPath("InputArrayTypeStyle.java"), expected); } diff --git a/src/test/resources/com/puppycrawl/tools/checkstyle/checks/arraytypestyle/InputArrayTypeStyle.java b/src/test/resources/com/puppycrawl/tools/checkstyle/checks/arraytypestyle/InputArrayTypeStyle.java index df738b2bcc87..25a231229230 100644 --- a/src/test/resources/com/puppycrawl/tools/checkstyle/checks/arraytypestyle/InputArrayTypeStyle.java +++ b/src/test/resources/com/puppycrawl/tools/checkstyle/checks/arraytypestyle/InputArrayTypeStyle.java @@ -33,7 +33,7 @@ public class Test public Test[] getTests() - { // we shouldn't check methods because there is no alternatives. + { return null; } @@ -41,6 +41,11 @@ public Test[] getNewTest() { return null; } + + public Test getOldTest()[] + { + return null; + } } public static void foo(java.util.Collection < ? extends InputArrayTypeStyle[] > collection) {} }