From cf24ea5221b5785114297236c194223203a62c54 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matthias=20Kr=C3=BCger?= Date: Wed, 23 Nov 2016 19:59:00 +0100 Subject: [PATCH] fix #7783: false positive knownConditionTrueFalse on assert(0 && "message"). --- lib/checkcondition.cpp | 12 ++++++++++-- test/testcondition.cpp | 14 ++++++++++++++ 2 files changed, 24 insertions(+), 2 deletions(-) diff --git a/lib/checkcondition.cpp b/lib/checkcondition.cpp index f6b44aa92..148aeb606 100644 --- a/lib/checkcondition.cpp +++ b/lib/checkcondition.cpp @@ -991,8 +991,16 @@ void CheckCondition::alwaysTrueFalse() // Don't warn in assertions. Condition is often 'always true' by intention. // If platform,defines,etc cause 'always false' then that is not dangerous neither. - const std::string str = tok->astParent() && tok->astParent()->previous() ? tok->astParent()->previous()->str() : ""; - if (str.find("assert")!=std::string::npos || str.find("ASSERT")!=std::string::npos) + bool assertFound = false; + for (const Token * tok2 = tok->astParent(); tok2 ; tok2 = tok2->astParent()) { // move backwards and try to find "assert" + if (tok2->str() == "(" && tok2->astOperand2()) { + const std::string& str = tok2->previous()->str(); + if ((str.find("assert")!=std::string::npos || str.find("ASSERT")!=std::string::npos)) + assertFound = true; + break; + } + } + if (assertFound) continue; // Don't warn when there are expanded macros.. diff --git a/test/testcondition.cpp b/test/testcondition.cpp index a6a0991f7..51212f662 100644 --- a/test/testcondition.cpp +++ b/test/testcondition.cpp @@ -1775,6 +1775,20 @@ private: "}"); ASSERT_EQUALS("", errout.str()); + // #7783 FP knownConditionTrueFalse on assert(0 && "message") + check("void foo(int x) {\n" + " if (x<0)\n" + " {\n" + " assert(0 && \"bla\");\n" + " ASSERT(0 && \"bla\");\n" + " assert_foo(0 && \"bla\");\n" + " ASSERT_FOO(0 && \"bla\");\n" + " assert((int)(0==0));\n" + " assert((int)(0==0) && \"bla\");\n" + " }\n" + "}\n"); + ASSERT_EQUALS("", errout.str()); + // #7750 warn about number and char literals in boolean expressions check("void f() {\n" " if('a'){}\n"