Clarify error message 'condition is always true/false'

This commit is contained in:
Daniel Marjamäki 2017-10-16 22:05:00 +02:00
parent 6d59290858
commit 4700b75ded
2 changed files with 10 additions and 8 deletions

View File

@ -1260,20 +1260,22 @@ void CheckCondition::alwaysTrueFalse()
if (tokens.empty() && hasSizeof)
continue;
alwaysTrueFalseError(tok, tok->values().front().intvalue != 0);
alwaysTrueFalseError(tok, &tok->values().front());
}
}
}
void CheckCondition::alwaysTrueFalseError(const Token *tok, bool knownResult)
void CheckCondition::alwaysTrueFalseError(const Token *tok, const ValueFlow::Value *value)
{
const bool condvalue = value && (value->intvalue != 0);
const std::string expr = tok ? tok->expressionString() : std::string("x");
reportError(tok,
const std::string errmsg = "Condition '" + expr + "' is always " + (condvalue ? "true" : "false");
const ErrorPath errorPath = getErrorPath(tok, value, errmsg);
reportError(errorPath,
Severity::style,
"knownConditionTrueFalse",
"Condition '" + expr + "' is always " + (knownResult ? "true" : "false"),
(knownResult ? CWE571 : CWE570), false);
errmsg,
(condvalue ? CWE571 : CWE570), false);
}
void CheckCondition::checkInvalidTestForOverflow()

View File

@ -138,7 +138,7 @@ private:
void clarifyConditionError(const Token *tok, bool assign, bool boolop);
void alwaysTrueFalseError(const Token *tok, bool knownResult);
void alwaysTrueFalseError(const Token *tok, const ValueFlow::Value *value);
void invalidTestForOverflow(const Token* tok, bool result);
@ -156,7 +156,7 @@ private:
c.redundantConditionError(nullptr, "If x > 11 the condition x > 10 is always true.", false);
c.moduloAlwaysTrueFalseError(nullptr, "1");
c.clarifyConditionError(nullptr, true, false);
c.alwaysTrueFalseError(nullptr, true);
c.alwaysTrueFalseError(nullptr, nullptr);
c.invalidTestForOverflow(nullptr, false);
}