aboutsummaryrefslogtreecommitdiff
path: root/src/main
diff options
context:
space:
mode:
authorrnveach <rveach02@gmail.com>2016-05-15 10:49:18 -0400
committerrnveach <rveach02@gmail.com>2016-05-27 09:37:25 -0400
commit2483ddc5a4d286891da1922cadf9e66547a7ec48 (patch)
treebcd462b59816c6bcfac0100c04f014994a28db6f /src/main
parent52131dd4ed72b55a415c1f88a77ae9f9a17b75cb (diff)
downloadcheckstyle-2483ddc5a4d286891da1922cadf9e66547a7ec48.tar.gz
Issue #3143: added maxForVoid option for ReturnCountCheck
Diffstat (limited to 'src/main')
-rw-r--r--src/main/java/com/puppycrawl/tools/checkstyle/checks/coding/ReturnCountCheck.java68
1 files changed, 52 insertions, 16 deletions
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 df7090421..bfb19ae3c 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
@@ -34,6 +34,18 @@ import com.puppycrawl.tools.checkstyle.utils.CommonUtils;
* (2 by default). Ignores specified methods ({@code equals()} by default).
* </p>
* <p>
+ * <b>max</b> property will only check returns in methods and lambdas that
+ * return a specific value (Ex: 'return 1;').
+ * </p>
+ * <p>
+ * <b>maxForVoid</b> property will only check returns in methods, constructors,
+ * and lambdas that have no return type (IE 'return;'). It will only count
+ * visible return statements. Return statements not normally written, but
+ * implied, at the end of the method/constructor definition will not be taken
+ * into account. To disallow "return;" in void return type methods, use a value
+ * of 0.
+ * </p>
+ * <p>
* Rationale: Too many return points can be indication that code is
* attempting to do too much or may be difficult to understand.
* </p>
@@ -58,6 +70,8 @@ public final class ReturnCountCheck extends AbstractCheck {
/** Maximum allowed number of return statements. */
private int max = 2;
+ /** Maximum allowed number of return statements for void methods. */
+ private int maxForVoid = 1;
/** Current method context. */
private Context context;
@@ -97,14 +111,6 @@ public final class ReturnCountCheck extends AbstractCheck {
}
/**
- * Getter for max property.
- * @return maximum allowed number of return statements.
- */
- public int getMax() {
- return max;
- }
-
- /**
* Setter for max property.
* @param max maximum allowed number of return statements.
*/
@@ -112,6 +118,14 @@ public final class ReturnCountCheck extends AbstractCheck {
this.max = max;
}
+ /**
+ * Setter for maxForVoid property.
+ * @param maxForVoid maximum allowed number of return statements for void methods.
+ */
+ public void setMaxForVoid(int maxForVoid) {
+ this.maxForVoid = maxForVoid;
+ }
+
@Override
public void beginTree(DetailAST rootAST) {
context = new Context(false);
@@ -129,7 +143,7 @@ public final class ReturnCountCheck extends AbstractCheck {
visitLambda();
break;
case TokenTypes.LITERAL_RETURN:
- context.visitLiteralReturn();
+ visitReturn(ast);
break;
default:
throw new IllegalStateException(ast.toString());
@@ -181,6 +195,21 @@ public final class ReturnCountCheck extends AbstractCheck {
}
/**
+ * Examines the return statement and tells context about it.
+ * @param ast return statement to check.
+ */
+ private void visitReturn(DetailAST ast) {
+ // we can't identify which max to use for lambdas, so we can only assign
+ // after the first return statement is seen
+ if (ast.getFirstChild().getType() == TokenTypes.SEMI) {
+ context.visitLiteralReturn(maxForVoid);
+ }
+ else {
+ context.visitLiteralReturn(max);
+ }
+ }
+
+ /**
* Class to encapsulate information about one method.
* @author <a href="mailto:simon@redhillconsulting.com.au">Simon Harris</a>
*/
@@ -189,6 +218,8 @@ public final class ReturnCountCheck extends AbstractCheck {
private final boolean checking;
/** Counter for return statements. */
private int count;
+ /** Maximum allowed number of return statements. */
+ private Integer maxAllowed;
/**
* Creates new method context.
@@ -196,23 +227,28 @@ public final class ReturnCountCheck extends AbstractCheck {
*/
Context(boolean checking) {
this.checking = checking;
- count = 0;
}
- /** Increase number of return statements. */
- public void visitLiteralReturn() {
+ /**
+ * Increase the number of return statements.
+ * @param maxAssigned Maximum allowed number of return statements.
+ */
+ public void visitLiteralReturn(int maxAssigned) {
+ if (maxAllowed == null) {
+ maxAllowed = maxAssigned;
+ }
+
++count;
}
/**
- * Checks if number of return statements in method more
+ * Checks if number of return statements in the method are more
* than allowed.
* @param ast method def associated with this context.
*/
public void checkCount(DetailAST ast) {
- if (checking && count > getMax()) {
- log(ast.getLineNo(), ast.getColumnNo(), MSG_KEY,
- count, getMax());
+ if (checking && maxAllowed != null && count > maxAllowed) {
+ log(ast.getLineNo(), ast.getColumnNo(), MSG_KEY, count, maxAllowed);
}
}
}