Skip to content

Commit

Permalink
Fix #12725 oppositeInnerCondition with negated bool (danmar#6401)
Browse files Browse the repository at this point in the history
  • Loading branch information
chrchr-github committed May 13, 2024
1 parent 5d17cc2 commit 6875514
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/checkcondition.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -716,7 +716,7 @@ void CheckCondition::multiCondition2()

// Condition..
const Token *cond2 = tok->str() == "if" ? condStartToken->astOperand2() : condStartToken->astOperand1();
const bool isReturnVar = (tok->str() == "return" && !Token::Match(cond2, "%cop%"));
const bool isReturnVar = (tok->str() == "return" && (!Token::Match(cond2, "%cop%") || (cond2 && cond2->isUnaryOp("!"))));

ErrorPath errorPath;

Expand Down
9 changes: 9 additions & 0 deletions test/testcondition.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2129,6 +2129,15 @@ class TestCondition : public TestFixture {
" bool m_value = false;\n"
"};");
ASSERT_EQUALS("", errout_str());

// #12725
check("bool f(bool b) {\n"
" if (b)\n"
" return !b;\n"
" b = g();\n"
" return b;\n"
"}\n");
ASSERT_EQUALS("[test.cpp:2] -> [test.cpp:3]: (style) Return value '!b' is always false\n", errout_str());
}

void oppositeInnerConditionPointers() {
Expand Down

0 comments on commit 6875514

Please sign in to comment.