in noImplicitReturns mode, also disallow "return;"

In --noImplicitReturns mode, if a function specifies a return type,
disallow empty "return;" statements.

Fixes #5916.
This commit is contained in:
Evan Martin
2016-03-02 17:05:33 -08:00
parent 0f6dbd0250
commit 5132ea64ea
4 changed files with 116 additions and 5 deletions

View File

@@ -13842,11 +13842,11 @@ namespace ts {
}
}
if (node.expression) {
const func = getContainingFunction(node);
if (func) {
const signature = getSignatureFromDeclaration(func);
const returnType = getReturnTypeOfSignature(signature);
const func = getContainingFunction(node);
if (func) {
const signature = getSignatureFromDeclaration(func);
const returnType = getReturnTypeOfSignature(signature);
if (node.expression) {
const exprType = checkExpressionCached(node.expression);
if (func.asteriskToken) {
@@ -13881,6 +13881,10 @@ namespace ts {
}
}
}
else if (compilerOptions.noImplicitReturns && !maybeTypeOfKind(returnType, TypeFlags.Void | TypeFlags.Any)) {
// The function has a return type, but the return statement doesn't have an expression.
error(node, Diagnostics.Not_all_code_paths_return_a_value);
}
}
}