diff --git a/lib/checkcondition.cpp b/lib/checkcondition.cpp index c930fc43e..e48ec70b3 100644 --- a/lib/checkcondition.cpp +++ b/lib/checkcondition.cpp @@ -830,11 +830,23 @@ void CheckCondition::identicalConditionAfterEarlyExitError(const Token *cond1, c { if (diag(cond1) & diag(cond2)) return; - const std::string cond(cond1 ? cond1->expressionString() : "x"); - errorPath.emplace_back(ErrorPathItem(cond1, "first condition")); - errorPath.emplace_back(ErrorPathItem(cond2, "second condition")); - reportError(errorPath, Severity::warning, "identicalConditionAfterEarlyExit", "Identical condition '" + cond + "', second condition is always false", CWE398, false); + const bool isReturnValue = cond2 && Token::simpleMatch(cond2->astParent(), "return"); + + const std::string cond(cond1 ? cond1->expressionString() : "x"); + const std::string value = (cond2 && cond2->valueType() && cond2->valueType()->type == ValueType::Type::BOOL) ? "false" : "0"; + + errorPath.emplace_back(ErrorPathItem(cond1, "If condition '" + cond + "' is true, the function will return/exit")); + errorPath.emplace_back(ErrorPathItem(cond2, (isReturnValue ? "Returning identical expression '" : "Testing identical condition '") + cond + "'")); + + reportError(errorPath, + Severity::warning, + "identicalConditionAfterEarlyExit", + isReturnValue + ? ("Identical condition and return expression '" + cond + "', return value is always " + value) + : ("Identical condition '" + cond + "', second condition is always false"), + CWE398, + false); } //--------------------------------------------------------------------------- diff --git a/test/testcondition.cpp b/test/testcondition.cpp index 73a279dfc..f8c8c34c8 100644 --- a/test/testcondition.cpp +++ b/test/testcondition.cpp @@ -2445,7 +2445,7 @@ private: " if (x > 100) { return false; }\n" " return x > 100;\n" "}"); - ASSERT_EQUALS("[test.cpp:2] -> [test.cpp:3]: (warning) Identical condition 'x>100', second condition is always false\n", errout.str()); + ASSERT_EQUALS("[test.cpp:2] -> [test.cpp:3]: (warning) Identical condition and return expression 'x>100', return value is always false\n", errout.str()); check("void f(int x) {\n" " if (x > 100) { return; }\n"