From 9f7a0146d0bde3ef6e6c0be0a634c58d7b7b76c4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Wed, 26 Sep 2012 20:15:46 +0200 Subject: [PATCH] AssignIf: Detect mistake in such code: 'int x=y&4; if ((x==3)||..' --- lib/checkassignif.cpp | 18 +++++++++++------- lib/checkassignif.h | 4 ++-- test/testassignif.cpp | 6 ++++++ 3 files changed, 19 insertions(+), 9 deletions(-) diff --git a/lib/checkassignif.cpp b/lib/checkassignif.cpp index 8f61b74c2..484d587af 100644 --- a/lib/checkassignif.cpp +++ b/lib/checkassignif.cpp @@ -54,13 +54,17 @@ void CheckAssignIf::assignIf() for (const Token *tok2 = tok->tokAt(4); tok2; tok2 = tok2->next()) { if (tok2->str() == "(" || tok2->str() == "}" || tok2->str() == "=") break; - if (Token::Match(tok2,"if ( %varid% %any% %num% &&|%oror%|)", varid)) { - const std::string& op(tok2->strAt(3)); - const MathLib::bigint num2 = MathLib::toLongNumber(tok2->strAt(4)); + if (Token::Match(tok2,"if ( (| %varid% %any% %num% &&|%oror%|)", varid)) { + const Token *vartok = tok2->tokAt(tok2->strAt(2)=="(" ? 3 : 2); + const std::string& op(vartok->strAt(1)); + const MathLib::bigint num2 = MathLib::toLongNumber(vartok->strAt(2)); + std::string condition; + if (Token::simpleMatch(tok2, "if ( (")) + condition = "'" + vartok->str() + op + vartok->strAt(2) + "'"; if (op == "==" && (num & num2) != ((bitop=='&') ? num2 : num)) - assignIfError(tok2, false); + assignIfError(tok2, condition, false); else if (op == "!=" && (num & num2) != ((bitop=='&') ? num2 : num)) - assignIfError(tok2, true); + assignIfError(tok2, condition, true); break; } } @@ -68,11 +72,11 @@ void CheckAssignIf::assignIf() } } -void CheckAssignIf::assignIfError(const Token *tok, bool result) +void CheckAssignIf::assignIfError(const Token *tok, const std::string &condition, bool result) { reportError(tok, Severity::style, "assignIfError", - "Mismatching assignment and comparison, comparison is always " + std::string(result ? "true" : "false") + "."); + "Mismatching assignment and comparison, comparison " + condition + (condition.empty()?"":" ") + "is always " + std::string(result ? "true" : "false") + "."); } diff --git a/lib/checkassignif.h b/lib/checkassignif.h index e72438ba4..7c1f50d89 100644 --- a/lib/checkassignif.h +++ b/lib/checkassignif.h @@ -63,7 +63,7 @@ public: private: - void assignIfError(const Token *tok, bool result); + void assignIfError(const Token *tok, const std::string &condition, bool result); void comparisonError(const Token *tok, const std::string &bitop, @@ -76,7 +76,7 @@ private: void getErrorMessages(ErrorLogger *errorLogger, const Settings *settings) const { CheckAssignIf c(0, settings, errorLogger); - c.assignIfError(0, false); + c.assignIfError(0, "", false); c.comparisonError(0, "&", 6, "==", 1, false); c.multiConditionError(0,1); } diff --git a/test/testassignif.cpp b/test/testassignif.cpp index 58b6247d5..5701687aa 100644 --- a/test/testassignif.cpp +++ b/test/testassignif.cpp @@ -74,6 +74,12 @@ private: "}\n"); ASSERT_EQUALS("[test.cpp:4]: (style) Mismatching assignment and comparison, comparison is always true.\n", errout.str()); + check("void foo(int x) {\n" + " int y = x & 4;\n" + " if ((y == 3) && (z == 1));\n" + "}\n"); + ASSERT_EQUALS("[test.cpp:3]: (style) Mismatching assignment and comparison, comparison 'y==3' is always false.\n", errout.str()); + // | check("void foo(int x) {\n" " int y = x | 0x14;\n"