Skip to content

fix #13773: False positive: should not warn when different ternary 2nd and 3rd expressions happens to have same value #7523

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
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"
" 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