From 9462a05e59b349313574f745ad4d0747e8090d06 Mon Sep 17 00:00:00 2001 From: Paul Date: Fri, 8 Oct 2021 18:01:12 -0500 Subject: [PATCH 1/2] Fix 8629: false negative: (style) Condition '...' is always true --- lib/valueflow.cpp | 8 ++++---- test/testcondition.cpp | 10 ++++++++++ 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/lib/valueflow.cpp b/lib/valueflow.cpp index d570cc0a1ff..d44b5351cd2 100644 --- a/lib/valueflow.cpp +++ b/lib/valueflow.cpp @@ -802,11 +802,11 @@ static void setTokenValue(Token* tok, ValueFlow::Value value, const Settings* se // increment else if (parent->str() == "++") { for (const ValueFlow::Value &val : tok->values()) { - if (!val.isIntValue() && !val.isFloatValue()) + if (!val.isIntValue() && !val.isFloatValue() && !val.isSymbolicValue()) continue; ValueFlow::Value v(val); if (parent == tok->previous()) { - if (v.isIntValue()) + if (v.isIntValue() || v.isSymbolicValue()) v.intvalue = v.intvalue + 1; else v.floatValue = v.floatValue + 1.0; @@ -818,11 +818,11 @@ static void setTokenValue(Token* tok, ValueFlow::Value value, const Settings* se // decrement else if (parent->str() == "--") { for (const ValueFlow::Value &val : tok->values()) { - if (!val.isIntValue() && !val.isFloatValue()) + if (!val.isIntValue() && !val.isFloatValue() && !val.isSymbolicValue()) continue; ValueFlow::Value v(val); if (parent == tok->previous()) { - if (v.isIntValue()) + if (v.isIntValue() || v.isSymbolicValue()) v.intvalue = v.intvalue - 1; else v.floatValue = v.floatValue - 1.0; diff --git a/test/testcondition.cpp b/test/testcondition.cpp index a46e0b2d236..777e5d1c7ea 100644 --- a/test/testcondition.cpp +++ b/test/testcondition.cpp @@ -3826,6 +3826,16 @@ class TestCondition : public TestFixture { "}\n"); ASSERT_EQUALS("[test.cpp:4]: (style) Condition 'z<1' is always false\n", errout.str()); + check("bool f(int &index, const int s, const double * const array, double & x) {\n" + " if (index >= s)\n" + " return false;\n" + " else {\n" + " x = array[index];\n" + " return (index++) >= s;\n" + " }\n" + "}\n"); + ASSERT_EQUALS("[test.cpp:6]: (style) Condition '(index++)>=s' is always false\n", errout.str()); + check("struct a {\n" " a *b() const;\n" "} c;\n" From 8cab2f6ab178a70fb647b8deedbe9eb0c19d2d88 Mon Sep 17 00:00:00 2001 From: Paul Date: Fri, 8 Oct 2021 18:02:29 -0500 Subject: [PATCH 2/2] Format --- test/testcondition.cpp | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/test/testcondition.cpp b/test/testcondition.cpp index 777e5d1c7ea..5cfc68d501a 100644 --- a/test/testcondition.cpp +++ b/test/testcondition.cpp @@ -3827,15 +3827,15 @@ class TestCondition : public TestFixture { ASSERT_EQUALS("[test.cpp:4]: (style) Condition 'z<1' is always false\n", errout.str()); check("bool f(int &index, const int s, const double * const array, double & x) {\n" - " if (index >= s)\n" - " return false;\n" - " else {\n" - " x = array[index];\n" - " return (index++) >= s;\n" - " }\n" - "}\n"); + " if (index >= s)\n" + " return false;\n" + " else {\n" + " x = array[index];\n" + " return (index++) >= s;\n" + " }\n" + "}\n"); ASSERT_EQUALS("[test.cpp:6]: (style) Condition '(index++)>=s' is always false\n", errout.str()); - + check("struct a {\n" " a *b() const;\n" "} c;\n"