improved error message 'mismatching comparison'

This commit is contained in:
Daniel Marjamäki 2011-11-18 20:07:42 +01:00
parent e78131f9e6
commit 033fef36da
3 changed files with 16 additions and 14 deletions

View File

@ -106,22 +106,18 @@ void CheckAssignIf::comparison()
if ((num1 & num2) != num2) {
const std::string op(compareToken->str());
comparisonError(tok, op=="==" ? false : true);
comparisonError(tok, num1, op, num2, op=="==" ? false : true);
}
}
}
}
void CheckAssignIf::comparisonError(const Token *tok, bool result)
void CheckAssignIf::comparisonError(const Token *tok, int value1, const std::string &op, int value2, bool result)
{
std::string errmsg("Mismatching comparison, the result is always " + std::string(result ? "true" : "false") + "\n"
"Mismatching comparison. This error message is for example given for such a comparison: ");
if (result)
errmsg += "'(x & 6 != 1)'. The result of 'x & 6' can't be 1 so the result of the comparison is always true";
else
errmsg += "'(x & 6 == 1)'. The result of 'x & 6' can't be 1 so the result of the comparison is always false";
std::ostringstream expression;
expression << std::hex << "(X & 0x" << value1 << ") " << op << " 0x" << value2;
const std::string errmsg("Expression '" + expression.str() + "' is always " + (result?"true":"false") + ". Look again at the constants.");
reportError(tok, Severity::style, "comparisonError", errmsg);
}

View File

@ -63,13 +63,19 @@ public:
private:
void assignIfError(const Token *tok, bool result);
void comparisonError(const Token *tok, bool result);
void comparisonError(const Token *tok,
int value1,
const std::string &op,
int value2,
bool result);
void multiConditionError(const Token *tok, unsigned int line1);
void getErrorMessages(ErrorLogger *errorLogger, const Settings *settings) {
CheckAssignIf c(0, settings, errorLogger);
c.assignIfError(0, false);
c.comparisonError(0, false);
c.comparisonError(0, 6, "==", 1, false);
c.multiConditionError(0,1);
}

View File

@ -94,19 +94,19 @@ private:
"{\n"
" if (x & 4 == 3);\n"
"}\n");
ASSERT_EQUALS("[test.cpp:3]: (style) Mismatching comparison, the result is always false\n", errout.str());
ASSERT_EQUALS("[test.cpp:3]: (style) Expression '(X & 0x4) == 0x3' is always false. Look again at the constants.\n", errout.str());
check("void foo(int x)\n"
"{\n"
" if ((x & 4) == 3);\n"
"}\n");
ASSERT_EQUALS("[test.cpp:3]: (style) Mismatching comparison, the result is always false\n", errout.str());
ASSERT_EQUALS("[test.cpp:3]: (style) Expression '(X & 0x4) == 0x3' is always false. Look again at the constants.\n", errout.str());
check("void foo(int x)\n"
"{\n"
" if (x & 4 != 3);\n"
"}\n");
ASSERT_EQUALS("[test.cpp:3]: (style) Mismatching comparison, the result is always true\n", errout.str());
ASSERT_EQUALS("[test.cpp:3]: (style) Expression '(X & 0x4) != 0x3' is always true. Look again at the constants.\n", errout.str());
}
void multicompare() {