From 0c1dff5c9390c43a2eea5e1738182d9b9bbe2921 Mon Sep 17 00:00:00 2001 From: Paul Fultz II Date: Tue, 13 Aug 2019 23:34:27 -0500 Subject: [PATCH] Fix issue 9268: false negative: (style) Condition '...' is always true (#2080) * Fix issue 9268: false negative: (style) Condition '...' is always true * Fix copy and paste mistake --- lib/valueflow.cpp | 3 +++ test/testcondition.cpp | 9 +++++++++ 2 files changed, 12 insertions(+) diff --git a/lib/valueflow.cpp b/lib/valueflow.cpp index 83c62d247..272d1bcf4 100644 --- a/lib/valueflow.cpp +++ b/lib/valueflow.cpp @@ -3911,6 +3911,7 @@ struct ValueFlowConditionHandler { startTokens[1] = top->link()->linkAt(1)->tokAt(2); bool bail = false; + const bool bothCanBeKnown = check_if && check_else && !Token::Match(tok->astParent(), "&&|%oror%"); for (int i = 0; i < 2; i++) { const Token *const startToken = startTokens[i]; @@ -3918,6 +3919,8 @@ struct ValueFlowConditionHandler { continue; std::list &values = (i == 0 ? cond.true_values : cond.false_values); valueFlowSetConditionToKnown(tok, values, i == 0); + if (bothCanBeKnown) + valueFlowSetConditionToKnown(tok, values, i != 0); bool changed = forward(startTokens[i], startTokens[i]->link(), var, values, true); values.front().setPossible(); diff --git a/test/testcondition.cpp b/test/testcondition.cpp index 614869b6e..998159050 100644 --- a/test/testcondition.cpp +++ b/test/testcondition.cpp @@ -3197,6 +3197,15 @@ private: " }\n" "}\n"); ASSERT_EQUALS("[test.cpp:3] -> [test.cpp:5]: (style) Condition 'array' is always true\n", errout.str()); + + check("void f(int *array, int size ) {\n" + " for(int i = 0; i < size; ++i) {\n" + " if(array == 0)\n" + " continue;\n" + " else if(array){}\n" + " }\n" + "}\n"); + ASSERT_EQUALS("[test.cpp:3] -> [test.cpp:5]: (style) Condition 'array' is always true\n", errout.str()); } void multiConditionAlwaysTrue() {