minor tweaks of knownConditionTrueFalse

This commit is contained in:
Daniel Marjamäki 2020-02-15 20:21:13 +01:00
parent 67b495fc50
commit 320cb9008f
2 changed files with 24 additions and 24 deletions

View File

@ -2100,10 +2100,10 @@ void CheckOther::duplicateExpressionError(const Token *tok1, const Token *tok2,
const std::string& op = opTok ? opTok->str() : "&&"; const std::string& op = opTok ? opTok->str() : "&&";
std::string msg = "Same expression on both sides of \'" + op + "\'"; std::string msg = "Same expression on both sides of \'" + op + "\'";
std::string id = "duplicateExpression"; const char *id = "duplicateExpression";
if (expr1 != expr2) { if (expr1 != expr2 && (!opTok || !opTok->isArithmeticalOp())) {
id = "knownConditionTrueFalse"; 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, "==|>=|<=")) if (Token::Match(opTok, "==|>=|<="))
msg = exprMsg + "true"; msg = exprMsg + "true";
else if (Token::Match(opTok, "!=|>|<")) 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"; 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 " "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 " "indicate a cut and paste or logic error. Please examine this code carefully to "
"determine if it is correct.", CWE398, false); "determine if it is correct.", CWE398, false);

View File

@ -4290,7 +4290,7 @@ private:
check("void foo(double a, double b) {\n" check("void foo(double a, double b) {\n"
" if ((b + a) > (a + 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" check("void f(int x) {\n"
" if ((x == 1) && (x == 0x00000001))\n" " if ((x == 1) && (x == 0x00000001))\n"
@ -4529,13 +4529,13 @@ private:
" const int i = sizeof(int);\n" " const int i = sizeof(int);\n"
" if ( i != sizeof (int)){}\n" " if ( i != sizeof (int)){}\n"
"}\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" check("void f() {\n"
" const int i = sizeof(int);\n" " const int i = sizeof(int);\n"
" if ( sizeof (int) != i){}\n" " if ( sizeof (int) != i){}\n"
"}\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"); check("void f(int a = 1) { if ( a != 1){}}\n");
ASSERT_EQUALS("", errout.str()); ASSERT_EQUALS("", errout.str());
@ -4544,21 +4544,21 @@ private:
" int a = 1;\n" " int a = 1;\n"
" if ( a != 1){} \n" " if ( a != 1){} \n"
"}\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" check("void f() {\n"
" int a = 1;\n" " int a = 1;\n"
" int b = 1;\n" " int b = 1;\n"
" if ( a != b){} \n" " if ( a != b){} \n"
"}\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" check("void f() {\n"
" int a = 1;\n" " int a = 1;\n"
" int b = a;\n" " int b = a;\n"
" if ( a != b){} \n" " if ( a != b){} \n"
"}\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" check("void use(int);\n"
"void f() {\n" "void f() {\n"
@ -4567,7 +4567,7 @@ private:
" use(b);\n" " use(b);\n"
" if ( a != 1){} \n" " if ( a != 1){} \n"
"}\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" check("void use(int);\n"
"void f() {\n" "void f() {\n"
@ -4591,7 +4591,7 @@ private:
" void f() {\n" " void f() {\n"
" if ( a != 1){} \n" " if ( a != 1){} \n"
"}\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" check("int a = 1;\n"
" void f() {\n" " void f() {\n"
@ -4603,7 +4603,7 @@ private:
" static const int a = 1;\n" " static const int a = 1;\n"
" if ( a != 1){} \n" " if ( a != 1){} \n"
"}\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" check("void f() {\n"
" static int a = 1;\n" " static int a = 1;\n"
@ -4616,7 +4616,7 @@ private:
" if ( a != 1){\n" " if ( a != 1){\n"
" a++;\n" " a++;\n"
" }}\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" check("void f(int b) {\n"
" int a = 1;\n" " int a = 1;\n"
@ -4655,7 +4655,7 @@ private:
" break;\n" " break;\n"
" }\n" " }\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() { void duplicateExpression8() {
@ -4759,7 +4759,7 @@ private:
" int a = 1;\n" " int a = 1;\n"
" while ( a != 1){}\n" " while ( a != 1){}\n"
"}\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"); check("void f() { int a = 1; while ( a != 1){ a++; }}\n");
ASSERT_EQUALS("", errout.str()); ASSERT_EQUALS("", errout.str());
@ -4775,7 +4775,7 @@ private:
" if( i != 0 ) {}\n" " if( i != 0 ) {}\n"
" }\n" " }\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" check("void f() {\n"
" for(int i = 0; i < 10;) {\n" " for(int i = 0; i < 10;) {\n"
@ -4816,7 +4816,7 @@ private:
" b++;\n" " b++;\n"
" }\n" " }\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 void duplicateExpressionTernary() { // #6391
@ -5331,8 +5331,8 @@ private:
" if (val < 0) continue;\n" " if (val < 0) continue;\n"
" if ((val > 0)) {}\n" " if ((val > 0)) {}\n"
"}\n"); "}\n");
ASSERT_EQUALS("[test.cpp:2] -> [test.cpp:3]: (style) The expression 'val < 0' is always false.\n" 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 expression 'val > 0' is always false.\n", errout.str()); "[test.cpp:2] -> [test.cpp:4]: (style) The comparison 'val > 0' is always false.\n", errout.str());
check("void f() {\n" check("void f() {\n"
" int val = 0;\n" " int val = 0;\n"
@ -5340,8 +5340,8 @@ private:
" if ((val > 0)) {}\n" " if ((val > 0)) {}\n"
" }\n" " }\n"
"}\n"); "}\n");
ASSERT_EQUALS("[test.cpp:2] -> [test.cpp:3]: (style) The expression 'val < 0' is always false.\n" 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 expression 'val > 0' is always false.\n", errout.str()); "[test.cpp:2] -> [test.cpp:4]: (style) The comparison 'val > 0' is always false.\n", errout.str());
check("void f() {\n" check("void f() {\n"
" int val = 0;\n" " int val = 0;\n"
@ -5349,8 +5349,8 @@ private:
" if ((val < 0)) {}\n" " if ((val < 0)) {}\n"
" }\n" " }\n"
"}\n"); "}\n");
ASSERT_EQUALS("[test.cpp:2] -> [test.cpp:3]: (style) The expression 'val < 0' is always false.\n" 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 expression 'val < 0' is always false.\n", errout.str()); "[test.cpp:2] -> [test.cpp:4]: (style) The comparison 'val < 0' is always false.\n", errout.str());
check("void f() {\n" check("void f() {\n"
" int activate = 0;\n" " int activate = 0;\n"