From 5132ea64ea379099ec0db00442e60e1bdfbeab13 Mon Sep 17 00:00:00 2001 From: Evan Martin Date: Wed, 2 Mar 2016 17:05:33 -0800 Subject: [PATCH] in noImplicitReturns mode, also disallow "return;" In --noImplicitReturns mode, if a function specifies a return type, disallow empty "return;" statements. Fixes #5916. --- src/compiler/checker.ts | 14 ++++-- ...tReturnsWithoutReturnExpression.errors.txt | 34 +++++++++++++ ...oImplicitReturnsWithoutReturnExpression.js | 48 +++++++++++++++++++ ...oImplicitReturnsWithoutReturnExpression.ts | 25 ++++++++++ 4 files changed, 116 insertions(+), 5 deletions(-) create mode 100644 tests/baselines/reference/noImplicitReturnsWithoutReturnExpression.errors.txt create mode 100644 tests/baselines/reference/noImplicitReturnsWithoutReturnExpression.js create mode 100644 tests/cases/compiler/noImplicitReturnsWithoutReturnExpression.ts diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index 4ca97793073e7..084fc48f0cb14 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -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); + } } } diff --git a/tests/baselines/reference/noImplicitReturnsWithoutReturnExpression.errors.txt b/tests/baselines/reference/noImplicitReturnsWithoutReturnExpression.errors.txt new file mode 100644 index 0000000000000..83d9a65e95d60 --- /dev/null +++ b/tests/baselines/reference/noImplicitReturnsWithoutReturnExpression.errors.txt @@ -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. + } + } + \ No newline at end of file diff --git a/tests/baselines/reference/noImplicitReturnsWithoutReturnExpression.js b/tests/baselines/reference/noImplicitReturnsWithoutReturnExpression.js new file mode 100644 index 0000000000000..8c9123f6e17f5 --- /dev/null +++ b/tests/baselines/reference/noImplicitReturnsWithoutReturnExpression.js @@ -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; + } +} diff --git a/tests/cases/compiler/noImplicitReturnsWithoutReturnExpression.ts b/tests/cases/compiler/noImplicitReturnsWithoutReturnExpression.ts new file mode 100644 index 0000000000000..f532b1280d31d --- /dev/null +++ b/tests/cases/compiler/noImplicitReturnsWithoutReturnExpression.ts @@ -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; + } +}