From 6875514e594dcfb009119da4fa7e661430f9f63e Mon Sep 17 00:00:00 2001 From: chrchr-github <78114321+chrchr-github@users.noreply.github.com> Date: Mon, 13 May 2024 18:06:47 +0200 Subject: [PATCH] Fix #12725 oppositeInnerCondition with negated bool (#6401) --- lib/checkcondition.cpp | 2 +- test/testcondition.cpp | 9 +++++++++ 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/lib/checkcondition.cpp b/lib/checkcondition.cpp index 70fa4d9ae33..bc1df36aacc 100644 --- a/lib/checkcondition.cpp +++ b/lib/checkcondition.cpp @@ -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; diff --git a/test/testcondition.cpp b/test/testcondition.cpp index db044308c71..1a535ff0bf0 100644 --- a/test/testcondition.cpp +++ b/test/testcondition.cpp @@ -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() {