Fixed #9285 (Misleading warning message)

This commit is contained in:
Daniel Marjamäki 2019-11-16 17:24:54 +01:00
parent 53cc922765
commit 7cd7aff60a
2 changed files with 17 additions and 5 deletions

View File

@ -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);
}
//---------------------------------------------------------------------------

View File

@ -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"