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

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

Merged
merged 2 commits into from
Apr 13, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
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 unrefenced in
andrewbranch marked this conversation as resolved.
Show resolved Hide resolved
// 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_appears_to_always_be_defined,
getTypeNameForErrorDisplay(type));
}
else {
error(location, Diagnostics.This_condition_will_always_return_true_since_this_function_appears_to_always_be_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
38 changes: 33 additions & 5 deletions tests/baselines/reference/truthinessPromiseCoercion.errors.txt
Original file line number Diff line number Diff line change
@@ -1,24 +1,52 @@
tests/cases/compiler/truthinessPromiseCoercion.ts(5,9): error TS2801: This condition will always return true since this 'Promise<number>' appears to always be defined.
tests/cases/compiler/truthinessPromiseCoercion.ts(9,5): error TS2801: This condition will always return true since this 'Promise<number>' appears to always be defined.
tests/cases/compiler/truthinessPromiseCoercion.ts(6,9): error TS2801: This condition will always return true since this 'Promise<number>' appears to always be defined.
tests/cases/compiler/truthinessPromiseCoercion.ts(10,5): error TS2801: This condition will always return true since this 'Promise<number>' appears to always be defined.
tests/cases/compiler/truthinessPromiseCoercion.ts(31,9): error TS2801: This condition will always return true since this 'Promise<unknown>' appears to always be defined.


==== tests/cases/compiler/truthinessPromiseCoercion.ts (2 errors) ====
==== tests/cases/compiler/truthinessPromiseCoercion.ts (3 errors) ====
declare const p: Promise<number>
declare const p2: null | Promise<number>
declare const obj: { p: Promise<unknown> }

async function f() {
if (p) {} // err
~
!!! error TS2801: This condition will always return true since this 'Promise<number>' appears to always be defined.
!!! related TS2773 tests/cases/compiler/truthinessPromiseCoercion.ts:5:9: Did you forget to use 'await'?
!!! related TS2773 tests/cases/compiler/truthinessPromiseCoercion.ts:6:9: Did you forget to use 'await'?
if (!!p) {} // no err
if (p2) {} // no err

p ? f.arguments : f.arguments;
~
!!! error TS2801: This condition will always return true since this 'Promise<number>' appears to always be defined.
!!! related TS2773 tests/cases/compiler/truthinessPromiseCoercion.ts:9:5: Did you forget to use 'await'?
!!! related TS2773 tests/cases/compiler/truthinessPromiseCoercion.ts:10:5: Did you forget to use 'await'?
!!p ? f.arguments : f.arguments;
p2 ? f.arguments : f.arguments;
}

// all ok
async function g() {
if (p) {
p;
}
if (p && p.then.length) {}
if (p) {
if (p) {
if (p) {
!!await (((((((p)))))));
}
}
}
}

async function h() {
if (obj.p) {} // error
~~~~~
!!! error TS2801: This condition will always return true since this 'Promise<unknown>' appears to always be defined.
!!! related TS2773 tests/cases/compiler/truthinessPromiseCoercion.ts:31:9: Did you forget to use 'await'?
if (obj.p) { // ok
await obj.p;
}
if (obj.p && await obj.p) {} // ok
}

45 changes: 45 additions & 0 deletions tests/baselines/reference/truthinessPromiseCoercion.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
//// [truthinessPromiseCoercion.ts]
declare const p: Promise<number>
declare const p2: null | Promise<number>
declare const obj: { p: Promise<unknown> }

async function f() {
if (p) {} // err
Expand All @@ -11,6 +12,29 @@ async function f() {
!!p ? f.arguments : f.arguments;
p2 ? f.arguments : f.arguments;
}

// all ok
async function g() {
if (p) {
p;
}
if (p && p.then.length) {}
if (p) {
if (p) {
if (p) {
!!await (((((((p)))))));
}
}
}
}

async function h() {
if (obj.p) {} // error
if (obj.p) { // ok
await obj.p;
}
if (obj.p && await obj.p) {} // ok
}


//// [truthinessPromiseCoercion.js]
Expand All @@ -22,3 +46,24 @@ async function f() {
!!p ? f.arguments : f.arguments;
p2 ? f.arguments : f.arguments;
}
// all ok
async function g() {
if (p) {
p;
}
if (p && p.then.length) { }
if (p) {
if (p) {
if (p) {
!!await (((((((p)))))));
}
}
}
}
async function h() {
if (obj.p) { } // error
if (obj.p) { // ok
await obj.p;
}
if (obj.p && await obj.p) { } // ok
}
80 changes: 73 additions & 7 deletions tests/baselines/reference/truthinessPromiseCoercion.symbols
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,13 @@ declare const p2: null | Promise<number>
>p2 : Symbol(p2, Decl(truthinessPromiseCoercion.ts, 1, 13))
>Promise : Symbol(Promise, Decl(lib.es5.d.ts, --, --), Decl(lib.es2015.iterable.d.ts, --, --), Decl(lib.es2015.promise.d.ts, --, --), Decl(lib.es2015.symbol.wellknown.d.ts, --, --), Decl(lib.es2018.promise.d.ts, --, --))

declare const obj: { p: Promise<unknown> }
>obj : Symbol(obj, Decl(truthinessPromiseCoercion.ts, 2, 13))
>p : Symbol(p, Decl(truthinessPromiseCoercion.ts, 2, 20))
>Promise : Symbol(Promise, Decl(lib.es5.d.ts, --, --), Decl(lib.es2015.iterable.d.ts, --, --), Decl(lib.es2015.promise.d.ts, --, --), Decl(lib.es2015.symbol.wellknown.d.ts, --, --), Decl(lib.es2018.promise.d.ts, --, --))

async function f() {
>f : Symbol(f, Decl(truthinessPromiseCoercion.ts, 1, 40))
>f : Symbol(f, Decl(truthinessPromiseCoercion.ts, 2, 42))

if (p) {} // err
>p : Symbol(p, Decl(truthinessPromiseCoercion.ts, 0, 13))
Expand All @@ -22,28 +27,89 @@ async function f() {
p ? f.arguments : f.arguments;
>p : Symbol(p, Decl(truthinessPromiseCoercion.ts, 0, 13))
>f.arguments : Symbol(Function.arguments, Decl(lib.es5.d.ts, --, --))
>f : Symbol(f, Decl(truthinessPromiseCoercion.ts, 1, 40))
>f : Symbol(f, Decl(truthinessPromiseCoercion.ts, 2, 42))
>arguments : Symbol(Function.arguments, Decl(lib.es5.d.ts, --, --))
>f.arguments : Symbol(Function.arguments, Decl(lib.es5.d.ts, --, --))
>f : Symbol(f, Decl(truthinessPromiseCoercion.ts, 1, 40))
>f : Symbol(f, Decl(truthinessPromiseCoercion.ts, 2, 42))
>arguments : Symbol(Function.arguments, Decl(lib.es5.d.ts, --, --))

!!p ? f.arguments : f.arguments;
>p : Symbol(p, Decl(truthinessPromiseCoercion.ts, 0, 13))
>f.arguments : Symbol(Function.arguments, Decl(lib.es5.d.ts, --, --))
>f : Symbol(f, Decl(truthinessPromiseCoercion.ts, 1, 40))
>f : Symbol(f, Decl(truthinessPromiseCoercion.ts, 2, 42))
>arguments : Symbol(Function.arguments, Decl(lib.es5.d.ts, --, --))
>f.arguments : Symbol(Function.arguments, Decl(lib.es5.d.ts, --, --))
>f : Symbol(f, Decl(truthinessPromiseCoercion.ts, 1, 40))
>f : Symbol(f, Decl(truthinessPromiseCoercion.ts, 2, 42))
>arguments : Symbol(Function.arguments, Decl(lib.es5.d.ts, --, --))

p2 ? f.arguments : f.arguments;
>p2 : Symbol(p2, Decl(truthinessPromiseCoercion.ts, 1, 13))
>f.arguments : Symbol(Function.arguments, Decl(lib.es5.d.ts, --, --))
>f : Symbol(f, Decl(truthinessPromiseCoercion.ts, 1, 40))
>f : Symbol(f, Decl(truthinessPromiseCoercion.ts, 2, 42))
>arguments : Symbol(Function.arguments, Decl(lib.es5.d.ts, --, --))
>f.arguments : Symbol(Function.arguments, Decl(lib.es5.d.ts, --, --))
>f : Symbol(f, Decl(truthinessPromiseCoercion.ts, 1, 40))
>f : Symbol(f, Decl(truthinessPromiseCoercion.ts, 2, 42))
>arguments : Symbol(Function.arguments, Decl(lib.es5.d.ts, --, --))
}

// all ok
async function g() {
>g : Symbol(g, Decl(truthinessPromiseCoercion.ts, 12, 1))

if (p) {
>p : Symbol(p, Decl(truthinessPromiseCoercion.ts, 0, 13))

p;
>p : Symbol(p, Decl(truthinessPromiseCoercion.ts, 0, 13))
}
if (p && p.then.length) {}
>p : Symbol(p, Decl(truthinessPromiseCoercion.ts, 0, 13))
>p.then.length : Symbol(Function.length, Decl(lib.es5.d.ts, --, --))
>p.then : Symbol(Promise.then, Decl(lib.es5.d.ts, --, --))
>p : Symbol(p, Decl(truthinessPromiseCoercion.ts, 0, 13))
>then : Symbol(Promise.then, Decl(lib.es5.d.ts, --, --))
>length : Symbol(Function.length, Decl(lib.es5.d.ts, --, --))

if (p) {
>p : Symbol(p, Decl(truthinessPromiseCoercion.ts, 0, 13))

if (p) {
>p : Symbol(p, Decl(truthinessPromiseCoercion.ts, 0, 13))

if (p) {
>p : Symbol(p, Decl(truthinessPromiseCoercion.ts, 0, 13))

!!await (((((((p)))))));
>p : Symbol(p, Decl(truthinessPromiseCoercion.ts, 0, 13))
}
}
}
}

async function h() {
>h : Symbol(h, Decl(truthinessPromiseCoercion.ts, 27, 1))

if (obj.p) {} // error
>obj.p : Symbol(p, Decl(truthinessPromiseCoercion.ts, 2, 20))
>obj : Symbol(obj, Decl(truthinessPromiseCoercion.ts, 2, 13))
>p : Symbol(p, Decl(truthinessPromiseCoercion.ts, 2, 20))

if (obj.p) { // ok
>obj.p : Symbol(p, Decl(truthinessPromiseCoercion.ts, 2, 20))
>obj : Symbol(obj, Decl(truthinessPromiseCoercion.ts, 2, 13))
>p : Symbol(p, Decl(truthinessPromiseCoercion.ts, 2, 20))

await obj.p;
>obj.p : Symbol(p, Decl(truthinessPromiseCoercion.ts, 2, 20))
>obj : Symbol(obj, Decl(truthinessPromiseCoercion.ts, 2, 13))
>p : Symbol(p, Decl(truthinessPromiseCoercion.ts, 2, 20))
}
if (obj.p && await obj.p) {} // ok
>obj.p : Symbol(p, Decl(truthinessPromiseCoercion.ts, 2, 20))
>obj : Symbol(obj, Decl(truthinessPromiseCoercion.ts, 2, 13))
>p : Symbol(p, Decl(truthinessPromiseCoercion.ts, 2, 20))
>obj.p : Symbol(p, Decl(truthinessPromiseCoercion.ts, 2, 20))
>obj : Symbol(obj, Decl(truthinessPromiseCoercion.ts, 2, 13))
>p : Symbol(p, Decl(truthinessPromiseCoercion.ts, 2, 20))
}

79 changes: 79 additions & 0 deletions tests/baselines/reference/truthinessPromiseCoercion.types
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ declare const p2: null | Promise<number>
>p2 : Promise<number> | null
>null : null

declare const obj: { p: Promise<unknown> }
>obj : { p: Promise<unknown>; }
>p : Promise<unknown>

async function f() {
>f : () => Promise<void>

Expand Down Expand Up @@ -53,3 +57,78 @@ async function f() {
>arguments : any
}

// all ok
async function g() {
>g : () => Promise<void>

if (p) {
>p : Promise<number>

p;
>p : Promise<number>
}
if (p && p.then.length) {}
>p && p.then.length : number
>p : Promise<number>
>p.then.length : number
>p.then : <TResult1 = number, TResult2 = never>(onfulfilled?: ((value: number) => TResult1 | PromiseLike<TResult1>) | null | undefined, onrejected?: ((reason: any) => TResult2 | PromiseLike<TResult2>) | null | undefined) => Promise<TResult1 | TResult2>
>p : Promise<number>
>then : <TResult1 = number, TResult2 = never>(onfulfilled?: ((value: number) => TResult1 | PromiseLike<TResult1>) | null | undefined, onrejected?: ((reason: any) => TResult2 | PromiseLike<TResult2>) | null | undefined) => Promise<TResult1 | TResult2>
>length : number

if (p) {
>p : Promise<number>

if (p) {
>p : Promise<number>

if (p) {
>p : Promise<number>

!!await (((((((p)))))));
>!!await (((((((p))))))) : boolean
>!await (((((((p))))))) : boolean
>await (((((((p))))))) : number
>(((((((p))))))) : Promise<number>
>((((((p)))))) : Promise<number>
>(((((p))))) : Promise<number>
>((((p)))) : Promise<number>
>(((p))) : Promise<number>
>((p)) : Promise<number>
>(p) : Promise<number>
>p : Promise<number>
}
}
}
}

async function h() {
>h : () => Promise<void>

if (obj.p) {} // error
>obj.p : Promise<unknown>
>obj : { p: Promise<unknown>; }
>p : Promise<unknown>

if (obj.p) { // ok
>obj.p : Promise<unknown>
>obj : { p: Promise<unknown>; }
>p : Promise<unknown>

await obj.p;
>await obj.p : unknown
>obj.p : Promise<unknown>
>obj : { p: Promise<unknown>; }
>p : Promise<unknown>
}
if (obj.p && await obj.p) {} // ok
>obj.p && await obj.p : unknown
>obj.p : Promise<unknown>
>obj : { p: Promise<unknown>; }
>p : Promise<unknown>
>await obj.p : unknown
>obj.p : Promise<unknown>
>obj : { p: Promise<unknown>; }
>p : Promise<unknown>
}

Loading