From 042ee8de010ee442c1ed818fac4650674131f7ce Mon Sep 17 00:00:00 2001 From: Christian Kamm <christian.d.kamm@nokia.com> Date: Mon, 29 Nov 2010 11:20:07 +0100 Subject: [PATCH] QmlJS: Don't warn on 'numbervalue == 0'. Reviewed-by: Roberto Raggi --- src/libs/qmljs/qmljscheck.cpp | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/src/libs/qmljs/qmljscheck.cpp b/src/libs/qmljs/qmljscheck.cpp index 52ccde81a72..389053bc448 100644 --- a/src/libs/qmljs/qmljscheck.cpp +++ b/src/libs/qmljs/qmljscheck.cpp @@ -540,10 +540,10 @@ bool Check::visit(FunctionExpression *ast) return false; } -static bool shouldAvoidNonStrictEqualityCheck(ExpressionNode *exp) +static bool shouldAvoidNonStrictEqualityCheck(ExpressionNode *exp, const Value *other) { if (NumericLiteral *literal = cast<NumericLiteral *>(exp)) { - if (literal->value == 0) + if (literal->value == 0 && !other->asNumberValue()) return true; } else if (cast<TrueLiteral *>(exp) || cast<FalseLiteral *>(exp) || cast<NullExpression *>(exp)) { return true; @@ -560,10 +560,15 @@ static bool shouldAvoidNonStrictEqualityCheck(ExpressionNode *exp) bool Check::visit(BinaryExpression *ast) { if (ast->op == QSOperator::Equal || ast->op == QSOperator::NotEqual) { - if (_options & WarnAllNonStrictEqualityChecks - || (_options & WarnDangerousNonStrictEqualityChecks - && (shouldAvoidNonStrictEqualityCheck(ast->left) - || shouldAvoidNonStrictEqualityCheck(ast->right)))) { + bool warn = _options & WarnAllNonStrictEqualityChecks; + if (!warn && _options & WarnDangerousNonStrictEqualityChecks) { + Evaluate eval(&_context); + 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")); } } -- GitLab