aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorvaano94 <vaano94@gmail.com>2017-12-20 13:47:35 +0300
committerRoman Ivanov <romani@users.noreply.github.com>2017-12-22 07:13:54 -0800
commit952a97db47130898379893c4da09cbb05c25f019 (patch)
tree6ad4b71c5e3ff17d54002be884a379152f28ef39
parent040eb836daedb8d39f5646c91e2b7dad3736088f (diff)
downloadcheckstyle-952a97db47130898379893c4da09cbb05c25f019.tar.gz
Issue #5306: add additional message for void return
-rw-r--r--src/main/java/com/puppycrawl/tools/checkstyle/checks/coding/ReturnCountCheck.java24
-rw-r--r--src/main/resources/com/puppycrawl/tools/checkstyle/checks/coding/messages.properties3
-rw-r--r--src/main/resources/com/puppycrawl/tools/checkstyle/checks/coding/messages_de.properties3
-rw-r--r--src/main/resources/com/puppycrawl/tools/checkstyle/checks/coding/messages_es.properties3
-rw-r--r--src/main/resources/com/puppycrawl/tools/checkstyle/checks/coding/messages_fi.properties3
-rw-r--r--src/main/resources/com/puppycrawl/tools/checkstyle/checks/coding/messages_fr.properties3
-rw-r--r--src/main/resources/com/puppycrawl/tools/checkstyle/checks/coding/messages_ja.properties3
-rw-r--r--src/main/resources/com/puppycrawl/tools/checkstyle/checks/coding/messages_pt.properties3
-rw-r--r--src/main/resources/com/puppycrawl/tools/checkstyle/checks/coding/messages_tr.properties3
-rw-r--r--src/main/resources/com/puppycrawl/tools/checkstyle/checks/coding/messages_zh.properties3
-rw-r--r--src/test/java/com/puppycrawl/tools/checkstyle/checks/coding/ReturnCountCheckTest.java20
-rw-r--r--src/test/resources/com/puppycrawl/tools/checkstyle/checks/coding/returncount/InputReturnCountVoid.java8
-rw-r--r--src/xdocs/config_coding.xml4
13 files changed, 60 insertions, 23 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 231fcd8fd..fcdcdda9d 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
@@ -60,6 +60,11 @@ public final class ReturnCountCheck extends AbstractCheck {
* file.
*/
public static final String MSG_KEY = "return.count";
+ /**
+ * A key pointing to the warning message text in "messages.properties"
+ * file.
+ */
+ public static final String MSG_KEY_VOID = "return.countVoid";
/** Stack of method contexts. */
private final Deque<Context> contextStack = new ArrayDeque<>();
@@ -199,10 +204,10 @@ public final class ReturnCountCheck extends AbstractCheck {
// 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);
+ context.visitLiteralReturn(maxForVoid, true);
}
else {
- context.visitLiteralReturn(max);
+ context.visitLiteralReturn(max, false);
}
}
@@ -217,6 +222,8 @@ public final class ReturnCountCheck extends AbstractCheck {
private int count;
/** Maximum allowed number of return statements. */
private Integer maxAllowed;
+ /** Identifies if context is void. */
+ private boolean isVoidContext;
/**
* Creates new method context.
@@ -227,10 +234,12 @@ public final class ReturnCountCheck extends AbstractCheck {
}
/**
- * Increase the number of return statements.
+ * Increase the number of return statements and set context return type.
* @param maxAssigned Maximum allowed number of return statements.
+ * @param voidReturn Identifies if context is void.
*/
- public void visitLiteralReturn(int maxAssigned) {
+ public void visitLiteralReturn(int maxAssigned, Boolean voidReturn) {
+ isVoidContext = voidReturn;
if (maxAllowed == null) {
maxAllowed = maxAssigned;
}
@@ -245,7 +254,12 @@ public final class ReturnCountCheck extends AbstractCheck {
*/
public void checkCount(DetailAST ast) {
if (checking && maxAllowed != null && count > maxAllowed) {
- log(ast.getLineNo(), ast.getColumnNo(), MSG_KEY, count, maxAllowed);
+ if (isVoidContext) {
+ log(ast.getLineNo(), ast.getColumnNo(), MSG_KEY_VOID, count, maxAllowed);
+ }
+ else {
+ log(ast.getLineNo(), ast.getColumnNo(), MSG_KEY, count, maxAllowed);
+ }
}
}
}
diff --git a/src/main/resources/com/puppycrawl/tools/checkstyle/checks/coding/messages.properties b/src/main/resources/com/puppycrawl/tools/checkstyle/checks/coding/messages.properties
index 64b56e1a2..7a2168816 100644
--- a/src/main/resources/com/puppycrawl/tools/checkstyle/checks/coding/messages.properties
+++ b/src/main/resources/com/puppycrawl/tools/checkstyle/checks/coding/messages.properties
@@ -44,7 +44,8 @@ overload.methods.declaration=Overload methods should not be split. Previous over
parameter.assignment=Assignment of parameter ''{0}'' is not allowed.
require.this.method=Method call to ''{0}'' needs \"{1}this.\".
require.this.variable=Reference to instance variable ''{0}'' needs \"{1}this.\".
-return.count=Return count is {0,number,integer} (max allowed is {1,number,integer}).
+return.count=Return count is {0,number,integer} (max allowed for non-void methods/lambdas is {1,number,integer}).
+return.countVoid=Return count is {0,number,integer} (max allowed for void methods/constructors/lambdas is {1,number,integer}).
simplify.boolReturn=Conditional logic can be removed.
simplify.expression=Expression can be simplified.
string.literal.equality=Literal Strings should be compared using equals(), not ''{0}''.
diff --git a/src/main/resources/com/puppycrawl/tools/checkstyle/checks/coding/messages_de.properties b/src/main/resources/com/puppycrawl/tools/checkstyle/checks/coding/messages_de.properties
index bb1308e27..6e46475d4 100644
--- a/src/main/resources/com/puppycrawl/tools/checkstyle/checks/coding/messages_de.properties
+++ b/src/main/resources/com/puppycrawl/tools/checkstyle/checks/coding/messages_de.properties
@@ -44,7 +44,8 @@ overload.methods.declaration=Die Definition der überladene Methoden steht nicht
parameter.assignment=Die Zuweisung des Parameters ''{0}'' ist nicht erlaubt.
require.this.method=Der Methodenaufruf ''{0}'' erfordert ein vorangestelltes \"{1}this.\".
require.this.variable=Der Instanzvariablenzugriff ''{0}'' erfordert ein vorangestelltes \"{1}this.\".
-return.count=Die Methode hat insgesamt {0,number,integer} return-Anweisungen (Obergrenze ist {1,number,integer}).
+return.count=Die Methode hat insgesamt {0,number,integer} return-Anweisungen (max erlaubt für nicht void Methoden / Lambdas ist ist {1,number,integer}).
+return.countVoid=Die Methode hat insgesamt {0,number,integer} return-Anweisungen (max erlaubt für void Methoden / Konstruktoren / Lambdas ist {1,number,integer}).
simplify.boolReturn=Die Verzweigungslogik kann entfernt werden.
simplify.expression=Der Ausdruck kann vereinfacht werden.
string.literal.equality=Der Vergleich von String-Literalen sollte mit equals() erfolgen, nicht mit ''{0}''.
diff --git a/src/main/resources/com/puppycrawl/tools/checkstyle/checks/coding/messages_es.properties b/src/main/resources/com/puppycrawl/tools/checkstyle/checks/coding/messages_es.properties
index 1a97e0017..3f7b7f6a8 100644
--- a/src/main/resources/com/puppycrawl/tools/checkstyle/checks/coding/messages_es.properties
+++ b/src/main/resources/com/puppycrawl/tools/checkstyle/checks/coding/messages_es.properties
@@ -44,7 +44,8 @@ overload.methods.declaration=Métodos de sobrecarga no deben ser divididos. Mét
parameter.assignment=No esta permitida la asignación del parámetro ''{0}''.
require.this.method=La llamada al método ''{0}'' necesita \"{1}this.\".
require.this.variable=La referencia a la variable de instancia ''{0}'' necesita \"{1}this.\".
-return.count=El número de sentencias return es {0,number,integer} (máximo permitido es {1,number,integer}).
+return.count=El número de sentencias return es {0,number,integer} (max permitido para métodos no vacíos / lambdas es {1,number,integer}).
+return.countVoid=El número de sentencias return es {0,number,integer} (max permitido para métodos / constructores / lambdas vacíos {1,number,integer}).
simplify.boolReturn=Se puede eliminar la lógica condicional.
simplify.expression=Se puede simplificar la expresión.
string.literal.equality=Las cadenas literales deben compararse usando equals(), no ''{0}''.
diff --git a/src/main/resources/com/puppycrawl/tools/checkstyle/checks/coding/messages_fi.properties b/src/main/resources/com/puppycrawl/tools/checkstyle/checks/coding/messages_fi.properties
index b28e231c1..1246514af 100644
--- a/src/main/resources/com/puppycrawl/tools/checkstyle/checks/coding/messages_fi.properties
+++ b/src/main/resources/com/puppycrawl/tools/checkstyle/checks/coding/messages_fi.properties
@@ -44,7 +44,8 @@ overload.methods.declaration=Ylikuormitus menetelmiä ei tulisi jakaa. Edellinen
parameter.assignment=Tehtävä parametrin ''{0}'' ei ole sallittu.
require.this.method=Menetelmä kehotus ''{0}'' tarvitsee \"{1}this.\".
require.this.variable=Viittaus Esimerkiksi muuttuja ''{0}'' tarvitsee \"{1}this.\".
-return.count=Paluu määrä on {0, number, integer} (max sallittu on {1, number, integer}).
+return.count=Paluu määrä on {0, number, integer} (max sallittu ei-tyhjiin menetelmiin / lambdas {1, number, integer}).
+return.countVoid=Paluu määrä on {0, number, integer} (max sallittu menetelmät / konstruktorit / lambdas ovat {1, number, integer}).
simplify.boolReturn=Konditionaalilogiikan voisi poistaa.
simplify.expression=Ilmaisua voisi yksinkertaistaa.
string.literal.equality=Kirjaimellinen Strings pitäisi verrata käyttämällä tasavertaisten (), ei ''{0}''.
diff --git a/src/main/resources/com/puppycrawl/tools/checkstyle/checks/coding/messages_fr.properties b/src/main/resources/com/puppycrawl/tools/checkstyle/checks/coding/messages_fr.properties
index 7ae8de9d5..a1d712fbf 100644
--- a/src/main/resources/com/puppycrawl/tools/checkstyle/checks/coding/messages_fr.properties
+++ b/src/main/resources/com/puppycrawl/tools/checkstyle/checks/coding/messages_fr.properties
@@ -44,7 +44,8 @@ overload.methods.declaration=Les méthodes de surcharge ne doivent pas être sé
parameter.assignment=Il est interdit d''affecter une valeur au paramètre ''{0}''.
require.this.method=L''appel à la méthode ''{0}'' nécessite l''utilisation de \"{1}this.\".
require.this.variable=La référence à la variable d''instance ''{0}'' doit utiliser \"{1}this.\".
-return.count=Le nombre de return est de {0,number,integer} alors que le maximum autorisé est de {1,number,integer}.
+return.count=Le nombre de return est de {0,number,integer} alors que le maximum méthodes non-nulles/lambdas {1,number,integer}.
+return.countVoid=Le nombre de return est de {0,number,integer} alors que le maximum autorisé void methods/constructors/lambdas est de {1,number,integer}.
simplify.boolReturn=Le test peut être supprimé et l''expression directement retournée.
simplify.expression=L''expression peut être simplifiée.
string.literal.equality=Les chaines de caractères littérales devraient être comparées avec la méthode equals() et pas avec ''{0}''.
diff --git a/src/main/resources/com/puppycrawl/tools/checkstyle/checks/coding/messages_ja.properties b/src/main/resources/com/puppycrawl/tools/checkstyle/checks/coding/messages_ja.properties
index 98adfa1fa..ae74664c6 100644
--- a/src/main/resources/com/puppycrawl/tools/checkstyle/checks/coding/messages_ja.properties
+++ b/src/main/resources/com/puppycrawl/tools/checkstyle/checks/coding/messages_ja.properties
@@ -44,7 +44,8 @@ overload.methods.declaration=オーバーロードされているメソッドは
parameter.assignment=パラメータ ''{0}'' への代入は許可されていません。
require.this.method=メソッド ''{0}'' への呼び出しは、 \"{1}this.\" が必要です。
require.this.variable=インスタンス変数 ''{0}'' への参照には \"{1}this.\" が必要です。
-return.count=return が {0,number,integer} 個所あります(最大 {1,number,integer} まで)。
+return.count=return が {0,number,integer} 個所あります(非無効メソッド/ ラムダに許される最大値は {1,number,integer} まで)。
+return.countVoid=return が {0,number,integer} 個所あります(無効メソッド/コンストラクタ/ ラムダメソッドに許可される最大値は {1,number,integer} まで)。
simplify.boolReturn=条件ロジックはなくせます。
simplify.expression=表現は簡潔にできます。
string.literal.equality=リテラルの文字列は ''{0}'' ではなく、 equals() を使用して比較するべきです。
diff --git a/src/main/resources/com/puppycrawl/tools/checkstyle/checks/coding/messages_pt.properties b/src/main/resources/com/puppycrawl/tools/checkstyle/checks/coding/messages_pt.properties
index 73fb2cc7c..da79ad8de 100644
--- a/src/main/resources/com/puppycrawl/tools/checkstyle/checks/coding/messages_pt.properties
+++ b/src/main/resources/com/puppycrawl/tools/checkstyle/checks/coding/messages_pt.properties
@@ -44,7 +44,8 @@ overload.methods.declaration=Métodos de sobrecarga não devem ser separados. O
parameter.assignment=A atribuição ao parâmetro ''{0}'' não é permitda.
require.this.method=A chamada de método no ''{0}'' precisa de \"{1}this.\".
require.this.variable=A referência à variável de instância ''{0}'' precisa de \"{1}this.\".
-return.count=O número de \"return\"s é {0,number,integer} (o máximo permitido é {1,number,integer}).
+return.count=O número de \"return\"s é {0,number,integer} (o máximo permitido para métodos não vazios/lambdas é {1,number,integer}).
+return.countVoid=O número de \"return\"s é {0,number,integer} (o máximo permitido para vazios métodos/construtores/lambdas é {1,number,integer}).
simplify.boolReturn=A lógica condicional pode ser removida.
simplify.expression=A expressão pode ser simplicada.
string.literal.equality=Literais de String deveriam ser comparados com \"equals()\", não com ''{0}''.
diff --git a/src/main/resources/com/puppycrawl/tools/checkstyle/checks/coding/messages_tr.properties b/src/main/resources/com/puppycrawl/tools/checkstyle/checks/coding/messages_tr.properties
index da4f29763..4e278314c 100644
--- a/src/main/resources/com/puppycrawl/tools/checkstyle/checks/coding/messages_tr.properties
+++ b/src/main/resources/com/puppycrawl/tools/checkstyle/checks/coding/messages_tr.properties
@@ -45,7 +45,8 @@ overload.methods.declaration=Aşırı yük yöntemleri bölünmüş olmamalıdı
parameter.assignment=''{0}'' parametresine atama yapılamaz.
require.this.method=''{0}'' metoduna erişim \"{1}this.\" kullanılarak yapılmalıdır.
require.this.variable=''{0}'' değişkenine erişim \"{1}this.\" kullanılarak yapılmalıdır.
-return.count=Kullanılan ''return'' sayısı {0,number,integer} (maksimum izin verilen değer {1,number,integer}).
+return.count=Kullanılan ''return'' sayısı {0,number,integer} (void olmayan yöntemler / lambdas için izin verilen maksimum sayı {1,number,integer}).
+return.countVoid=Kullanılan ''return'' sayısı {0,number,integer} (void yöntemleri / kurucu / lambdas için izin verilen maksimum sayı {1,number,integer}).
simplify.boolReturn=Koşul mantığı kaldırılabilir.
simplify.expression=İfade sadeleştirilebilir.
string.literal.equality=''String'' ifadeleri ''{0}'' kullanarak değil, equals() metodu kullanarak karşılaştırılmalı.
diff --git a/src/main/resources/com/puppycrawl/tools/checkstyle/checks/coding/messages_zh.properties b/src/main/resources/com/puppycrawl/tools/checkstyle/checks/coding/messages_zh.properties
index 5bc59c5a1..51bf29a60 100644
--- a/src/main/resources/com/puppycrawl/tools/checkstyle/checks/coding/messages_zh.properties
+++ b/src/main/resources/com/puppycrawl/tools/checkstyle/checks/coding/messages_zh.properties
@@ -44,7 +44,8 @@ overload.methods.declaration=重载方法应写在一起,上一个重载方法
parameter.assignment=不应对方法参数''{0}''赋值。
require.this.method=对方法 ''{0}'' 的调用需要 \"{1}this.\"。
require.this.variable=对实例属性 ''{0}'' 的引用需要 \"{1}this.\"。
-return.count=Return 次数 {0,number,integer} 次(最多: {1,number,integer} 次)。
+return.count=Return 次数 {0,number,integer} 次(最大允许非空虚方法/ 拉姆达: {1,number,integer} 次)。
+return.countVoid=Return 次数 {0,number,integer} 次(最大允許為void方法/構造函數/ 拉姆达: {1,number,integer} 次)。
simplify.boolReturn=不必要的条件逻辑。
simplify.expression=表达式可被简化。
string.literal.equality=字符串应使用equals()方法进行比较,而非''{0}''。
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 eccf7ec67..6e407a47f 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
@@ -20,6 +20,7 @@
package com.puppycrawl.tools.checkstyle.checks.coding;
import static com.puppycrawl.tools.checkstyle.checks.coding.ReturnCountCheck.MSG_KEY;
+import static com.puppycrawl.tools.checkstyle.checks.coding.ReturnCountCheck.MSG_KEY_VOID;
import static org.junit.Assert.assertTrue;
import java.io.File;
@@ -48,9 +49,9 @@ public class ReturnCountCheckTest extends AbstractModuleTestSupport {
final DefaultConfiguration checkConfig =
createModuleConfig(ReturnCountCheck.class);
final String[] expected = {
- "18:5: " + getCheckMessage(MSG_KEY, 7, 1),
- "30:5: " + getCheckMessage(MSG_KEY, 2, 1),
- "35:17: " + getCheckMessage(MSG_KEY, 6, 1),
+ "18:5: " + getCheckMessage(MSG_KEY_VOID, 7, 1),
+ "30:5: " + getCheckMessage(MSG_KEY_VOID, 2, 1),
+ "35:17: " + getCheckMessage(MSG_KEY_VOID, 6, 1),
"49:5: " + getCheckMessage(MSG_KEY, 7, 2),
};
verify(checkConfig, getPath("InputReturnCountSwitches.java"), expected);
@@ -63,9 +64,9 @@ public class ReturnCountCheckTest extends AbstractModuleTestSupport {
checkConfig.addAttribute("format", "^$");
final String[] expected = {
"5:5: " + getCheckMessage(MSG_KEY, 7, 2),
- "18:5: " + getCheckMessage(MSG_KEY, 7, 1),
- "30:5: " + getCheckMessage(MSG_KEY, 2, 1),
- "35:17: " + getCheckMessage(MSG_KEY, 6, 1),
+ "18:5: " + getCheckMessage(MSG_KEY_VOID, 7, 1),
+ "30:5: " + getCheckMessage(MSG_KEY_VOID, 2, 1),
+ "35:17: " + getCheckMessage(MSG_KEY_VOID, 6, 1),
"49:5: " + getCheckMessage(MSG_KEY, 7, 2),
};
verify(checkConfig, getPath("InputReturnCountSwitches.java"), expected);
@@ -146,10 +147,11 @@ public class ReturnCountCheckTest extends AbstractModuleTestSupport {
checkConfig.addAttribute("max", "2");
checkConfig.addAttribute("maxForVoid", "0");
final String[] expected = {
- "4:5: " + getCheckMessage(MSG_KEY, 1, 0),
- "8:5: " + getCheckMessage(MSG_KEY, 1, 0),
- "14:5: " + getCheckMessage(MSG_KEY, 2, 0),
+ "4:5: " + getCheckMessage(MSG_KEY_VOID, 1, 0),
+ "8:5: " + getCheckMessage(MSG_KEY_VOID, 1, 0),
+ "14:5: " + getCheckMessage(MSG_KEY_VOID, 2, 0),
"30:5: " + getCheckMessage(MSG_KEY, 3, 2),
+ "41:5: " + getCheckMessage(MSG_KEY_VOID, 2, 0),
};
verify(checkConfig, getPath("InputReturnCountVoid.java"), expected);
}
diff --git a/src/test/resources/com/puppycrawl/tools/checkstyle/checks/coding/returncount/InputReturnCountVoid.java b/src/test/resources/com/puppycrawl/tools/checkstyle/checks/coding/returncount/InputReturnCountVoid.java
index 9a2d48faa..a1beceeae 100644
--- a/src/test/resources/com/puppycrawl/tools/checkstyle/checks/coding/returncount/InputReturnCountVoid.java
+++ b/src/test/resources/com/puppycrawl/tools/checkstyle/checks/coding/returncount/InputReturnCountVoid.java
@@ -37,4 +37,12 @@ class InputReturnCountVoid {
return 0;
}
+
+ void method5() {
+ if (true) {
+ return;
+ }
+
+ return;
+ }
}
diff --git a/src/xdocs/config_coding.xml b/src/xdocs/config_coding.xml
index d05b6c229..c44d889f6 100644
--- a/src/xdocs/config_coding.xml
+++ b/src/xdocs/config_coding.xml
@@ -3984,6 +3984,10 @@ class C {
<a href="https://github.com/search?q=path%3Asrc%2Fmain%2Fresources%2Fcom%2Fpuppycrawl%2Ftools%2Fcheckstyle%2Fchecks%2Fcoding+filename%3Amessages*.properties+repo%3Acheckstyle%2Fcheckstyle+%22return.count%22">
return.count</a>
</li>
+ <li>
+ <a href="https://github.com/search?q=path%3Asrc%2Fmain%2Fresources%2Fcom%2Fpuppycrawl%2Ftools%2Fcheckstyle%2Fchecks%2Fcoding+filename%3Amessages*.properties+repo%3Acheckstyle%2Fcheckstyle+%22return.countVoid%22">
+ return.countVoid</a>
+ </li>
</ul>
<p>
All messages can be customized if the default message doesn't suit you.