ReturnCount
Since Checkstyle 3.2
Description
equals
by default).
max property will only check returns in methods and lambdas that return a specific value (Ex: 'return 1;').
maxForVoid 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.
Rationale: Too many return points can mean that code is attempting to do too much or may be difficult to understand.
Properties
name | description | type | default value | since |
---|---|---|---|---|
format | Specify method names to ignore. | Pattern | "^equals$" |
3.4 |
max | Specify maximum allowed number of return statements in non-void methods/lambdas. | int | 2 |
3.2 |
maxForVoid | Specify maximum allowed number of return statements in void methods/constructors/lambdas. | int | 1 |
6.19 |
tokens | tokens to check | subset of tokens CTOR_DEF , METHOD_DEF , LAMBDA . | CTOR_DEF , METHOD_DEF , LAMBDA . | 3.2 |
Examples
To configure the check so that it doesn't allow more than three
return statements per method (ignoring the equals()
method):
<module name="Checker">
<module name="TreeWalker">
<module name="ReturnCount">
<property name="max" value="3"/>
</module>
</module>
</module>
Example:
public class Example1 {
public Example1() {}
// ok below, because default void restriction is 1
public Example1(int i) { return; }
public int signA(int x) {
if (x < -2) { return -1; }
return 0;
}
// violation below, 'max allowed for non-void methods/lambdas is 3'
public int signB(int x) {
if (x < -2) { return -1; }
if (x == 0) { return 0; }
if (x > 2) { return 2; }
return 1;
}
// ok below, because non-void restriction is 3
final Predicate<Integer> lambdaA = i -> {
if (i > 5) { return true; }
return false;
};
final Predicate<Integer> lambdaB = i -> { return i > 5; };
public void methodA(int x) {}
// ok below, because default void restriction is 1
public void methodB(int x) { return; }
}
To configure the check so that it doesn't allow any return statements per void method:
<module name="Checker">
<module name="TreeWalker">
<module name="ReturnCount">
<property name="maxForVoid" value="0"/>
</module>
</module>
</module>
Example:
public class Example2 {
public Example2() {}
// violation below, 'max allowed for void methods/constructors/lambdas is 0'
public Example2(int i) { return; }
public int signA(int x) {
if (x < -2) { return -1; }
return 0;
}
// violation below, 'max allowed for non-void methods/lambdas is 2'
public int signB(int x) {
if (x < -2) { return -1; }
if (x == 0) { return 0; }
if (x > 2) { return 2; }
return 1;
}
// ok below, because default non-void restriction is 2
final Predicate<Integer> lambdaA = i -> {
if (i > 5) { return true; }
return false;
};
final Predicate<Integer> lambdaB = i -> { return i > 5; };
public void methodA(int x) {}
// violation below, 'max allowed for void methods/constructors/lambdas is 0'
public void methodB(int x) { return; }
}
To configure the check so that it doesn't allow more than 2
return statements per method (ignoring the equals()
method) and more than 1 return statements per void method:
<module name="Checker">
<module name="TreeWalker">
<module name="ReturnCount">
<property name="max" value="2"/>
<property name="maxForVoid" value="1"/>
</module>
</module>
</module>
Example:
public class Example3 {
public Example3() {}
// ok below, because default void restriction is 1
public Example3(int i) { return; }
public int signA(int x) {
if (x < -2) { return -1; }
return 0;
}
// violation below, 'max allowed for non-void methods/lambdas is 2'
public int signB(int x) {
if (x < -2) { return -1; }
if (x == 0) { return 0; }
if (x > 2) { return 2; }
return 1;
}
// ok below, because non-void restriction is 2
final Predicate<Integer> lambdaA = i -> {
if (i > 5) { return true; }
return false;
};
final Predicate<Integer> lambdaB = i -> { return i > 5; };
public void methodA(int x) {}
// ok below, because default void restriction is 1
public void methodB(int x) { return; }
}
To configure the check so that it doesn't allow more than three return statements per method for all methods:
<module name="Checker">
<module name="TreeWalker">
<module name="ReturnCount">
<property name="max" value="3"/>
<property name="format" value="^$"/>
</module>
</module>
</module>
Example:
public class Example4 {
public Example4() {}
// ok below, because default void restriction is 1
public Example4(int i) { return; }
public int signA(int x) {
if (x < -2) { return -1; }
return 0;
}
// violation below, 'max allowed for non-void methods/lambdas is 3'
public int signB(int x) {
if (x < -2) { return -1; }
if (x == 0) { return 0; }
if (x > 2) { return 2; }
return 1;
}
// ok below, because non-void restriction is 3
final Predicate<Integer> lambdaA = i -> {
if (i > 5) { return true; }
return false;
};
final Predicate<Integer> lambdaB = i -> { return i > 5; };
public void methodA(int x) {}
// ok below, because default void restriction is 1
public void methodB(int x) { return; }
}
To configure the check so that it doesn't allow any return statements in constructors, more than one return statement in all lambda expressions and more than two return statements in methods:
<module name="Checker">
<module name="TreeWalker">
<module name="ReturnCount">
<property name="maxForVoid" value="0"/>
<property name="tokens" value="CTOR_DEF"/>
</module>
<module name="ReturnCount">
<property name="max" value="1"/>
<property name="tokens" value="LAMBDA"/>
</module>
<module name="ReturnCount">
<property name="max" value="2"/>
<property name="tokens" value="METHOD_DEF"/>
</module>
</module>
</module>
Example:
public class Example5 {
public Example5() {}
// violation below, 'max allowed for void methods/constructors/lambdas is 0'
public Example5(int i) { return; }
public int signA(int x) {
if (x < -2) { return -1; }
return 0;
}
// violation below, 'max allowed for non-void methods/lambdas is 2'
public int signB(int x) {
if (x < -2) { return -1; }
if (x == 0) { return 0; }
if (x > 2) { return 2; }
return 1;
}
// violation below, 'max allowed for non-void methods/lambdas is 1'
final Predicate<Integer> lambdaA = i -> {
if (i > 5) { return true; }
return false;
};
final Predicate<Integer> lambdaB = i -> { return i > 5; };
public void methodA(int x) {}
// ok below, because default void restriction is 1
public void methodB(int x) { return; }
}
Example of Usage
Violation Messages
All messages can be customized if the default message doesn't suit you. Please see the documentation to learn how to.
Package
com.puppycrawl.tools.checkstyle.checks.coding