From aae810dd2c09ded8cdf47d52990134309131329e Mon Sep 17 00:00:00 2001 From: chrchr-github <78114321+chrchr-github@users.noreply.github.com> Date: Mon, 4 Apr 2022 06:33:54 +0200 Subject: [PATCH] Fix #10107 FP: duplicateValueTenary (#3969) --- lib/checkother.cpp | 4 +++- test/testother.cpp | 7 +++++++ 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/lib/checkother.cpp b/lib/checkother.cpp index 866191ae1..1e0303b9a 100644 --- a/lib/checkother.cpp +++ b/lib/checkother.cpp @@ -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); diff --git a/test/testother.cpp b/test/testother.cpp index 8e800d79e..0db11da48 100644 --- a/test/testother.cpp +++ b/test/testother.cpp @@ -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() {