From 320cb9008f065b4c9b611d0f51d4b1383dab6816 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Sat, 15 Feb 2020 20:21:13 +0100 Subject: [PATCH] minor tweaks of knownConditionTrueFalse --- lib/checkother.cpp | 8 ++++---- test/testother.cpp | 40 ++++++++++++++++++++-------------------- 2 files changed, 24 insertions(+), 24 deletions(-) diff --git a/lib/checkother.cpp b/lib/checkother.cpp index 8d3efe6e7..7a6b5d343 100644 --- a/lib/checkother.cpp +++ b/lib/checkother.cpp @@ -2100,10 +2100,10 @@ void CheckOther::duplicateExpressionError(const Token *tok1, const Token *tok2, const std::string& op = opTok ? opTok->str() : "&&"; std::string msg = "Same expression on both sides of \'" + op + "\'"; - std::string id = "duplicateExpression"; - if (expr1 != expr2) { + const char *id = "duplicateExpression"; + if (expr1 != expr2 && (!opTok || !opTok->isArithmeticalOp())) { id = "knownConditionTrueFalse"; - std::string exprMsg = "The expression \'" + expr1 + " " + op + " " + expr2 + "\' is always "; + std::string exprMsg = "The comparison \'" + expr1 + " " + op + " " + expr2 + "\' is always "; if (Token::Match(opTok, "==|>=|<=")) msg = exprMsg + "true"; else if (Token::Match(opTok, "!=|>|<")) @@ -2112,7 +2112,7 @@ void CheckOther::duplicateExpressionError(const Token *tok1, const Token *tok2, msg += " because '" + expr1 + "' and '" + expr2 + "' represent the same value"; } - reportError(errors, Severity::style, id.c_str(), msg + ".\n" + reportError(errors, Severity::style, id, msg + ".\n" "Finding the same expression on both sides of an operator is suspicious and might " "indicate a cut and paste or logic error. Please examine this code carefully to " "determine if it is correct.", CWE398, false); diff --git a/test/testother.cpp b/test/testother.cpp index 806f62c88..7869074f9 100644 --- a/test/testother.cpp +++ b/test/testother.cpp @@ -4290,7 +4290,7 @@ private: check("void foo(double a, double b) {\n" " if ((b + a) > (a + b)) {}\n" "}"); - ASSERT_EQUALS("[test.cpp:2]: (style) The expression 'b+a > a+b' is always false because 'b+a' and 'a+b' represent the same value.\n", errout.str()); + ASSERT_EQUALS("[test.cpp:2]: (style) The comparison 'b+a > a+b' is always false because 'b+a' and 'a+b' represent the same value.\n", errout.str()); check("void f(int x) {\n" " if ((x == 1) && (x == 0x00000001))\n" @@ -4529,13 +4529,13 @@ private: " const int i = sizeof(int);\n" " if ( i != sizeof (int)){}\n" "}\n"); - ASSERT_EQUALS("[test.cpp:2] -> [test.cpp:3]: (style) The expression 'i != sizeof(int)' is always false because 'i' and 'sizeof(int)' represent the same value.\n", errout.str()); + ASSERT_EQUALS("[test.cpp:2] -> [test.cpp:3]: (style) The comparison 'i != sizeof(int)' is always false because 'i' and 'sizeof(int)' represent the same value.\n", errout.str()); check("void f() {\n" " const int i = sizeof(int);\n" " if ( sizeof (int) != i){}\n" "}\n"); - ASSERT_EQUALS("[test.cpp:2] -> [test.cpp:3]: (style) The expression 'sizeof(int) != i' is always false because 'sizeof(int)' and 'i' represent the same value.\n", errout.str()); + ASSERT_EQUALS("[test.cpp:2] -> [test.cpp:3]: (style) The comparison 'sizeof(int) != i' is always false because 'sizeof(int)' and 'i' represent the same value.\n", errout.str()); check("void f(int a = 1) { if ( a != 1){}}\n"); ASSERT_EQUALS("", errout.str()); @@ -4544,21 +4544,21 @@ private: " int a = 1;\n" " if ( a != 1){} \n" "}\n"); - ASSERT_EQUALS("[test.cpp:2] -> [test.cpp:3]: (style) The expression 'a != 1' is always false.\n", errout.str()); + ASSERT_EQUALS("[test.cpp:2] -> [test.cpp:3]: (style) The comparison 'a != 1' is always false.\n", errout.str()); check("void f() {\n" " int a = 1;\n" " int b = 1;\n" " if ( a != b){} \n" "}\n"); - ASSERT_EQUALS("[test.cpp:2] -> [test.cpp:3] -> [test.cpp:4]: (style) The expression 'a != b' is always false because 'a' and 'b' represent the same value.\n", errout.str()); + ASSERT_EQUALS("[test.cpp:2] -> [test.cpp:3] -> [test.cpp:4]: (style) The comparison 'a != b' is always false because 'a' and 'b' represent the same value.\n", errout.str()); check("void f() {\n" " int a = 1;\n" " int b = a;\n" " if ( a != b){} \n" "}\n"); - ASSERT_EQUALS("[test.cpp:3] -> [test.cpp:4]: (style) The expression 'a != b' is always false because 'a' and 'b' represent the same value.\n", errout.str()); + ASSERT_EQUALS("[test.cpp:3] -> [test.cpp:4]: (style) The comparison 'a != b' is always false because 'a' and 'b' represent the same value.\n", errout.str()); check("void use(int);\n" "void f() {\n" @@ -4567,7 +4567,7 @@ private: " use(b);\n" " if ( a != 1){} \n" "}\n"); - ASSERT_EQUALS("[test.cpp:3] -> [test.cpp:6]: (style) The expression 'a != 1' is always false.\n", errout.str()); + ASSERT_EQUALS("[test.cpp:3] -> [test.cpp:6]: (style) The comparison 'a != 1' is always false.\n", errout.str()); check("void use(int);\n" "void f() {\n" @@ -4591,7 +4591,7 @@ private: " void f() {\n" " if ( a != 1){} \n" "}\n"); - ASSERT_EQUALS("[test.cpp:1] -> [test.cpp:3]: (style) The expression 'a != 1' is always false.\n", errout.str()); + ASSERT_EQUALS("[test.cpp:1] -> [test.cpp:3]: (style) The comparison 'a != 1' is always false.\n", errout.str()); check("int a = 1;\n" " void f() {\n" @@ -4603,7 +4603,7 @@ private: " static const int a = 1;\n" " if ( a != 1){} \n" "}\n"); - ASSERT_EQUALS("[test.cpp:2] -> [test.cpp:3]: (style) The expression 'a != 1' is always false.\n", errout.str()); + ASSERT_EQUALS("[test.cpp:2] -> [test.cpp:3]: (style) The comparison 'a != 1' is always false.\n", errout.str()); check("void f() {\n" " static int a = 1;\n" @@ -4616,7 +4616,7 @@ private: " if ( a != 1){\n" " a++;\n" " }}\n"); - ASSERT_EQUALS("[test.cpp:2] -> [test.cpp:3]: (style) The expression 'a != 1' is always false.\n", errout.str()); + ASSERT_EQUALS("[test.cpp:2] -> [test.cpp:3]: (style) The comparison 'a != 1' is always false.\n", errout.str()); check("void f(int b) {\n" " int a = 1;\n" @@ -4655,7 +4655,7 @@ private: " break;\n" " }\n" "}\n"); - ASSERT_EQUALS("[test.cpp:2] -> [test.cpp:5]: (style) The expression 'Diag == 0' is always true.\n", errout.str()); + ASSERT_EQUALS("[test.cpp:2] -> [test.cpp:5]: (style) The comparison 'Diag == 0' is always true.\n", errout.str()); } void duplicateExpression8() { @@ -4759,7 +4759,7 @@ private: " int a = 1;\n" " while ( a != 1){}\n" "}\n"); - ASSERT_EQUALS("[test.cpp:2] -> [test.cpp:3]: (style) The expression 'a != 1' is always false.\n", errout.str()); + ASSERT_EQUALS("[test.cpp:2] -> [test.cpp:3]: (style) The comparison 'a != 1' is always false.\n", errout.str()); check("void f() { int a = 1; while ( a != 1){ a++; }}\n"); ASSERT_EQUALS("", errout.str()); @@ -4775,7 +4775,7 @@ private: " if( i != 0 ) {}\n" " }\n" "}\n"); - ASSERT_EQUALS("[test.cpp:2] -> [test.cpp:3]: (style) The expression 'i != 0' is always false.\n", errout.str()); + ASSERT_EQUALS("[test.cpp:2] -> [test.cpp:3]: (style) The comparison 'i != 0' is always false.\n", errout.str()); check("void f() {\n" " for(int i = 0; i < 10;) {\n" @@ -4816,7 +4816,7 @@ private: " b++;\n" " }\n" "}\n"); - ASSERT_EQUALS("[test.cpp:3] -> [test.cpp:4]: (style) The expression 'a != 1' is always false.\n", errout.str()); + ASSERT_EQUALS("[test.cpp:3] -> [test.cpp:4]: (style) The comparison 'a != 1' is always false.\n", errout.str()); } void duplicateExpressionTernary() { // #6391 @@ -5331,8 +5331,8 @@ private: " if (val < 0) continue;\n" " if ((val > 0)) {}\n" "}\n"); - ASSERT_EQUALS("[test.cpp:2] -> [test.cpp:3]: (style) The expression 'val < 0' is always false.\n" - "[test.cpp:2] -> [test.cpp:4]: (style) The expression 'val > 0' is always false.\n", errout.str()); + ASSERT_EQUALS("[test.cpp:2] -> [test.cpp:3]: (style) The comparison 'val < 0' is always false.\n" + "[test.cpp:2] -> [test.cpp:4]: (style) The comparison 'val > 0' is always false.\n", errout.str()); check("void f() {\n" " int val = 0;\n" @@ -5340,8 +5340,8 @@ private: " if ((val > 0)) {}\n" " }\n" "}\n"); - ASSERT_EQUALS("[test.cpp:2] -> [test.cpp:3]: (style) The expression 'val < 0' is always false.\n" - "[test.cpp:2] -> [test.cpp:4]: (style) The expression 'val > 0' is always false.\n", errout.str()); + ASSERT_EQUALS("[test.cpp:2] -> [test.cpp:3]: (style) The comparison 'val < 0' is always false.\n" + "[test.cpp:2] -> [test.cpp:4]: (style) The comparison 'val > 0' is always false.\n", errout.str()); check("void f() {\n" " int val = 0;\n" @@ -5349,8 +5349,8 @@ private: " if ((val < 0)) {}\n" " }\n" "}\n"); - ASSERT_EQUALS("[test.cpp:2] -> [test.cpp:3]: (style) The expression 'val < 0' is always false.\n" - "[test.cpp:2] -> [test.cpp:4]: (style) The expression 'val < 0' is always false.\n", errout.str()); + ASSERT_EQUALS("[test.cpp:2] -> [test.cpp:3]: (style) The comparison 'val < 0' is always false.\n" + "[test.cpp:2] -> [test.cpp:4]: (style) The comparison 'val < 0' is always false.\n", errout.str()); check("void f() {\n" " int activate = 0;\n"