Skip to content

Uncalled function checks only works with single conditional #42835

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 8 commits into from
Feb 17, 2022
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
95 changes: 52 additions & 43 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33207,7 +33207,7 @@ namespace ts {
if (operator === SyntaxKind.AmpersandAmpersandToken || operator === SyntaxKind.BarBarToken || operator === SyntaxKind.QuestionQuestionToken) {
if (operator === SyntaxKind.AmpersandAmpersandToken) {
const parent = walkUpParenthesizedExpressions(node.parent);
checkTestingKnownTruthyCallableOrAwaitableType(node.left, leftType, isIfStatement(parent) ? parent.thenStatement : undefined);
checkTestingKnownTruthyCallableOrAwaitableType(node.left, isIfStatement(parent) ? parent.thenStatement : undefined);
}
checkTruthinessOfType(leftType, node.left);
}
Expand Down Expand Up @@ -33780,8 +33780,8 @@ namespace ts {
}

function checkConditionalExpression(node: ConditionalExpression, checkMode?: CheckMode): Type {
const type = checkTruthinessExpression(node.condition);
checkTestingKnownTruthyCallableOrAwaitableType(node.condition, type, node.whenTrue);
checkTruthinessExpression(node.condition);
checkTestingKnownTruthyCallableOrAwaitableType(node.condition, node.whenTrue);
const type1 = checkExpression(node.whenTrue, checkMode);
const type2 = checkExpression(node.whenFalse, checkMode);
return getUnionType([type1, type2], UnionReduction.Subtype);
Expand Down Expand Up @@ -37278,8 +37278,8 @@ namespace ts {
function checkIfStatement(node: IfStatement) {
// Grammar checking
checkGrammarStatementInAmbientContext(node);
const type = checkTruthinessExpression(node.expression);
checkTestingKnownTruthyCallableOrAwaitableType(node.expression, type, node.thenStatement);
checkTruthinessExpression(node.expression);
checkTestingKnownTruthyCallableOrAwaitableType(node.expression, node.thenStatement);
checkSourceElement(node.thenStatement);

if (node.thenStatement.kind === SyntaxKind.EmptyStatement) {
Expand All @@ -37289,48 +37289,57 @@ namespace ts {
checkSourceElement(node.elseStatement);
}

function checkTestingKnownTruthyCallableOrAwaitableType(condExpr: Expression, type: Type, body?: Statement | Expression) {
function checkTestingKnownTruthyCallableOrAwaitableType(condExpr: Expression, body?: Statement | Expression) {
if (!strictNullChecks) return;
if (getFalsyFlags(type)) return;

const location = isBinaryExpression(condExpr) ? condExpr.right : condExpr;
if (isPropertyAccessExpression(location) && isTypeAssertion(location.expression)) {
return;
}

const testedNode = isIdentifier(location) ? location
: isPropertyAccessExpression(location) ? location.name
: isBinaryExpression(location) && isIdentifier(location.right) ? location.right
: undefined;

// While it technically should be invalid for any known-truthy value
// 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);
const isPromise = !!getAwaitedTypeOfPromise(type);
if (callSignatures.length === 0 && !isPromise) {
return;
}

const testedSymbol = testedNode && getSymbolAtLocation(testedNode);
if (!testedSymbol && !isPromise) {
return;
}
helper(condExpr, body);
while (isBinaryExpression(condExpr) && condExpr.operatorToken.kind === SyntaxKind.BarBarToken) {
condExpr = condExpr.left;
helper(condExpr, body);
}

function helper(condExpr: Expression, body: Expression | Statement | undefined) {
const location = isBinaryExpression(condExpr) &&
(condExpr.operatorToken.kind === SyntaxKind.BarBarToken || condExpr.operatorToken.kind === SyntaxKind.AmpersandAmpersandToken)
? condExpr.right
: condExpr;
const type = checkTruthinessExpression(location);
const isPropertyExpressionCast = isPropertyAccessExpression(location) && isTypeAssertion(location.expression);
if (getFalsyFlags(type) || isPropertyExpressionCast) return;

// While it technically should be invalid for any known-truthy value
// 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);
const isPromise = !!getAwaitedTypeOfPromise(type);
if (callSignatures.length === 0 && !isPromise) {
return;
}

const isUsed = testedSymbol && isBinaryExpression(condExpr.parent) && isSymbolUsedInBinaryExpressionChain(condExpr.parent, testedSymbol)
|| testedSymbol && body && isSymbolUsedInConditionBody(condExpr, body, testedNode, testedSymbol);
if (!isUsed) {
if (isPromise) {
errorAndMaybeSuggestAwait(
location,
/*maybeMissingAwait*/ true,
Diagnostics.This_condition_will_always_return_true_since_this_0_is_always_defined,
getTypeNameForErrorDisplay(type));
const testedNode = isIdentifier(location) ? location
: isPropertyAccessExpression(location) ? location.name
: isBinaryExpression(location) && isIdentifier(location.right) ? location.right
: undefined;
const testedSymbol = testedNode && getSymbolAtLocation(testedNode);
if (!testedSymbol && !isPromise) {
return;
}
else {
error(location, Diagnostics.This_condition_will_always_return_true_since_this_function_is_always_defined_Did_you_mean_to_call_it_instead);

const isUsed = testedSymbol && isBinaryExpression(condExpr.parent) && isSymbolUsedInBinaryExpressionChain(condExpr.parent, testedSymbol)
|| testedSymbol && body && isSymbolUsedInConditionBody(condExpr, body, testedNode, testedSymbol);
if (!isUsed) {
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);
}
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/compiler/debug.ts
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ namespace ts {
}

export function assertNever(member: never, message = "Illegal value:", stackCrawlMark?: AnyFunction): never {
const detail = typeof member === "object" && hasProperty(member, "kind") && hasProperty(member, "pos") && formatSyntaxKind ? "SyntaxKind: " + formatSyntaxKind((member as Node).kind) : JSON.stringify(member);
const detail = typeof member === "object" && hasProperty(member, "kind") && hasProperty(member, "pos") ? "SyntaxKind: " + formatSyntaxKind((member as Node).kind) : JSON.stringify(member);
return fail(`${message} ${detail}`, stackCrawlMark || assertNever);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
tests/cases/compiler/uncalledFunctionChecksInConditional.ts(5,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/uncalledFunctionChecksInConditional.ts(9,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/uncalledFunctionChecksInConditional.ts(9,14): error TS2774: This condition will always return true since this function is always defined. Did you mean to call it instead?
tests/cases/compiler/uncalledFunctionChecksInConditional.ts(13,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/uncalledFunctionChecksInConditional.ts(32,10): error TS2774: This condition will always return true since this function is always defined. Did you mean to call it instead?
tests/cases/compiler/uncalledFunctionChecksInConditional.ts(36,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/uncalledFunctionChecksInConditional.ts(40,22): error TS2774: This condition will always return true since this function is always defined. Did you mean to call it instead?
tests/cases/compiler/uncalledFunctionChecksInConditional.ts(44,16): error TS2774: This condition will always return true since this function is always defined. Did you mean to call it instead?
tests/cases/compiler/uncalledFunctionChecksInConditional.ts(48,22): error TS2774: This condition will always return true since this function is always defined. Did you mean to call it instead?


==== tests/cases/compiler/uncalledFunctionChecksInConditional.ts (9 errors) ====
declare function isFoo(): boolean;
declare function isBar(): boolean;
declare const isUndefinedFoo: (() => boolean) | undefined;

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

if (isFoo || isBar) {
Copy link
Member

Choose a reason for hiding this comment

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

just for safety, can you add a test like x || y || z() || foo?

~~~~~
!!! error TS2774: This condition will always return true since this function is always 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?
// error on isFoo, isBar
}

if (isFoo || isFoo()) {
~~~~~
!!! error TS2774: This condition will always return true since this function is always defined. Did you mean to call it instead?
// error on isFoo
}

if (isUndefinedFoo || isFoo()) {
// no error
}

if (isFoo && isFoo()) {
// no error
}

declare const x: boolean;
declare const ux: boolean | undefined;
declare const y: boolean;
declare const uy: boolean | undefined;
declare function z(): boolean;
declare const uz: (() => boolean) | undefined;

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

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

if (x || y || z() || isFoo) {
~~~~~
!!! error TS2774: This condition will always return true since this function is always defined. Did you mean to call it instead?
// error on isFoo
}

if (x || uy || z || isUndefinedFoo) {
~
!!! error TS2774: This condition will always return true since this function is always defined. Did you mean to call it instead?
// error on z
}

if (ux || y || uz || isFoo) {
~~~~~
!!! error TS2774: This condition will always return true since this function is always defined. Did you mean to call it instead?
// error on isFoo
}

84 changes: 84 additions & 0 deletions tests/baselines/reference/uncalledFunctionChecksInConditional.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
//// [uncalledFunctionChecksInConditional.ts]
declare function isFoo(): boolean;
declare function isBar(): boolean;
declare const isUndefinedFoo: (() => boolean) | undefined;

if (isFoo) {
// error on isFoo
}

if (isFoo || isBar) {
// error on isFoo, isBar
}

if (isFoo || isFoo()) {
// error on isFoo
}

if (isUndefinedFoo || isFoo()) {
// no error
}

if (isFoo && isFoo()) {
// no error
}

declare const x: boolean;
declare const ux: boolean | undefined;
declare const y: boolean;
declare const uy: boolean | undefined;
declare function z(): boolean;
declare const uz: (() => boolean) | undefined;

if (x || isFoo) {
// error on isFoo
}

if (isFoo || x) {
// error on isFoo
}

if (x || y || z() || isFoo) {
// error on isFoo
}

if (x || uy || z || isUndefinedFoo) {
// error on z
}

if (ux || y || uz || isFoo) {
// error on isFoo
}


//// [uncalledFunctionChecksInConditional.js]
if (isFoo) {
// error on isFoo
}
if (isFoo || isBar) {
// error on isFoo, isBar
}
if (isFoo || isFoo()) {
// error on isFoo
}
if (isUndefinedFoo || isFoo()) {
// no error
}
if (isFoo && isFoo()) {
// no error
}
if (x || isFoo) {
// error on isFoo
}
if (isFoo || x) {
// error on isFoo
}
if (x || y || z() || isFoo) {
// error on isFoo
}
if (x || uy || z || isUndefinedFoo) {
// error on z
}
if (ux || y || uz || isFoo) {
// error on isFoo
}
Loading