diff --git a/lib/valueflow.cpp b/lib/valueflow.cpp index 532a8cdfc..c6f338620 100644 --- a/lib/valueflow.cpp +++ b/lib/valueflow.cpp @@ -116,11 +116,14 @@ static void valueFlowBeforeCondition(TokenList *tokenlist, ErrorLogger *errorLog // skip if variable is conditionally used in ?: expression. const Token *parent = tok2->astParent(); - while (parent && !Token::Match(parent, "[?:]")) + while (parent && !Token::Match(parent, "%oror%|&&?|:")) parent = parent->astParent(); if (parent) { if (settings->debugwarnings) - bailout(tokenlist, errorLogger, tok2, "no simplification of " + tok2->str() + " within ?: expression"); + bailout(tokenlist, + errorLogger, + tok2, + "no simplification of " + tok2->str() + " within " + (Token::Match(parent,"[?:]") ? "?:" : parent->str()) + " expression"); continue; } diff --git a/test/testvalueflow.cpp b/test/testvalueflow.cpp index 9f3564679..45507a306 100644 --- a/test/testvalueflow.cpp +++ b/test/testvalueflow.cpp @@ -104,6 +104,15 @@ private: "}"; ASSERT_EQUALS(true, testValueOfX(code, 3U, 123)); + // guarding by && + bailout("void f(int x) {\n" + " if (!x || \n" // <- x can be 0 + " a/x) {}\n" // <- x can't be 0 + " if (x==0) {}\n" + "}"); + TODO_ASSERT_EQUALS(true, false, testValueOfX(code, 2U, 0)); + ASSERT_EQUALS(false, testValueOfX(code, 3U, 0)); + // bailout: ?: bailout("void f(int x) {\n" " y = ((x<0) ? x : ((x==2)?3:4));\n"