From 3644d79162f4beba6aeef782bff59b8e79d5988e Mon Sep 17 00:00:00 2001 From: chrchr-github <78114321+chrchr-github@users.noreply.github.com> Date: Tue, 20 Sep 2022 21:57:27 +0200 Subject: [PATCH] Fix duplicateConditionalAssign FP (#4484) * Fix #9406 FN redundant assignment of Boolean variable * Fix warning, refactor * Update testcondition.cpp * Fix duplicateConditionalAssign FP * Format --- lib/checkcondition.cpp | 9 ++++++--- test/testcondition.cpp | 6 ++++++ 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/lib/checkcondition.cpp b/lib/checkcondition.cpp index 5d0e77957..3455ef009 100644 --- a/lib/checkcondition.cpp +++ b/lib/checkcondition.cpp @@ -1764,10 +1764,13 @@ void CheckCondition::checkDuplicateConditionalAssign() continue; bool isRedundant = false; if (isBoolVar) { - if (!astIsBool(condTok)) - continue; const bool isNegation = condTok->str() == "!"; - if (!(assignTok->astOperand1() && assignTok->astOperand1()->varId() == (isNegation ? condTok->next() : condTok)->varId())) + const Token* const varTok = isNegation ? condTok->next() : condTok; + const ValueType* vt = varTok->variable() ? varTok->variable()->valueType() : nullptr; + if (!(vt && vt->type == ValueType::Type::BOOL && !vt->pointer)) + continue; + + if (!(assignTok->astOperand1() && assignTok->astOperand1()->varId() == varTok->varId())) continue; if (!(assignTok->astOperand2() && assignTok->astOperand2()->hasKnownIntValue())) continue; diff --git a/test/testcondition.cpp b/test/testcondition.cpp index a1152b212..7fc125370 100644 --- a/test/testcondition.cpp +++ b/test/testcondition.cpp @@ -5441,6 +5441,12 @@ private: "}\n"); ASSERT_EQUALS("", errout.str()); + check("void f(int& i) {\n" + " if (!i)\n" + " i = 1; \n" + "}\n"); + ASSERT_EQUALS("", errout.str()); + check("struct S {\n" // #9406 " S() : b(false) {}\n" " void f() {\n"