From 880e0e3b5f6775c96e5cc0f881bc077bd8b2133d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Mon, 1 Aug 2011 07:13:47 +0200 Subject: [PATCH] AssignIf: Improved the error message for mismatching comparison --- lib/checkassignif.cpp | 13 ++++++++++--- test/testassignif.cpp | 6 +++--- 2 files changed, 13 insertions(+), 6 deletions(-) diff --git a/lib/checkassignif.cpp b/lib/checkassignif.cpp index 5178a3f20..e48b4fc7a 100644 --- a/lib/checkassignif.cpp +++ b/lib/checkassignif.cpp @@ -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); } diff --git a/test/testassignif.cpp b/test/testassignif.cpp index 7ea5d12d0..cfbfab7b1 100644 --- a/test/testassignif.cpp +++ b/test/testassignif.cpp @@ -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()); } };