Fix #10107 FP: duplicateValueTenary (#3969)

This commit is contained in:
chrchr-github 2022-04-04 06:33:54 +02:00 committed by GitHub
parent 955d6d8fc6
commit aae810dd2c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 10 additions and 1 deletions

View File

@ -2426,7 +2426,9 @@ void CheckOther::checkDuplicateExpression()
}
}
} else if (styleEnabled && tok->astOperand1() && tok->astOperand2() && tok->str() == ":" && tok->astParent() && tok->astParent()->str() == "?") {
if (!isVariableChanged(tok->astParent(), /*indirect*/ 0, mSettings, mTokenizer->isCPP()) && !tok->astOperand1()->values().empty() && !tok->astOperand2()->values().empty() && isEqualKnownValue(tok->astOperand1(), tok->astOperand2()))
if (!tok->astOperand1()->values().empty() && !tok->astOperand2()->values().empty() && isEqualKnownValue(tok->astOperand1(), tok->astOperand2()) &&
!isVariableChanged(tok->astParent(), /*indirect*/ 0, mSettings, mTokenizer->isCPP()) &&
isConstStatement(tok->astOperand1(), mTokenizer->isCPP()) && isConstStatement(tok->astOperand2(), mTokenizer->isCPP()))
duplicateValueTernaryError(tok);
else if (isSameExpression(mTokenizer->isCPP(), true, tok->astOperand1(), tok->astOperand2(), mSettings->library, false, true, &errorPath))
duplicateExpressionTernaryError(tok, errorPath);

View File

@ -5863,6 +5863,13 @@ private:
" ++x;\n"
"}\n");
ASSERT_EQUALS("", errout.str());
check("struct S { int a, b; };\n" // #10107
"S f(bool x, S s) {\n"
" (x) ? f.a = 42 : f.b = 42;\n"
" return f;\n"
"}\n");
ASSERT_EQUALS("", errout.str());
}
void duplicateExpressionTemplate() {