From 4700b75ded9ef2918e86afc4806a846acd415552 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Mon, 16 Oct 2017 22:05:00 +0200 Subject: [PATCH] Clarify error message 'condition is always true/false' --- lib/checkcondition.cpp | 14 ++++++++------ lib/checkcondition.h | 4 ++-- 2 files changed, 10 insertions(+), 8 deletions(-) diff --git a/lib/checkcondition.cpp b/lib/checkcondition.cpp index f1e1f2fad..fb32e5240 100644 --- a/lib/checkcondition.cpp +++ b/lib/checkcondition.cpp @@ -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() diff --git a/lib/checkcondition.h b/lib/checkcondition.h index 238f84cde..99e7f88e1 100644 --- a/lib/checkcondition.h +++ b/lib/checkcondition.h @@ -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); }