diff --git a/lib/valueflow.cpp b/lib/valueflow.cpp index 88c2f3aba..06b6ed084 100644 --- a/lib/valueflow.cpp +++ b/lib/valueflow.cpp @@ -1171,6 +1171,11 @@ static void valueFlowTerminatingCondition(TokenList *tokenlist, SymbolDatabase* for (Token* tok = cond.first->next(); tok != scope->bodyEnd; tok = tok->next()) { if (tok == cond.first) continue; + const Token *tokParent = tok->astParent(); + while (tokParent && tokParent != cond.first) + tokParent = tokParent->astParent(); + if (tokParent == cond.first) + continue; if (!Token::Match(tok, "%comp%")) continue; // Skip known values diff --git a/test/testcondition.cpp b/test/testcondition.cpp index 318959a86..10772ee53 100644 --- a/test/testcondition.cpp +++ b/test/testcondition.cpp @@ -38,6 +38,7 @@ private: void run() override { LOAD_LIB_2(settings0.library, "qt.cfg"); + LOAD_LIB_2(settings0.library, "std.cfg"); settings0.addEnabled("style"); settings0.addEnabled("warning"); @@ -1991,9 +1992,6 @@ private: check("void f1(QString s) { if(s.isEmpty()) if(s.length() > 42) {}} "); ASSERT_EQUALS("[test.cpp:1] -> [test.cpp:1]: (warning) Opposite inner 'if' condition leads to a dead code block.\n", errout.str()); - check("void f1(const std::string &s) { if(s.empty()) if(s.size() == 0) {}} "); - ASSERT_EQUALS("", errout.str()); - check("void f1(const std::string &s, bool b) { if(s.empty() || ((s.size() == 1) && b)) {}} "); ASSERT_EQUALS("", errout.str()); @@ -2519,6 +2517,9 @@ private: "[test.cpp:4]: (style) Condition 'x++==2' is always false\n", errout.str()); + check("void f1(const std::string &s) { if(s.empty()) if(s.size() == 0) {}} "); + ASSERT_EQUALS("[test.cpp:1] -> [test.cpp:1]: (style) Condition 's.size()==0' is always true\n", errout.str()); + // Avoid FP when condition comes from macro check("#define NOT !\n" "void f() {\n" @@ -2703,6 +2704,11 @@ private: " }\n" "};\n"); ASSERT_EQUALS("", errout.str()); + + check("void f(const char* x, const char* t) {\n" + " if (!(strcmp(x, y) == 0)) { return; }\n" + "}\n"); + ASSERT_EQUALS("", errout.str()); } void multiConditionAlwaysTrue() {