-
Notifications
You must be signed in to change notification settings - Fork 12.8k
do not report 'noImplicitReturns' error if inferred return type of th… #5824
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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9871,31 +9871,39 @@ namespace ts { | |
} | ||
|
||
// Functions with with an explicitly specified 'void' or 'any' return type don't need any return expressions. | ||
if (returnType && (returnType === voidType || isTypeAny(returnType))) { | ||
return; | ||
} | ||
|
||
// if return type is not specified then we'll do the check only if 'noImplicitReturns' option is set | ||
if (!returnType && !compilerOptions.noImplicitReturns) { | ||
if (returnType === voidType || isTypeAny(returnType)) { | ||
return; | ||
} | ||
|
||
// If all we have is a function signature, or an arrow function with an expression body, then there is nothing to check. | ||
// also if HasImplicitReturnValue flags is not set this means that all codepaths in function body end with return of throw | ||
// also if HasImplicitReturn flag is not set this means that all codepaths in function body end with return or throw | ||
if (nodeIsMissing(func.body) || func.body.kind !== SyntaxKind.Block || !(func.flags & NodeFlags.HasImplicitReturn)) { | ||
return; | ||
} | ||
|
||
if (!returnType || func.flags & NodeFlags.HasExplicitReturn) { | ||
if (compilerOptions.noImplicitReturns) { | ||
error(func.type || func, Diagnostics.Not_all_code_paths_return_a_value); | ||
} | ||
} | ||
else { | ||
// This function does not conform to the specification. | ||
const hasExplicitReturn = func.flags & NodeFlags.HasExplicitReturn; | ||
|
||
if (returnType && !hasExplicitReturn) { | ||
// minimal check: function has syntactic return type annotation and no explicit return statements in the body | ||
// this function does not conform to the specification. | ||
// NOTE: having returnType !== undefined is a precondition for entering this branch so func.type will always be present | ||
error(func.type, Diagnostics.A_function_whose_declared_type_is_neither_void_nor_any_must_return_a_value); | ||
} | ||
else if (compilerOptions.noImplicitReturns) { | ||
if (!returnType) { | ||
// If return type annotation is omitted check if function has any explicit return statements. | ||
// If it does not have any - its inferred return type is void - don't do any checks. | ||
// Otherwise get inferred return type from function body and report error only if it is not void / anytype | ||
const inferredReturnType = hasExplicitReturn | ||
? getReturnTypeOfSignature(getSignatureFromDeclaration(func)) | ||
: voidType; | ||
|
||
if (inferredReturnType === voidType || isTypeAny(inferredReturnType)) { | ||
return; | ||
} | ||
} | ||
error(func.type || func, Diagnostics.Not_all_code_paths_return_a_value); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What about There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
} | ||
} | ||
|
||
function checkFunctionExpressionOrObjectLiteralMethod(node: FunctionExpression | MethodDeclaration, contextualMapper?: TypeMapper): Type { | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,39 @@ | ||
tests/cases/compiler/reachabilityChecks7.ts(3,16): error TS7030: Not all code paths return a value. | ||
tests/cases/compiler/reachabilityChecks7.ts(6,9): error TS7030: Not all code paths return a value. | ||
tests/cases/compiler/reachabilityChecks7.ts(14,16): error TS7030: Not all code paths return a value. | ||
tests/cases/compiler/reachabilityChecks7.ts(18,22): error TS2355: A function whose declared type is neither 'void' nor 'any' must return a value. | ||
|
||
|
||
==== tests/cases/compiler/reachabilityChecks7.ts (2 errors) ==== | ||
|
||
// async function without return type annotation - error | ||
async function f1() { | ||
~~ | ||
!!! error TS7030: Not all code paths return a value. | ||
} | ||
|
||
let x = async function() { | ||
~~~~~ | ||
!!! error TS7030: Not all code paths return a value. | ||
} | ||
|
||
// async function with which promised type is void - return can be omitted | ||
async function f2(): Promise<void> { | ||
|
||
} | ||
} | ||
|
||
async function f3(x) { | ||
~~ | ||
!!! error TS7030: Not all code paths return a value. | ||
if (x) return 10; | ||
} | ||
|
||
async function f4(): Promise<number> { | ||
~~~~~~~~~~~~~~~ | ||
!!! error TS2355: A function whose declared type is neither 'void' nor 'any' must return a value. | ||
|
||
} | ||
|
||
function voidFunc(): void { | ||
} | ||
|
||
function calltoVoidFunc(x) { | ||
if (x) return voidFunc(); | ||
} | ||
|
||
declare function use(s: string): void; | ||
let x1 = () => { use("Test"); } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
remove "explicitly specified"
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
'explicitly specified' reflects what is checked in the
if
below - if function has explicit type annotation and it isvoid
/any
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Remove the second "with"