From 7760a92930c47c0f7a9822777a7edbe2833dfd5d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Thu, 29 Nov 2012 10:19:52 +0100 Subject: [PATCH] CheckAssignIf: Better handling of various expressions in assignments --- lib/checkassignif.cpp | 22 ++++++++++++++++++---- test/testassignif.cpp | 13 +++++++++++++ 2 files changed, 31 insertions(+), 4 deletions(-) diff --git a/lib/checkassignif.cpp b/lib/checkassignif.cpp index 7c905a3fc..92a63d7aa 100644 --- a/lib/checkassignif.cpp +++ b/lib/checkassignif.cpp @@ -42,15 +42,29 @@ void CheckAssignIf::assignIf() if (tok->str() != "=") continue; - if (Token::Match(tok->tokAt(-2), "[;{}] %var% = %var% [&|] %num% ;")) { + if (Token::Match(tok->tokAt(-2), "[;{}] %var% =")) { const unsigned int varid(tok->previous()->varId()); if (varid == 0) continue; - const char bitop(tok->strAt(2).at(0)); + char bitop = '\0'; + MathLib::bigint num = 0; - const MathLib::bigint num = MathLib::toLongNumber(tok->strAt(3)); - if (num < 0) + if (Token::Match(tok->next(), "%num% [&|]")) { + bitop = tok->strAt(2).at(0); + num = MathLib::toLongNumber(tok->next()->str()); + } else { + const Token * const endToken = Token::findmatch(tok, ";"); + if (endToken && Token::Match(endToken->tokAt(-2), "[&|] %num% ;")) { + bitop = endToken->strAt(-2).at(0); + num = MathLib::toLongNumber(endToken->previous()->str()); + } + } + + if (bitop == '\0') + continue; + + if (num < 0 && bitop == '|') continue; bool islocal = false; diff --git a/test/testassignif.cpp b/test/testassignif.cpp index 9018e9e02..dfec2c86f 100644 --- a/test/testassignif.cpp +++ b/test/testassignif.cpp @@ -87,6 +87,19 @@ private: "}"); ASSERT_EQUALS("", errout.str()); + // various simple assignments + check("void foo(int x) {\n" + " int y = (x+1) | 1;\n" + " if (y == 2);\n" + "}"); + ASSERT_EQUALS("[test.cpp:2] -> [test.cpp:3]: (style) Mismatching assignment and comparison, comparison 'y==2' is always false.\n", errout.str()); + + check("void foo() {\n" + " int y = 1 | x();\n" + " if (y == 2);\n" + "}"); + ASSERT_EQUALS("[test.cpp:2] -> [test.cpp:3]: (style) Mismatching assignment and comparison, comparison 'y==2' is always false.\n", errout.str()); + // multiple conditions check("void foo(int x) {\n" " int y = x & 4;\n"