MCPcopy Create free account
hub / github.com/cppcheck-opensource/cppcheck / identicalConditionAfterEarlyExitError

Method identicalConditionAfterEarlyExitError

lib/checkcondition.cpp:909–930  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

907}
908
909void CheckConditionImpl::identicalConditionAfterEarlyExitError(const Token *cond1, const Token* cond2, ErrorPath errorPath)
910{
911 if (diag(cond1, cond2))
912 return;
913
914 const bool isReturnValue = cond2 && Token::simpleMatch(cond2->astParent(), "return");
915
916 const std::string cond(cond1 ? cond1->expressionString() : "x");
917 const std::string value = (cond2 && cond2->valueType() && cond2->valueType()->type == ValueType::Type::BOOL) ? "false" : "0";
918
919 errorPath.emplace_back(cond1, "If condition '" + cond + "' is true, the function will return/exit");
920 errorPath.emplace_back(cond2, (isReturnValue ? "Returning identical expression '" : "Testing identical condition '") + cond + "'");
921
922 reportError(std::move(errorPath),
923 Severity::warning,
924 "identicalConditionAfterEarlyExit",
925 isReturnValue
926 ? ("Identical condition and return expression '" + cond + "', return value is always " + value)
927 : ("Identical condition '" + cond + "', second condition is always false"),
928 CWE398,
929 Certainty::normal);
930}
931
932//---------------------------------------------------------------------------
933// if ((x != 1) || (x != 3)) // expression always true

Callers 1

getErrorMessagesMethod · 0.80

Calls 5

astParentMethod · 0.80
diagFunction · 0.70
simpleMatchFunction · 0.70
reportErrorFunction · 0.70
expressionStringMethod · 0.45

Tested by

no test coverage detected