Permalink
Browse files

in noImplicitReturns mode, also disallow "return;"

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

Fixes #5916.
  • Loading branch information...
evmar committed Mar 3, 2016
1 parent 0f6dbd0 commit 5132ea64ea379099ec0db00442e60e1bdfbeab13
View
@@ -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);
}
}
}
@@ -0,0 +1,34 @@
tests/cases/compiler/noImplicitReturnsWithoutReturnExpression.ts(2,5): error TS7030: Not all code paths return a value.
tests/cases/compiler/noImplicitReturnsWithoutReturnExpression.ts(22,9): error TS7030: Not all code paths return a value.
==== tests/cases/compiler/noImplicitReturnsWithoutReturnExpression.ts (2 errors) ====
function isMissingReturnExpression(): number {
return;
~~~~~~~
!!! error TS7030: Not all code paths return a value.
}
function isMissingReturnExpression2(): any {
return;
}
function isMissingReturnExpression3(): number|void {
return;
}
function isMissingReturnExpression4(): void {
return;
}
function isMissingReturnExpression5(x) {
if (x) {
return 0;
}
else {
return;
~~~~~~~
!!! error TS7030: Not all code paths return a value.
}
}
@@ -0,0 +1,48 @@
//// [noImplicitReturnsWithoutReturnExpression.ts]
function isMissingReturnExpression(): number {
return;
}
function isMissingReturnExpression2(): any {
return;
}
function isMissingReturnExpression3(): number|void {
return;
}
function isMissingReturnExpression4(): void {
return;
}
function isMissingReturnExpression5(x) {
if (x) {
return 0;
}
else {
return;
}
}
//// [noImplicitReturnsWithoutReturnExpression.js]
function isMissingReturnExpression() {
return;
}
function isMissingReturnExpression2() {
return;
}
function isMissingReturnExpression3() {
return;
}
function isMissingReturnExpression4() {
return;
}
function isMissingReturnExpression5(x) {
if (x) {
return 0;
}
else {
return;
}
}
@@ -0,0 +1,25 @@
// @noImplicitReturns: true
function isMissingReturnExpression(): number {
return;
}
function isMissingReturnExpression2(): any {
return;
}
function isMissingReturnExpression3(): number|void {
return;
}
function isMissingReturnExpression4(): void {
return;
}
function isMissingReturnExpression5(x) {
if (x) {
return 0;
}
else {
return;
}
}

0 comments on commit 5132ea6

Please sign in to comment.