Skip to content

Commit 6875514

Browse files
Fix #12725 oppositeInnerCondition with negated bool (#6401)
1 parent 5d17cc2 commit 6875514

2 files changed

Lines changed: 10 additions & 1 deletion

File tree

lib/checkcondition.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -716,7 +716,7 @@ void CheckCondition::multiCondition2()
716716

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

721721
ErrorPath errorPath;
722722

test/testcondition.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2129,6 +2129,15 @@ class TestCondition : public TestFixture {
21292129
" bool m_value = false;\n"
21302130
"};");
21312131
ASSERT_EQUALS("", errout_str());
2132+
2133+
// #12725
2134+
check("bool f(bool b) {\n"
2135+
" if (b)\n"
2136+
" return !b;\n"
2137+
" b = g();\n"
2138+
" return b;\n"
2139+
"}\n");
2140+
ASSERT_EQUALS("[test.cpp:2] -> [test.cpp:3]: (style) Return value '!b' is always false\n", errout_str());
21322141
}
21332142

21342143
void oppositeInnerConditionPointers() {

0 commit comments

Comments
 (0)