From c82d53d4ae413004dae3b2b7dc0da55fd8cfb077 Mon Sep 17 00:00:00 2001 From: Christian Kamm Date: Fri, 30 Sep 2011 10:39:00 +0200 Subject: [PATCH] QmlJS checks: Add confusing +/- check. Migrated from QtChecker. Change-Id: I06115152f979f9abfa9cbdf4a3e2e63a51ea7284 Reviewed-on: http://codereview.qt-project.org/5858 Reviewed-by: Fawzi Mohamed Sanity-Review: Qt Sanity Bot --- src/libs/qmljs/qmljscheck.cpp | 29 +++++++++++++++++++ src/libs/qmljs/qmljsstaticanalysismessage.cpp | 4 --- src/libs/qmljs/qmljsstaticanalysismessage.h | 2 -- .../codemodel/check/confusing-plus-minus.qml | 14 +++++++++ tests/auto/qml/codemodel/check/tst_check.cpp | 2 +- 5 files changed, 44 insertions(+), 7 deletions(-) create mode 100644 tests/auto/qml/codemodel/check/confusing-plus-minus.qml diff --git a/src/libs/qmljs/qmljscheck.cpp b/src/libs/qmljs/qmljscheck.cpp index 81da022205b..f22588e3a40 100644 --- a/src/libs/qmljs/qmljscheck.cpp +++ b/src/libs/qmljs/qmljscheck.cpp @@ -873,6 +873,35 @@ bool Check::visit(BinaryExpression *ast) addMessage(MaybeWarnEqualityTypeCoercion, ast->operatorToken); } } + + // check odd + ++ combinations + const QLatin1Char newline('\n'); + if (ast->op == QSOperator::Add || ast->op == QSOperator::Sub) { + QChar match; + Type msg; + if (ast->op == QSOperator::Add) { + match = QLatin1Char('+'); + msg = WarnConfusingPluses; + } else { + QTC_CHECK(ast->op == QSOperator::Sub); + match = QLatin1Char('-'); + msg = WarnConfusingMinuses; + } + + if (int(op.end()) + 1 < source.size()) { + const QChar next = source.at(op.end()); + if (next.isSpace() && next != newline + && source.at(op.end() + 1) == match) + addMessage(msg, SourceLocation(op.begin(), 3, op.startLine, op.startColumn)); + } + if (op.begin() >= 2) { + const QChar prev = source.at(op.begin() - 1); + if (prev.isSpace() && prev != newline + && source.at(op.begin() - 2) == match) + addMessage(msg, SourceLocation(op.begin() - 2, 3, op.startLine, op.startColumn - 2)); + } + } + return true; } diff --git a/src/libs/qmljs/qmljsstaticanalysismessage.cpp b/src/libs/qmljs/qmljsstaticanalysismessage.cpp index 7b27c235a9f..3ba57865833 100644 --- a/src/libs/qmljs/qmljsstaticanalysismessage.cpp +++ b/src/libs/qmljs/qmljsstaticanalysismessage.cpp @@ -146,12 +146,8 @@ StaticAnalysisMessages::StaticAnalysisMessages() tr("do not use void expressions")); newMsg(WarnConfusingPluses, Warning, tr("confusing pluses")); - newMsg(WarnConfusingPreincrement, Warning, - tr("confusing preincrement")); newMsg(WarnConfusingMinuses, Warning, tr("confusing minuses")); - newMsg(WarnConfusingPredecrement, Warning, - tr("confusing predecrement")); newMsg(HintDeclareVarsInOneLine, Hint, tr("declare all function vars on a single line")); // unused diff --git a/src/libs/qmljs/qmljsstaticanalysismessage.h b/src/libs/qmljs/qmljsstaticanalysismessage.h index 6e9b0c39cc2..27be6e610b2 100644 --- a/src/libs/qmljs/qmljsstaticanalysismessage.h +++ b/src/libs/qmljs/qmljsstaticanalysismessage.h @@ -88,9 +88,7 @@ enum Type WarnBlock = 115, WarnVoid = 116, WarnConfusingPluses = 117, - WarnConfusingPreincrement = 118, WarnConfusingMinuses = 119, - WarnConfusingPredecrement = 120, HintDeclareVarsInOneLine = 121, HintExtraParentheses = 123, MaybeWarnEqualityTypeCoercion = 126, diff --git a/tests/auto/qml/codemodel/check/confusing-plus-minus.qml b/tests/auto/qml/codemodel/check/confusing-plus-minus.qml new file mode 100644 index 00000000000..f371f92936c --- /dev/null +++ b/tests/auto/qml/codemodel/check/confusing-plus-minus.qml @@ -0,0 +1,14 @@ +import QtQuick 1.0 + +Item { + function foo() { + var x, y + x = 1 + +2 // 117 15 17 + x = 1 + ++x // 117 15 17 + x = x++ + x // 117 15 17 + x = 1 - -2 // 119 15 17 + x = 1 - --x // 119 15 17 + x = x-- - x // 119 15 17 + x = x + --y + y-- + x++ - y + } +} diff --git a/tests/auto/qml/codemodel/check/tst_check.cpp b/tests/auto/qml/codemodel/check/tst_check.cpp index d782ab19b7e..72aeabbdc03 100644 --- a/tests/auto/qml/codemodel/check/tst_check.cpp +++ b/tests/auto/qml/codemodel/check/tst_check.cpp @@ -162,7 +162,7 @@ void tst_Check::test() fail |= !QCOMPARE_NOEXIT(actual.location.startLine, expected.location.startLine); if (fail) return; - fail |= !QCOMPARE_NOEXIT(actual.type, expected.type); + fail |= !QCOMPARE_NOEXIT((int)actual.type, (int)expected.type); fail |= !QCOMPARE_NOEXIT(actual.location.startColumn, expected.location.startColumn); fail |= !QCOMPARE_NOEXIT(actual.location.offset, expected.location.offset); fail |= !QCOMPARE_NOEXIT(actual.location.length, expected.location.length);