Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion lib/checkother.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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()
{
{
Expand Down Expand Up @@ -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));
Expand Down
15 changes: 15 additions & 0 deletions test/testother.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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__)
Expand Down Expand Up @@ -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"
Comment thread
ludviggunne marked this conversation as resolved.
" 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)
Loading