Skip to content

Commit

Permalink
Merge pull request #7358 from martine/empty-return
Browse files Browse the repository at this point in the history
in noImplicitReturns mode, also disallow "return;"
  • Loading branch information
mhegazy committed Mar 3, 2016
2 parents 744e510 + 5132ea6 commit 0cba37d
Show file tree
Hide file tree
Showing 4 changed files with 116 additions and 5 deletions.
14 changes: 9 additions & 5 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13848,11 +13848,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) {
Expand Down Expand Up @@ -13887,6 +13887,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);
}
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -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.
}
}

Original file line number Diff line number Diff line change
@@ -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;
}
}
25 changes: 25 additions & 0 deletions tests/cases/compiler/noImplicitReturnsWithoutReturnExpression.ts
Original file line number Diff line number Diff line change
@@ -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 0cba37d

Please sign in to comment.