Fix duplicateConditionalAssign FP (#4484)

* Fix #9406 FN redundant assignment of Boolean variable

* Fix warning, refactor

* Update testcondition.cpp

* Fix duplicateConditionalAssign FP

* Format
This commit is contained in:
chrchr-github 2022-09-20 21:57:27 +02:00 committed by GitHub
parent 48999cf1d1
commit 3644d79162
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 12 additions and 3 deletions

View File

@ -1764,10 +1764,13 @@ void CheckCondition::checkDuplicateConditionalAssign()
continue; continue;
bool isRedundant = false; bool isRedundant = false;
if (isBoolVar) { if (isBoolVar) {
if (!astIsBool(condTok))
continue;
const bool isNegation = condTok->str() == "!"; 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; continue;
if (!(assignTok->astOperand2() && assignTok->astOperand2()->hasKnownIntValue())) if (!(assignTok->astOperand2() && assignTok->astOperand2()->hasKnownIntValue()))
continue; continue;

View File

@ -5441,6 +5441,12 @@ private:
"}\n"); "}\n");
ASSERT_EQUALS("", errout.str()); 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 check("struct S {\n" // #9406
" S() : b(false) {}\n" " S() : b(false) {}\n"
" void f() {\n" " void f() {\n"