Skip to content
Snippets Groups Projects
Commit 042ee8de authored by Christian Kamm's avatar Christian Kamm
Browse files

QmlJS: Don't warn on 'numbervalue == 0'.

Reviewed-by: Roberto Raggi
parent 5d740f46
No related branches found
No related tags found
No related merge requests found
...@@ -540,10 +540,10 @@ bool Check::visit(FunctionExpression *ast) ...@@ -540,10 +540,10 @@ bool Check::visit(FunctionExpression *ast)
return false; return false;
} }
static bool shouldAvoidNonStrictEqualityCheck(ExpressionNode *exp) static bool shouldAvoidNonStrictEqualityCheck(ExpressionNode *exp, const Value *other)
{ {
if (NumericLiteral *literal = cast<NumericLiteral *>(exp)) { if (NumericLiteral *literal = cast<NumericLiteral *>(exp)) {
if (literal->value == 0) if (literal->value == 0 && !other->asNumberValue())
return true; return true;
} else if (cast<TrueLiteral *>(exp) || cast<FalseLiteral *>(exp) || cast<NullExpression *>(exp)) { } else if (cast<TrueLiteral *>(exp) || cast<FalseLiteral *>(exp) || cast<NullExpression *>(exp)) {
return true; return true;
...@@ -560,10 +560,15 @@ static bool shouldAvoidNonStrictEqualityCheck(ExpressionNode *exp) ...@@ -560,10 +560,15 @@ static bool shouldAvoidNonStrictEqualityCheck(ExpressionNode *exp)
bool Check::visit(BinaryExpression *ast) bool Check::visit(BinaryExpression *ast)
{ {
if (ast->op == QSOperator::Equal || ast->op == QSOperator::NotEqual) { if (ast->op == QSOperator::Equal || ast->op == QSOperator::NotEqual) {
if (_options & WarnAllNonStrictEqualityChecks bool warn = _options & WarnAllNonStrictEqualityChecks;
|| (_options & WarnDangerousNonStrictEqualityChecks if (!warn && _options & WarnDangerousNonStrictEqualityChecks) {
&& (shouldAvoidNonStrictEqualityCheck(ast->left) Evaluate eval(&_context);
|| shouldAvoidNonStrictEqualityCheck(ast->right)))) { const Value *lhs = eval(ast->left);
const Value *rhs = eval(ast->right);
warn = shouldAvoidNonStrictEqualityCheck(ast->left, rhs)
|| shouldAvoidNonStrictEqualityCheck(ast->right, lhs);
}
if (warn) {
warning(ast->operatorToken, tr("== and != perform type coercion, use === or !== instead to avoid")); warning(ast->operatorToken, tr("== and != perform type coercion, use === or !== instead to avoid"));
} }
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment