AssignIf: Improved the error message for mismatching comparison

This commit is contained in:
Daniel Marjamäki 2011-08-01 07:13:47 +02:00
parent bc4db75aa9
commit 880e0e3b5f
2 changed files with 13 additions and 6 deletions

View File

@ -121,7 +121,14 @@ void CheckAssignIf::comparison()
void CheckAssignIf::comparisonError(const Token *tok, bool result)
{
reportError(tok, Severity::style,
"comparisonError",
"Comparison is always " + std::string(result ? "true" : "false"));
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";
reportError(tok, Severity::style, "comparisonError", errmsg);
}

View File

@ -83,19 +83,19 @@ private:
"{\n"
" if (x & 4 == 3);\n"
"}\n");
ASSERT_EQUALS("[test.cpp:3]: (style) Comparison is always false\n", errout.str());
ASSERT_EQUALS("[test.cpp:3]: (style) Mismatching comparison, the result is always false\n", errout.str());
check("void foo(int x)\n"
"{\n"
" if ((x & 4) == 3);\n"
"}\n");
ASSERT_EQUALS("[test.cpp:3]: (style) Comparison is always false\n", errout.str());
ASSERT_EQUALS("[test.cpp:3]: (style) Mismatching comparison, the result is always false\n", errout.str());
check("void foo(int x)\n"
"{\n"
" if (x & 4 != 3);\n"
"}\n");
ASSERT_EQUALS("[test.cpp:3]: (style) Comparison is always true\n", errout.str());
ASSERT_EQUALS("[test.cpp:3]: (style) Mismatching comparison, the result is always true\n", errout.str());
}
};