diff --git a/resources/default-config.json b/resources/default-config.json index 8e4f008a..538bff9b 100644 --- a/resources/default-config.json +++ b/resources/default-config.json @@ -393,6 +393,7 @@ }, { "props": { + "enforceReturnTypeForAnonymous": false, "allowEmptyReturn": true, "enforceReturnType": false }, diff --git a/src/checkstyle/checks/type/ReturnCheck.hx b/src/checkstyle/checks/type/ReturnCheck.hx index d37e193d..792f58d3 100644 --- a/src/checkstyle/checks/type/ReturnCheck.hx +++ b/src/checkstyle/checks/type/ReturnCheck.hx @@ -6,11 +6,13 @@ class ReturnCheck extends Check { public var allowEmptyReturn:Bool; public var enforceReturnType:Bool; + public var enforceReturnTypeForAnonymous:Bool; public function new() { super(AST); allowEmptyReturn = true; enforceReturnType = false; + enforceReturnTypeForAnonymous = false; categories = [Category.CLARITY]; points = 2; } @@ -105,7 +107,12 @@ class ReturnCheck extends Check { } function warnReturnTypeMissing(name:String, pos:Position) { - if (name == null) logPos("Return type not specified for anonymous method", pos); + if (name == null) { + if (!enforceReturnTypeForAnonymous) { + return; + } + logPos("Return type not specified for anonymous method", pos); + } else logPos('Return type not specified for method "${name}"', pos); } } \ No newline at end of file diff --git a/test/checks/type/ReturnCheckTest.hx b/test/checks/type/ReturnCheckTest.hx index d5be81f5..a5468d94 100644 --- a/test/checks/type/ReturnCheckTest.hx +++ b/test/checks/type/ReturnCheckTest.hx @@ -17,6 +17,21 @@ class ReturnCheckTest extends CheckTestCase { assertNoMsg(check, CORRECT_RETURN); } + public function testCorrectReturnForAnonymous() { + var check = new ReturnCheck(); + assertNoMsg(check, CORRECT_RETURN_ANONYMOUS); + check.enforceReturnTypeForAnonymous = true; + assertNoMsg(check, CORRECT_RETURN_ANONYMOUS); + } + + public function testIncorrectReturnForAnonymous() { + var check = new ReturnCheck(); + assertNoMsg(check, TEST5); + check.enforceReturnTypeForAnonymous = true; + assertMsg(check, TEST5, MSG_NO_ANON_RETURN); + + } + public function testVoid() { assertMsg(new ReturnCheck(), TEST1, MSG_VOID_RETURN); } @@ -28,6 +43,9 @@ class ReturnCheckTest extends CheckTestCase { assertMsg(check, TEST2B, MSG_NOT_TEST1_RETURN); assertMsg(check, TEST_FOR, MSG_NOT_TEST1_RETURN); assertMsg(check, TEST_WHILE, MSG_NOT_TEST1_RETURN); + + assertNoMsg(check, TEST5); + check.enforceReturnTypeForAnonymous = true; assertMsg(check, TEST5, MSG_NO_ANON_RETURN); } @@ -171,6 +189,16 @@ abstract ReturnCheckTests(String) to String { public function test1(); }"; + var CORRECT_RETURN_ANONYMOUS = + "abstractAndClass Test { + public function test7() { + var x = function(i):Int{ + return i * i; + } + return; + } + }"; + var CORRECT_RETURN = "abstractAndClass Test { public function test1():Int {