Skip to content

Don’t error for missing await when promise is referenced in condition body #43593

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 2 commits into from
Apr 13, 2021
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
39 changes: 20 additions & 19 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35371,15 +35371,6 @@ namespace ts {
if (!strictNullChecks) return;
if (getFalsyFlags(type)) return;

if (getAwaitedTypeOfPromise(type)) {
errorAndMaybeSuggestAwait(
condExpr,
/*maybeMissingAwait*/ true,
Diagnostics.This_condition_will_always_return_true_since_this_0_appears_to_always_be_defined,
getTypeNameForErrorDisplay(type));
return;
}

const location = isBinaryExpression(condExpr) ? condExpr.right : condExpr;
const testedNode = isIdentifier(location) ? location
: isPropertyAccessExpression(location) ? location.name
Expand All @@ -35392,12 +35383,13 @@ namespace ts {
}

// While it technically should be invalid for any known-truthy value
// to be tested, we de-scope to functions unrefenced in the block as a
// heuristic to identify the most common bugs. There are too many
// false positives for values sourced from type definitions without
// strictNullChecks otherwise.
// to be tested, we de-scope to functions and Promises unreferenced in
// the block as a heuristic to identify the most common bugs. There
// are too many false positives for values sourced from type
// definitions without strictNullChecks otherwise.
const callSignatures = getSignaturesOfType(type, SignatureKind.Call);
if (callSignatures.length === 0) {
const isPromise = !!getAwaitedTypeOfPromise(type);
if (callSignatures.length === 0 && !isPromise) {
return;
}

Expand All @@ -35406,14 +35398,23 @@ namespace ts {
return;
}

const isUsed = isBinaryExpression(condExpr.parent) && isFunctionUsedInBinaryExpressionChain(condExpr.parent, testedSymbol)
|| body && isFunctionUsedInConditionBody(condExpr, body, testedNode, testedSymbol);
const isUsed = isBinaryExpression(condExpr.parent) && isSymbolUsedInBinaryExpressionChain(condExpr.parent, testedSymbol)
|| body && isSymbolUsedInConditionBody(condExpr, body, testedNode, testedSymbol);
if (!isUsed) {
error(location, Diagnostics.This_condition_will_always_return_true_since_this_function_appears_to_always_be_defined_Did_you_mean_to_call_it_instead);
if (isPromise) {
errorAndMaybeSuggestAwait(
location,
/*maybeMissingAwait*/ true,
Diagnostics.This_condition_will_always_return_true_since_this_0_is_always_defined,
getTypeNameForErrorDisplay(type));
}
else {
error(location, Diagnostics.This_condition_will_always_return_true_since_this_function_is_always_defined_Did_you_mean_to_call_it_instead);
}
}
}

function isFunctionUsedInConditionBody(expr: Expression, body: Statement | Expression, testedNode: Node, testedSymbol: Symbol): boolean {
function isSymbolUsedInConditionBody(expr: Expression, body: Statement | Expression, testedNode: Node, testedSymbol: Symbol): boolean {
return !!forEachChild(body, function check(childNode): boolean | undefined {
if (isIdentifier(childNode)) {
const childSymbol = getSymbolAtLocation(childNode);
Expand Down Expand Up @@ -35451,7 +35452,7 @@ namespace ts {
});
}

function isFunctionUsedInBinaryExpressionChain(node: Node, testedSymbol: Symbol): boolean {
function isSymbolUsedInBinaryExpressionChain(node: Node, testedSymbol: Symbol): boolean {
while (isBinaryExpression(node) && node.operatorToken.kind === SyntaxKind.AmpersandAmpersandToken) {
const isUsed = forEachChild(node.right, function visit(child): boolean | undefined {
if (isIdentifier(child)) {
Expand Down
4 changes: 2 additions & 2 deletions src/compiler/diagnosticMessages.json
Original file line number Diff line number Diff line change
Expand Up @@ -3172,7 +3172,7 @@
"category": "Error",
"code": 2773
},
"This condition will always return true since this function appears to always be defined. Did you mean to call it instead?": {
"This condition will always return true since this function is always defined. Did you mean to call it instead?": {
"category": "Error",
"code": 2774
},
Expand Down Expand Up @@ -3280,7 +3280,7 @@
"category": "Error",
"code": 2800
},
"This condition will always return true since this '{0}' appears to always be defined.": {
"This condition will always return true since this '{0}' is always defined.": {
"category": "Error",
"code": 2801
},
Expand Down
2 changes: 1 addition & 1 deletion src/services/codefixes/addMissingAwait.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ namespace ts.codefix {
Diagnostics.Operator_0_cannot_be_applied_to_type_1.code,
Diagnostics.Operator_0_cannot_be_applied_to_types_1_and_2.code,
Diagnostics.This_condition_will_always_return_0_since_the_types_1_and_2_have_no_overlap.code,
Diagnostics.This_condition_will_always_return_true_since_this_0_appears_to_always_be_defined.code,
Diagnostics.This_condition_will_always_return_true_since_this_0_is_always_defined.code,
Diagnostics.Type_0_is_not_an_array_type.code,
Diagnostics.Type_0_is_not_an_array_type_or_a_string_type.code,
Diagnostics.Type_0_is_not_an_array_type_or_a_string_type_Use_compiler_option_downlevelIteration_to_allow_iterating_of_iterators.code,
Expand Down
2 changes: 1 addition & 1 deletion src/services/codefixes/fixMissingCallParentheses.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
namespace ts.codefix {
const fixId = "fixMissingCallParentheses";
const errorCodes = [
Diagnostics.This_condition_will_always_return_true_since_this_function_appears_to_always_be_defined_Did_you_mean_to_call_it_instead.code,
Diagnostics.This_condition_will_always_return_true_since_this_function_is_always_defined_Did_you_mean_to_call_it_instead.code,
];

registerCodeFix({
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
tests/cases/compiler/truthinessCallExpressionCoercion.ts(2,9): error TS2774: This condition will always return true since this function appears to always be defined. Did you mean to call it instead?
tests/cases/compiler/truthinessCallExpressionCoercion.ts(18,9): error TS2774: This condition will always return true since this function appears to always be defined. Did you mean to call it instead?
tests/cases/compiler/truthinessCallExpressionCoercion.ts(36,9): error TS2774: This condition will always return true since this function appears to always be defined. Did you mean to call it instead?
tests/cases/compiler/truthinessCallExpressionCoercion.ts(50,9): error TS2774: This condition will always return true since this function appears to always be defined. Did you mean to call it instead?
tests/cases/compiler/truthinessCallExpressionCoercion.ts(66,13): error TS2774: This condition will always return true since this function appears to always be defined. Did you mean to call it instead?
tests/cases/compiler/truthinessCallExpressionCoercion.ts(76,9): error TS2774: This condition will always return true since this function appears to always be defined. Did you mean to call it instead?
tests/cases/compiler/truthinessCallExpressionCoercion.ts(82,9): error TS2774: This condition will always return true since this function appears to always be defined. Did you mean to call it instead?
tests/cases/compiler/truthinessCallExpressionCoercion.ts(2,9): error TS2774: This condition will always return true since this function is always defined. Did you mean to call it instead?
tests/cases/compiler/truthinessCallExpressionCoercion.ts(18,9): error TS2774: This condition will always return true since this function is always defined. Did you mean to call it instead?
tests/cases/compiler/truthinessCallExpressionCoercion.ts(36,9): error TS2774: This condition will always return true since this function is always defined. Did you mean to call it instead?
tests/cases/compiler/truthinessCallExpressionCoercion.ts(50,9): error TS2774: This condition will always return true since this function is always defined. Did you mean to call it instead?
tests/cases/compiler/truthinessCallExpressionCoercion.ts(66,13): error TS2774: This condition will always return true since this function is always defined. Did you mean to call it instead?
tests/cases/compiler/truthinessCallExpressionCoercion.ts(76,9): error TS2774: This condition will always return true since this function is always defined. Did you mean to call it instead?
tests/cases/compiler/truthinessCallExpressionCoercion.ts(82,9): error TS2774: This condition will always return true since this function is always defined. Did you mean to call it instead?


==== tests/cases/compiler/truthinessCallExpressionCoercion.ts (7 errors) ====
function onlyErrorsWhenTestingNonNullableFunctionType(required: () => boolean, optional?: () => boolean) {
if (required) { // error
~~~~~~~~
!!! error TS2774: This condition will always return true since this function appears to always be defined. Did you mean to call it instead?
!!! error TS2774: This condition will always return true since this function is always defined. Did you mean to call it instead?
}

if (optional) { // ok
Expand All @@ -29,7 +29,7 @@ tests/cases/compiler/truthinessCallExpressionCoercion.ts(82,9): error TS2774: Th

if (test) { // error
~~~~
!!! error TS2774: This condition will always return true since this function appears to always be defined. Did you mean to call it instead?
!!! error TS2774: This condition will always return true since this function is always defined. Did you mean to call it instead?
console.log('test');
}

Expand All @@ -49,7 +49,7 @@ tests/cases/compiler/truthinessCallExpressionCoercion.ts(82,9): error TS2774: Th

if (test) { // error
~~~~
!!! error TS2774: This condition will always return true since this function appears to always be defined. Did you mean to call it instead?
!!! error TS2774: This condition will always return true since this function is always defined. Did you mean to call it instead?
[() => null].forEach(test => {
test();
});
Expand All @@ -65,7 +65,7 @@ tests/cases/compiler/truthinessCallExpressionCoercion.ts(82,9): error TS2774: Th

if (x.foo.bar) { // error
~~~~~~~~~
!!! error TS2774: This condition will always return true since this function appears to always be defined. Did you mean to call it instead?
!!! error TS2774: This condition will always return true since this function is always defined. Did you mean to call it instead?
}

if (x.foo.bar) { // ok
Expand All @@ -83,7 +83,7 @@ tests/cases/compiler/truthinessCallExpressionCoercion.ts(82,9): error TS2774: Th
test() {
if (this.isUser) { // error
~~~~~~~~~~~
!!! error TS2774: This condition will always return true since this function appears to always be defined. Did you mean to call it instead?
!!! error TS2774: This condition will always return true since this function is always defined. Did you mean to call it instead?
}

if (this.maybeIsUser) { // ok
Expand All @@ -95,15 +95,15 @@ tests/cases/compiler/truthinessCallExpressionCoercion.ts(82,9): error TS2774: Th
function A(stats: StatsBase<any>) {
if (stats.isDirectory) { // err
~~~~~~~~~~~~~~~~~
!!! error TS2774: This condition will always return true since this function appears to always be defined. Did you mean to call it instead?
!!! error TS2774: This condition will always return true since this function is always defined. Did you mean to call it instead?
console.log(`[Directory] ${stats.ctime}`)
}
}

function B(a: Nested, b: Nested) {
if (a.stats.isDirectory) { // err
~~~~~~~~~~~~~~~~~~~
!!! error TS2774: This condition will always return true since this function appears to always be defined. Did you mean to call it instead?
!!! error TS2774: This condition will always return true since this function is always defined. Did you mean to call it instead?
b.stats.isDirectory();
}
if (a.stats.isDirectory) { // ok
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
tests/cases/compiler/truthinessCallExpressionCoercion1.ts(3,5): error TS2774: This condition will always return true since this function appears to always be defined. Did you mean to call it instead?
tests/cases/compiler/truthinessCallExpressionCoercion1.ts(19,5): error TS2774: This condition will always return true since this function appears to always be defined. Did you mean to call it instead?
tests/cases/compiler/truthinessCallExpressionCoercion1.ts(33,5): error TS2774: This condition will always return true since this function appears to always be defined. Did you mean to call it instead?
tests/cases/compiler/truthinessCallExpressionCoercion1.ts(46,5): error TS2774: This condition will always return true since this function appears to always be defined. Did you mean to call it instead?
tests/cases/compiler/truthinessCallExpressionCoercion1.ts(76,9): error TS2774: This condition will always return true since this function appears to always be defined. Did you mean to call it instead?
tests/cases/compiler/truthinessCallExpressionCoercion1.ts(3,5): error TS2774: This condition will always return true since this function is always defined. Did you mean to call it instead?
tests/cases/compiler/truthinessCallExpressionCoercion1.ts(19,5): error TS2774: This condition will always return true since this function is always defined. Did you mean to call it instead?
tests/cases/compiler/truthinessCallExpressionCoercion1.ts(33,5): error TS2774: This condition will always return true since this function is always defined. Did you mean to call it instead?
tests/cases/compiler/truthinessCallExpressionCoercion1.ts(46,5): error TS2774: This condition will always return true since this function is always defined. Did you mean to call it instead?
tests/cases/compiler/truthinessCallExpressionCoercion1.ts(76,9): error TS2774: This condition will always return true since this function is always defined. Did you mean to call it instead?


==== tests/cases/compiler/truthinessCallExpressionCoercion1.ts (5 errors) ====
function onlyErrorsWhenTestingNonNullableFunctionType(required: () => boolean, optional?: () => boolean) {
// error
required ? console.log('required') : undefined;
~~~~~~~~
!!! error TS2774: This condition will always return true since this function appears to always be defined. Did you mean to call it instead?
!!! error TS2774: This condition will always return true since this function is always defined. Did you mean to call it instead?

// ok
optional ? console.log('optional') : undefined;
Expand All @@ -28,7 +28,7 @@ tests/cases/compiler/truthinessCallExpressionCoercion1.ts(76,9): error TS2774: T
// error
test ? console.log('test') : undefined;
~~~~
!!! error TS2774: This condition will always return true since this function appears to always be defined. Did you mean to call it instead?
!!! error TS2774: This condition will always return true since this function is always defined. Did you mean to call it instead?

// ok
test ? console.log(test) : undefined;
Expand All @@ -44,7 +44,7 @@ tests/cases/compiler/truthinessCallExpressionCoercion1.ts(76,9): error TS2774: T
// error
test
~~~~
!!! error TS2774: This condition will always return true since this function appears to always be defined. Did you mean to call it instead?
!!! error TS2774: This condition will always return true since this function is always defined. Did you mean to call it instead?
? [() => null].forEach(test => { test() })
: undefined;
}
Expand All @@ -59,7 +59,7 @@ tests/cases/compiler/truthinessCallExpressionCoercion1.ts(76,9): error TS2774: T
// error
x.foo.bar ? console.log('x.foo.bar') : undefined;
~~~~~~~~~
!!! error TS2774: This condition will always return true since this function appears to always be defined. Did you mean to call it instead?
!!! error TS2774: This condition will always return true since this function is always defined. Did you mean to call it instead?

// ok
x.foo.bar ? x.foo.bar : undefined;
Expand Down Expand Up @@ -91,7 +91,7 @@ tests/cases/compiler/truthinessCallExpressionCoercion1.ts(76,9): error TS2774: T
// error
this.isUser ? console.log('this.isUser') : undefined;
~~~~~~~~~~~
!!! error TS2774: This condition will always return true since this function appears to always be defined. Did you mean to call it instead?
!!! error TS2774: This condition will always return true since this function is always defined. Did you mean to call it instead?

// ok
this.maybeIsUser ? console.log('this.maybeIsUser') : undefined;
Expand Down
Loading