Skip to content
Open
Show file tree
Hide file tree
Changes from 2 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(ternarySameValuePlatformDependent); // #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 ternarySameValuePlatformDependent() // #13773
{
check("typedef float _Complex complex_f32;\n"
"typedef struct {\n"
" uint16_t real;\n"
" uint16_t imag;\n"
"} packed_complex;\n"
"void foo(void) {\n"
" b = a ? sizeof(packed_complex) : sizeof(complex_f32);\n"
"}\n");
ASSERT_EQUALS("", errout_str());
}
};

REGISTER_TEST(TestOther)
Loading