Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

in noImplicitReturns mode, also disallow "return;" #7358

Merged
merged 1 commit into from
Mar 3, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 9 additions & 5 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

4 spaces per indentation

}
}
}

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;
}
}