From 5a2362b2a0ff493d5df3a95182473e08c6efb37a Mon Sep 17 00:00:00 2001 From: Paul Fultz II Date: Fri, 7 Sep 2018 08:02:34 -0500 Subject: [PATCH] Fix issue 8738: Dont warn about multiconditions when value is known (#1369) --- lib/checkcondition.cpp | 6 +++++- test/testcondition.cpp | 9 +++++++++ test/testother.cpp | 8 ++++++++ 3 files changed, 22 insertions(+), 1 deletion(-) diff --git a/lib/checkcondition.cpp b/lib/checkcondition.cpp index 44215d422..d8b44ad31 100644 --- a/lib/checkcondition.cpp +++ b/lib/checkcondition.cpp @@ -427,7 +427,11 @@ void CheckCondition::multiCondition() break; tok2 = tok2->tokAt(4); - if (isOverlappingCond(cond1, tok2->astOperand2(), true)) + if (cond1 && + tok2->astOperand2() && + !cond1->hasKnownValue() && + !tok2->astOperand2()->hasKnownValue() && + isOverlappingCond(cond1, tok2->astOperand2(), true)) multiConditionError(tok2, cond1->linenr()); } } diff --git a/test/testcondition.cpp b/test/testcondition.cpp index 862c98539..65c42c93f 100644 --- a/test/testcondition.cpp +++ b/test/testcondition.cpp @@ -2542,6 +2542,15 @@ private: " }\n" "}\n"); ASSERT_EQUALS("", errout.str()); + + check("void f() {\n" + " int activate = 0;\n" + " int foo = 0;\n" + " if (activate) {}\n" + " else if (foo) {}\n" + "}\n"); + ASSERT_EQUALS("[test.cpp:4]: (style) Condition 'activate' is always false\n" + "[test.cpp:5]: (style) Condition 'foo' is always false\n", errout.str()); } void checkInvalidTestForOverflow() { diff --git a/test/testother.cpp b/test/testother.cpp index ac8de2f12..b0b27ab4d 100644 --- a/test/testother.cpp +++ b/test/testother.cpp @@ -4623,6 +4623,14 @@ private: "}\n"); ASSERT_EQUALS("[test.cpp:2] -> [test.cpp:3]: (style) The expression 'val < 0' is always false.\n" "[test.cpp:2] -> [test.cpp:4]: (style) The expression 'val < 0' is always false.\n", errout.str()); + + check("void f() {\n" + " int activate = 0;\n" + " int foo = 0;\n" + " if (activate) {}\n" + " else if (foo) {}\n" + "}\n"); + ASSERT_EQUALS("", errout.str()); } void checkSignOfUnsignedVariable() {