mirror of
https://github.com/microsoft/TypeScript.git
synced 2026-06-15 03:09:59 -05:00
Flag non-nullable values with call expressions in if statements as errors
This commit is contained in:
@@ -27930,7 +27930,25 @@ namespace ts {
|
||||
// Grammar checking
|
||||
checkGrammarStatementInAmbientContext(node);
|
||||
|
||||
checkTruthinessExpression(node.expression);
|
||||
const type = checkTruthinessExpression(node.expression);
|
||||
|
||||
if (strictNullChecks &&
|
||||
(node.expression.kind === SyntaxKind.Identifier ||
|
||||
node.expression.kind === SyntaxKind.PropertyAccessExpression ||
|
||||
node.expression.kind === SyntaxKind.ElementAccessExpression)) {
|
||||
const possiblyFalsy = !!getFalsyFlags(type);
|
||||
if (!possiblyFalsy) {
|
||||
// While it technically should be invalid for any known-truthy value
|
||||
// to be tested, we de-scope to functions 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) {
|
||||
error(node.expression, Diagnostics.This_condition_will_always_return_true_since_the_function_is_always_defined_Did_you_mean_to_call_it_instead);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
checkSourceElement(node.thenStatement);
|
||||
|
||||
if (node.thenStatement.kind === SyntaxKind.EmptyStatement) {
|
||||
|
||||
@@ -2689,7 +2689,10 @@
|
||||
"category": "Error",
|
||||
"code": 2773
|
||||
},
|
||||
|
||||
"This condition will always return true since the function is always defined. Did you mean to call it instead?": {
|
||||
"category": "Error",
|
||||
"code": 2774
|
||||
},
|
||||
"Import declaration '{0}' is using private name '{1}'.": {
|
||||
"category": "Error",
|
||||
"code": 4000
|
||||
|
||||
Reference in New Issue
Block a user