ASSERT_EQUALS("[test.cpp:3]: (style) Comparison of two functions returning boolean value using relational (<, >, <= or >=) operator.\n",errout.str());
}
voidcheckComparisonOfFuncReturningBool2(){
check("void f(){\n"
" int temp = 4;\n"
" bool a = true;\n"
" if(compare(temp) > a){\n"
" printf(\"foo\");\n"
" }\n"
"}\n"
"bool compare(int temp){\n"
" if(temp==4){\n"
" return true;\n"
" }\n"
" else\n"
" return false;\n"
"}");
ASSERT_EQUALS("[test.cpp:4]: (style) Comparison of a function returning boolean value using relational (<, >, <= or >=) operator.\n",errout.str());
}
voidcheckComparisonOfFuncReturningBool3(){
check("void f(){\n"
" int temp = 4;\n"
" if(compare(temp) > temp){\n"
" printf(\"foo\");\n"
" }\n"
"}\n"
"bool compare(int temp){\n"
" if(temp==4){\n"
" return true;\n"
" }\n"
" else\n"
" return false;\n"
"}");
ASSERT_EQUALS("[test.cpp:3]: (style) Comparison of a function returning boolean value using relational (<, >, <= or >=) operator.\n",errout.str());
}
voidcheckComparisonOfFuncReturningBool4(){
check("void f(){\n"
" int temp = 4;\n"
" bool b = compare2(6);\n"
" if(compare1(temp)> b){\n"
" printf(\"foo\");\n"
" }\n"
"}\n"
"bool compare1(int temp){\n"
" if(temp==4){\n"
" return true;\n"
" }\n"
" else\n"
" return false;\n"
"}\n"
"bool compare2(int temp){\n"
" if(temp == 5){\n"
" return true;\n"
" }\n"
" else\n"
" return false;\n"
"}");
ASSERT_EQUALS("[test.cpp:4]: (style) Comparison of a function returning boolean value using relational (<, >, <= or >=) operator.\n",errout.str());
}
voidcheckComparisonOfFuncReturningBool5(){
check("void f(){\n"
" int temp = 4;\n"
" if(compare1(temp) > !compare2(temp)){\n"
" printf(\"foo\");\n"
" }\n"
"}\n"
"bool compare1(int temp){\n"
" if(temp==4){\n"
" return true;\n"
" }\n"
" else\n"
" return false;\n"
"}\n"
"bool compare2(int temp){\n"
" if(temp==4){\n"
" return false;\n"
" }\n"
" else\n"
" return true;\n"
"}");
ASSERT_EQUALS("[test.cpp:3]: (style) Comparison of two functions returning boolean value using relational (<, >, <= or >=) operator.\n",errout.str());
}
voidcheckComparisonOfFuncReturningBool6(){
check("int compare1(int temp);\n"
"namespace Foo {\n"
" bool compare1(int temp);\n"
"}\n"
"void f(){\n"
" int temp = 4;\n"
" if(compare1(temp) > compare2(temp)){\n"
" printf(\"foo\");\n"
" }\n"
"}");
ASSERT_EQUALS("",errout.str());
check("namespace Foo {\n"
" bool compare1(int temp);\n"
"}\n"
"int compare1(int temp);\n"
"void f(){\n"
" int temp = 4;\n"
" if(compare1(temp) > compare2(temp)){\n"
" printf(\"foo\");\n"
" }\n"
"}");
ASSERT_EQUALS("",errout.str());
check("int compare1(int temp);\n"
"namespace Foo {\n"
" bool compare1(int temp);\n"
" void f(){\n"
" int temp = 4;\n"
" if(compare1(temp) > compare2(temp)){\n"
" printf(\"foo\");\n"
" }\n"
" }\n"
"}");
ASSERT_EQUALS("[test.cpp:6]: (style) Comparison of a function returning boolean value using relational (<, >, <= or >=) operator.\n",errout.str());
check("int compare1(int temp);\n"
"namespace Foo {\n"
" bool compare1(int temp);\n"
" void f(){\n"
" int temp = 4;\n"
" if(::compare1(temp) > compare2(temp)){\n"
" printf(\"foo\");\n"
" }\n"
" }\n"
"}");
ASSERT_EQUALS("",errout.str());
check("bool compare1(int temp);\n"
"void f(){\n"
" int temp = 4;\n"
" if(foo.compare1(temp) > compare2(temp)){\n"
" printf(\"foo\");\n"
" }\n"
"}");
ASSERT_EQUALS("",errout.str());
}
voidcheckComparisonOfBoolWithBool(){
constcharcode[]="void f(){\n"
" int temp = 4;\n"
" bool b = compare2(6);\n"
" bool a = compare1(4);\n"
" if(b > a){\n"
" printf(\"foo\");\n"
" }\n"
"}\n"
"bool compare1(int temp){\n"
" if(temp==4){\n"
" return true;\n"
" }\n"
" else\n"
" return false;\n"
"}\n"
"bool compare2(int temp){\n"
"if(temp==5){\n"
" return true;\n"
" }\n"
" else\n"
" return false;\n"
"}\n";
check(code,true);
ASSERT_EQUALS("[test.cpp:5]: (style) Comparison of a variable having boolean value using relational (<, >, <= or >=) operator.\n",errout.str());
check(code,false);
ASSERT_EQUALS("",errout.str());
}
voidbitwiseOnBoolean(){// 3062
check("void f(_Bool a, _Bool b) {\n"
" if(a & b) {}\n"
"}");
ASSERT_EQUALS("[test.cpp:2]: (style, inconclusive) Boolean variable 'a' is used in bitwise operation. Did you mean '&&'?\n",errout.str());
check("void f(_Bool a, _Bool b) {\n"
" if(a | b) {}\n"
"}");
ASSERT_EQUALS("[test.cpp:2]: (style, inconclusive) Boolean variable 'a' is used in bitwise operation. Did you mean '||'?\n",errout.str());
check("void f(bool a, bool b) {\n"
" if(a & !b) {}\n"
"}");
ASSERT_EQUALS("[test.cpp:2]: (style, inconclusive) Boolean variable 'a' is used in bitwise operation. Did you mean '&&'?\n",errout.str());
check("void f(bool a, bool b) {\n"
" if(a | !b) {}\n"
"}");
ASSERT_EQUALS("[test.cpp:2]: (style, inconclusive) Boolean variable 'a' is used in bitwise operation. Did you mean '||'?\n",errout.str());
check("bool a, b;\n"
"void f() {\n"
" if(a & b) {}\n"
"}");
ASSERT_EQUALS("[test.cpp:3]: (style, inconclusive) Boolean variable 'a' is used in bitwise operation. Did you mean '&&'?\n",errout.str());
check("bool a, b;\n"
"void f() {\n"
" if(a & !b) {}\n"
"}");
ASSERT_EQUALS("[test.cpp:3]: (style, inconclusive) Boolean variable 'a' is used in bitwise operation. Did you mean '&&'?\n",errout.str());
check("bool a, b;\n"
"void f() {\n"
" if(a | b) {}\n"
"}");
ASSERT_EQUALS("[test.cpp:3]: (style, inconclusive) Boolean variable 'a' is used in bitwise operation. Did you mean '||'?\n",errout.str());
check("bool a, b;\n"
"void f() {\n"
" if(a | !b) {}\n"
"}");
ASSERT_EQUALS("[test.cpp:3]: (style, inconclusive) Boolean variable 'a' is used in bitwise operation. Did you mean '||'?\n",errout.str());
check("void f(bool a, int b) {\n"
" if(a & b) {}\n"
"}");
ASSERT_EQUALS("[test.cpp:2]: (style, inconclusive) Boolean variable 'a' is used in bitwise operation. Did you mean '&&'?\n",errout.str());
check("void f(int a, bool b) {\n"
" if(a & b) {}\n"
"}");
ASSERT_EQUALS("[test.cpp:2]: (style, inconclusive) Boolean variable 'b' is used in bitwise operation. Did you mean '&&'?\n",errout.str());
check("void f(int a, int b) {\n"
" if(a & b) {}\n"
"}");
ASSERT_EQUALS("",errout.str());
check("void f(bool b) {\n"
" foo(bar, &b);\n"
"}");
ASSERT_EQUALS("",errout.str());
}
voidincrementBoolean(){
check("bool bValue = true;\n"
"void f() { bValue++; }");
ASSERT_EQUALS("[test.cpp:2]: (style) Incrementing a variable of type 'bool' with postfix operator++ is deprecated by the C++ Standard. You should assign it the value 'true' instead.\n",errout.str());
check("void f(bool test){\n"
" test++;\n"
"}");
ASSERT_EQUALS("[test.cpp:2]: (style) Incrementing a variable of type 'bool' with postfix operator++ is deprecated by the C++ Standard. You should assign it the value 'true' instead.\n",errout.str());
check("void f(int test){\n"
" test++;\n"
"}");
ASSERT_EQUALS("",errout.str());
}
voidcomparisonOfBoolWithInt1(){
check("void f(bool x) {\n"
" if (x < 10) {\n"
" printf(\"foo\");\n"
" }\n"
"}");
ASSERT_EQUALS("[test.cpp:2]: (warning) Comparison of a boolean with an integer.\n",errout.str());
check("void f(bool x) {\n"
" if (10 >= x) {\n"
" printf(\"foo\");\n"
" }\n"
"}");
ASSERT_EQUALS("[test.cpp:2]: (warning) Comparison of a boolean with an integer.\n",errout.str());
check("void f(bool x) {\n"
" if (x != 0) {\n"
" printf(\"foo\");\n"
" }\n"
"}");
ASSERT_EQUALS("",errout.str());
check("void f(bool x) {\n"// #3356
" if (x == 1) {\n"
" }\n"
"}");
ASSERT_EQUALS("",errout.str());
check("void f(bool x) {\n"
" if (x != 10) {\n"
" printf(\"foo\");\n"
" }\n"
"}");
ASSERT_EQUALS("[test.cpp:2]: (warning) Comparison of a boolean with an integer that is neither 1 nor 0.\n",errout.str());
check("void f(bool x) {\n"
" if (x == 10) {\n"
" printf(\"foo\");\n"
" }\n"
"}");
ASSERT_EQUALS("[test.cpp:2]: (warning) Comparison of a boolean with an integer that is neither 1 nor 0.\n",errout.str());
ASSERT_EQUALS("[test.cpp:2]: (error) Converting pointer arithmetic result to bool. The bool is always true unless there is undefined behaviour.\n",errout.str());
ASSERT_EQUALS("[test.cpp:2]: (error) Converting pointer arithmetic result to bool. The bool is always true unless there is undefined behaviour.\n",errout.str());