diff --git a/lib/checkother.cpp b/lib/checkother.cpp index 282f3f3e50e..09b94d97baa 100644 --- a/lib/checkother.cpp +++ b/lib/checkother.cpp @@ -2663,6 +2663,14 @@ isStaticAssert(const Settings &settings, const Token *tok) return false; } +static bool isSizeof(const Token *tok) +{ + if (!Token::simpleMatch(tok, "(")) + return false; + + return Token::simpleMatch(tok->astOperand1(), "sizeof"); +} + void CheckOther::checkDuplicateExpression() { { @@ -2846,7 +2854,8 @@ void CheckOther::checkDuplicateExpression() } else if (tok->astOperand1() && tok->astOperand2() && tok->str() == ":" && tok->astParent() && tok->astParent()->str() == "?") { if (!tok->astOperand1()->values().empty() && !tok->astOperand2()->values().empty() && isEqualKnownValue(tok->astOperand1(), tok->astOperand2()) && !isVariableChanged(tok->astParent(), /*indirect*/ 0, *mSettings) && - isConstStatement(tok->astOperand1(), mSettings->library) && isConstStatement(tok->astOperand2(), mSettings->library)) + isConstStatement(tok->astOperand1(), mSettings->library) && isConstStatement(tok->astOperand2(), mSettings->library) && + !isSizeof(tok->astOperand1()) && !isSizeof(tok->astOperand2())) duplicateValueTernaryError(tok); else if (isSameExpression(true, tok->astOperand1(), tok->astOperand2(), *mSettings, false, true, &errorPath)) duplicateExpressionTernaryError(tok, std::move(errorPath)); diff --git a/test/testother.cpp b/test/testother.cpp index 492f111e10e..eb6bb28b060 100644 --- a/test/testother.cpp +++ b/test/testother.cpp @@ -306,6 +306,8 @@ class TestOther : public TestFixture { TEST_CASE(knownConditionFloating); TEST_CASE(knownConditionPrefixed); + + TEST_CASE(ternarySameValueSizeof); // #13773 } #define check(...) check_(__FILE__, __LINE__, __VA_ARGS__) @@ -13093,6 +13095,19 @@ class TestOther : public TestFixture { "[test.cpp:2:13] -> [test.cpp:3:11]: (style) The comparison 'i > +1' is always false. [knownConditionTrueFalse]\n", errout_str()); } + + void ternarySameValueSizeof() // #13773 + { + check("void f(void) {\n" + " b = a ? sizeof(unsigned int) : sizeof(uint32_t);\n" + "}\n"); + ASSERT_EQUALS("", errout_str()); + + check("void f(void) {\n" + " b = a ? sizeof(uint32_t) : sizeof(uint32_t);\n" + "}\n"); + ASSERT_EQUALS("[test.cpp:2:30]: (style) Same expression in both branches of ternary operator. [duplicateExpressionTernary]\n", errout_str()); + } }; REGISTER_TEST(TestOther)