" char *a = first, *b = first + 1, *c = first + 2;\n"
" char* m1 = compare(a, b) < 0\n"
" ? (compare(b, c) < 0 ? b : (compare(a, c) < 0 ? c : a))\n"
" : (compare(a, c) < 0 ? a : (compare(b, c) < 0 ? c : b));\n"
"}", dinit(CheckOptions, $.cpp = false));
ASSERT_EQUALS("", errout_str());
// #7381
check("void foo(bool *p, bool b) {\n"
" p = b;\n"
" p = &b;\n"
"}");
ASSERT_EQUALS("[test.cpp:2:7]: (error) Boolean value assigned to pointer. [assignBoolToPointer]\n", errout_str());
}
voidassignBoolToFloat() {
check("void foo1() {\n"
" double d = false;\n"
"}");
ASSERT_EQUALS("[test.cpp:2:14]: (style) Boolean value assigned to floating point variable. [assignBoolToFloat]\n", errout_str());
check("void foo2() {\n"
" float d = true;\n"
"}");
ASSERT_EQUALS("[test.cpp:2:13]: (style) Boolean value assigned to floating point variable. [assignBoolToFloat]\n", errout_str());
check("void foo3() {\n"
" long double d = (2>1);\n"
"}");
ASSERT_EQUALS("[test.cpp:2:19]: (style) Boolean value assigned to floating point variable. [assignBoolToFloat]\n", errout_str());
// stability - don't crash:
check("void foo4() {\n"
" unknown = false;\n"
"}");
ASSERT_EQUALS("", errout_str());
check("struct S {\n"
" float p;\n"
"};\n"
"void f() {\n"
" S s = {0};\n"
" s.p = true;\n"
"}");
ASSERT_EQUALS("[test.cpp:6:9]: (style) Boolean value assigned to floating point variable. [assignBoolToFloat]\n", errout_str());
check("struct S {\n"
" float* p[1];\n"
"};\n"
"void f() {\n"
" S s = {0};\n"
" *s.p[0] = true;\n"
"}");
ASSERT_EQUALS("[test.cpp:6:13]: (style) Boolean value assigned to floating point variable. [assignBoolToFloat]\n", errout_str());
}
voidcomparisonOfBoolExpressionWithInt1() {
check("void f(int x) {\n"
" if ((x && 0x0f)==6)\n"
" a++;\n"
"}");
ASSERT_EQUALS("[test.cpp:2:20]: (warning) Comparison of a boolean expression with an integer other than 0 or 1. [compareBoolExpressionWithInt]\n", errout_str());
check("void f(int x) {\n"
" if ((x && 0x0f)==0)\n"
" a++;\n"
"}");
ASSERT_EQUALS("", errout_str());
check("void f(int x) {\n"
" if ((x || 0x0f)==6)\n"
" a++;\n"
"}");
ASSERT_EQUALS("[test.cpp:2:20]: (warning) Comparison of a boolean expression with an integer other than 0 or 1. [compareBoolExpressionWithInt]\n", errout_str());
check("void f(int x) {\n"
" if ((x || 0x0f)==0)\n"
" a++;\n"
"}");
ASSERT_EQUALS("", errout_str());
check("void f(int x) {\n"
" if ((x & 0x0f)==6)\n"
" a++;\n"
"}");
ASSERT_EQUALS("", errout_str());
check("void f(int x) {\n"
" if ((x | 0x0f)==6)\n"
" a++;\n"
"}");
ASSERT_EQUALS("", errout_str());
check("void f(int x) {\n"
" if ((5 && x)==3)\n"
" a++;\n"
"}");
ASSERT_EQUALS("[test.cpp:2:17]: (warning) Comparison of a boolean expression with an integer other than 0 or 1. [compareBoolExpressionWithInt]\n", errout_str());
check("void f(int x) {\n"
" if ((5 && x)==3 || (8 && x)==9)\n"
" a++;\n"
"}");
ASSERT_EQUALS(
"[test.cpp:2:17]: (warning) Comparison of a boolean expression with an integer other than 0 or 1. [compareBoolExpressionWithInt]\n"
"[test.cpp:2:32]: (warning) Comparison of a boolean expression with an integer other than 0 or 1. [compareBoolExpressionWithInt]\n",
errout_str());
check("void f(int x) {\n"
" if ((5 && x)!=3)\n"
" a++;\n"
"}");
ASSERT_EQUALS("[test.cpp:2:17]: (warning) Comparison of a boolean expression with an integer other than 0 or 1. [compareBoolExpressionWithInt]\n", errout_str());
check("void f(int x) {\n"
" if ((5 && x) > 3)\n"
" a++;\n"
"}");
ASSERT_EQUALS("[test.cpp:2:18]: (warning) Comparison of a boolean expression with an integer other than 0 or 1. [compareBoolExpressionWithInt]\n", errout_str());
check("void f(int x) {\n"
" if ((5 && x) > 0)\n"
" a++;\n"
"}");
ASSERT_EQUALS("", errout_str());
check("void f(int x) {\n"
" if ((5 && x) < 0)\n"
" a++;\n"
"}"
);
ASSERT_EQUALS("[test.cpp:2:18]: (warning) Comparison of a boolean expression with an integer. [compareBoolExpressionWithInt]\n", errout_str());
check("void f(int x) {\n"
" if ((5 && x) < 1)\n"
" a++;\n"
"}");
ASSERT_EQUALS("", errout_str());
check("void f(int x) {\n"
" if ((5 && x) > 1)\n"
" a++;\n"
"}"
);
ASSERT_EQUALS("[test.cpp:2:18]: (warning) Comparison of a boolean expression with an integer. [compareBoolExpressionWithInt]\n", errout_str());
check("void f(int x) {\n"
" if (0 < (5 && x))\n"
" a++;\n"
"}");
ASSERT_EQUALS("", errout_str());
check("void f(int x) {\n"
" if (0 > (5 && x))\n"
" a++;\n"
"}"
);
ASSERT_EQUALS("[test.cpp:2:11]: (warning) Comparison of a boolean expression with an integer. [compareBoolExpressionWithInt]\n", errout_str());
check("void f(int x) {\n"
" if (1 > (5 && x))\n"
" a++;\n"
"}");
ASSERT_EQUALS("", errout_str());
check("void f(int x) {\n"
" if (1 < (5 && x))\n"
" a++;\n"
"}"
);
ASSERT_EQUALS("[test.cpp:2:11]: (warning) Comparison of a boolean expression with an integer. [compareBoolExpressionWithInt]\n", errout_str());
check("void f(bool x ) {\n"
" if ( x > false )\n"
" a++;\n"
"}");
ASSERT_EQUALS("[test.cpp:2:12]: (warning) Comparison of a boolean value using relational operator (<, >, <= or >=). [comparisonOfBoolWithInvalidComparator]\n", errout_str());
check("void f(bool x ) {\n"
" if ( false < x )\n"
" a++;\n"
"}");
ASSERT_EQUALS("[test.cpp:2:16]: (warning) Comparison of a boolean value using relational operator (<, >, <= or >=). [comparisonOfBoolWithInvalidComparator]\n", errout_str());
check("void f(bool x ) {\n"
" if ( x < false )\n"
" a++;\n"
"}");
ASSERT_EQUALS("[test.cpp:2:12]: (warning) Comparison of a boolean value using relational operator (<, >, <= or >=). [comparisonOfBoolWithInvalidComparator]\n", errout_str());
check("void f(bool x ) {\n"
" if ( false > x )\n"
" a++;\n"
"}");
ASSERT_EQUALS("[test.cpp:2:16]: (warning) Comparison of a boolean value using relational operator (<, >, <= or >=). [comparisonOfBoolWithInvalidComparator]\n", errout_str());
check("void f(bool x ) {\n"
" if ( x >= false )\n"
" a++;\n"
"}");
ASSERT_EQUALS("[test.cpp:2:13]: (warning) Comparison of a boolean value using relational operator (<, >, <= or >=). [comparisonOfBoolWithInvalidComparator]\n", errout_str());
check("void f(bool x ) {\n"
" if ( false >= x )\n"
" a++;\n"
"}");
ASSERT_EQUALS("[test.cpp:2:17]: (warning) Comparison of a boolean value using relational operator (<, >, <= or >=). [comparisonOfBoolWithInvalidComparator]\n", errout_str());
check("void f(bool x ) {\n"
" if ( x <= false )\n"
" a++;\n"
"}");
ASSERT_EQUALS("[test.cpp:2:13]: (warning) Comparison of a boolean value using relational operator (<, >, <= or >=). [comparisonOfBoolWithInvalidComparator]\n", errout_str());
check("void f(bool x ) {\n"
" if ( false <= x )\n"
" a++;\n"
"}");
ASSERT_EQUALS("[test.cpp:2:17]: (warning) Comparison of a boolean value using relational operator (<, >, <= or >=). [comparisonOfBoolWithInvalidComparator]\n", errout_str());
check("typedef int (*func)(bool invert);\n"
"void x(int, func f);\n"
"void foo(int error) {\n"
" if (error == ABC) { }\n"
"}");
ASSERT_EQUALS("", errout_str());
check("int f() { return !a+b<c; }"); // #5072
ASSERT_EQUALS("",errout_str());
check("int f() { return (!a+b<c); }");
ASSERT_EQUALS("",errout_str());
check("int f() { return (a+(b<5)<=c); }");
ASSERT_EQUALS("",errout_str());
}
voidcomparisonOfBoolExpressionWithInt2() {
check("void f(int x) {\n"
" if (!x == 10) {\n"
" printf(\"x not equal to 10\");\n"
" }\n"
"}");
ASSERT_EQUALS("[test.cpp:2:12]: (warning) Comparison of a boolean expression with an integer other than 0 or 1. [compareBoolExpressionWithInt]\n", errout_str());
check("void f(int x) {\n"
" if (!x != 10) {\n"
" printf(\"x not equal to 10\");\n"
" }\n"
"}");
ASSERT_EQUALS("[test.cpp:2:12]: (warning) Comparison of a boolean expression with an integer other than 0 or 1. [compareBoolExpressionWithInt]\n", errout_str());
check("void f(int x) {\n"
" if (x != 10) {\n"
" printf(\"x not equal to 10\");\n"
" }\n"
"}");
ASSERT_EQUALS("", errout_str());
check("void f(int x) {\n"
" if (10 == !x) {\n"
" printf(\"x not equal to 10\");\n"
" }\n"
"}");
ASSERT_EQUALS("[test.cpp:2:12]: (warning) Comparison of a boolean expression with an integer other than 0 or 1. [compareBoolExpressionWithInt]\n", errout_str());
check("void f(int x) {\n"
" if (10 != !x) {\n"
" printf(\"x not equal to 10\");\n"
" }\n"
"}");
ASSERT_EQUALS("[test.cpp:2:12]: (warning) Comparison of a boolean expression with an integer other than 0 or 1. [compareBoolExpressionWithInt]\n", errout_str());
check("void f(int x, int y) {\n"
" if (y != !x) {\n"
" printf(\"x not equal to 10\");\n"
" }\n"
"}");
ASSERT_EQUALS("", errout_str());
check("void f(int x, bool y) {\n"
" if (y != !x) {\n"
" printf(\"x not equal to 10\");\n"
" }\n"
"}");
ASSERT_EQUALS("", errout_str());
check("void f(int x) {\n"
" if (10 != x) {\n"
" printf(\"x not equal to 10\");\n"
" }\n"
"}");
ASSERT_EQUALS("", errout_str());
check("void f(int x, int y) {\n"
" return (!y == !x);\n"
"}");
ASSERT_EQUALS("", errout_str());
check("int f(int a) {\n"
" return (x()+1 == !a);\n"
"}");
TODO_ASSERT_EQUALS("error", "", errout_str());
check("void f() { if (!!a+!!b+!!c>1){} }");
ASSERT_EQUALS("",errout_str());
check("void f(int a, int b, int c) { if (a != !b || c) {} }");
ASSERT_EQUALS("",errout_str());
check("void f(int a, int b, int c) { if (1 < !!a + !!b + !!c) {} }");
ASSERT_EQUALS("",errout_str());
check("void f(int a, int b, int c) { if (1 < !(a+b)) {} }");
ASSERT_EQUALS("[test.cpp:1:37]: (warning) Comparison of a boolean expression with an integer. [compareBoolExpressionWithInt]\n",errout_str());
}
voidcomparisonOfBoolExpressionWithInt3() {
check("int f(int x) {\n"
" return t<0>() && x;\n"
"}");
ASSERT_EQUALS("", errout_str());
}
voidcomparisonOfBoolExpressionWithInt4() {
// #5016
check("void f() {\n"
" for(int i = 4; i > -1 < 5 ; --i) {}\n"
"}");
ASSERT_EQUALS("[test.cpp:2:25]: (warning) Comparison of a boolean expression with an integer other than 0 or 1. [compareBoolExpressionWithInt]\n", errout_str());
check("void f(int a, int b, int c) {\n"
" return (a > b) < c;\n"
"}");
ASSERT_EQUALS("", errout_str());
check("void f(int a, int b, int c) {\n"
" return x(a > b) < c;\n"
"}");
ASSERT_EQUALS("", errout_str());
check("void f(int a, int b, int c) {\n"
" return a > b == c;\n"
"}");
ASSERT_EQUALS("", errout_str());
// templates
check("struct Tokenizer { TokenList list; };\n"
"void Tokenizer::f() {\n"
" std::list<Token*> locationList;\n"
"}");
ASSERT_EQUALS("", errout_str());
// #5063 - or
check("void f() {\n"
" return a > b or c < d;\n"
"}");
ASSERT_EQUALS("", errout_str());
check("int f() {\n"
" return (a < b) != 0U;\n"
"}");
ASSERT_EQUALS("", errout_str());
check("int f() {\n"
" return (a < b) != 0x0;\n"
"}");
ASSERT_EQUALS("", errout_str());
check("int f() {\n"
" return (a < b) != 42U;\n"
"}");
ASSERT_EQUALS("[test.cpp:2:18]: (warning) Comparison of a boolean expression with an integer other than 0 or 1. [compareBoolExpressionWithInt]\n", errout_str());
}
voidcheckComparisonOfFuncReturningBool1() {
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:17]: (style) Comparison of two functions returning boolean value using relational (<, >, <= or >=) operator. [comparisonOfTwoFuncsReturningBoolError]\n", errout_str());
}
voidcheckComparisonOfFuncReturningBool2() {
check("void leftOfComparison(){\n"
" int temp = 4;\n"
" bool a = true;\n"
" if(compare(temp) > a){\n"
" printf(\"foo\");\n"
" }\n"
"}\n"
"void rightOfComparison(){\n"
" int temp = 4;\n"
" bool a = true;\n"
" if(a < compare(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:4:12]: (style) Comparison of a function returning boolean value using relational (<, >, <= or >=) operator. [comparisonOfFuncReturningBoolError]\n"
"[test.cpp:11:7]: (style) Comparison of a function returning boolean value using relational (<, >, <= or >=) operator. [comparisonOfFuncReturningBoolError]\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);");
ASSERT_EQUALS("[test.cpp:3:19]: (warning) Comparison of a boolean expression with an integer other than 0 or 1. [compareBoolExpressionWithInt]\n"
"[test.cpp:3:12]: (style) Comparison of a function returning boolean value using relational (<, >, <= or >=) operator. [comparisonOfFuncReturningBoolError]\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:13]: (style) Comparison of a function returning boolean value using relational (<, >, <= or >=) operator. [comparisonOfFuncReturningBoolError]\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:17]: (style) Comparison of two functions returning boolean value using relational (<, >, <= or >=) operator. [comparisonOfTwoFuncsReturningBoolError]\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:20]: (style) Comparison of a function returning boolean value using relational (<, >, <= or >=) operator. [comparisonOfFuncReturningBoolError]\n", errout_str());
" if ((int)c1.isEmpty() < (int)c2.isEmpty()) {}\n"
" if (static_cast<int>(c1.isEmpty()) < static_cast<int>(c2.isEmpty())) {}\n"
"}\n");
ASSERT_EQUALS("[test.cpp:6:20]: (style) Comparison of two functions returning boolean value using relational (<, >, <= or >=) operator. [comparisonOfTwoFuncsReturningBoolError]\n"
"[test.cpp:7:20]: (style) Comparison of two functions returning boolean value using relational (<, >, <= or >=) operator. [comparisonOfTwoFuncsReturningBoolError]\n"
"[test.cpp:8:24]: (style) Comparison of two functions returning boolean value using relational (<, >, <= or >=) operator. [comparisonOfTwoFuncsReturningBoolError]\n"
"[test.cpp:9:36]: (style) Comparison of two functions returning boolean value using relational (<, >, <= or >=) operator. [comparisonOfTwoFuncsReturningBoolError]\n",
" if(b > a){ \n"// here warning should be displayed
" ;\n"
" }\n"
"}");
ASSERT_EQUALS("[test.cpp:4:10]: (style) Comparison of a variable having boolean value using relational (<, >, <= or >=) operator. [comparisonOfBoolWithBoolError]\n", errout_str());
// op: <
check("int main(void){\n"
" bool a = true;\n"
" bool b = false;\n"
" if(b < a){ \n"// here warning should be displayed
" ;\n"
" }\n"
"}");
ASSERT_EQUALS("[test.cpp:4:10]: (style) Comparison of a variable having boolean value using relational (<, >, <= or >=) operator. [comparisonOfBoolWithBoolError]\n", errout_str());
// op: >=
check("int main(void){\n"
" bool a = true;\n"
" bool b = false;\n"
" if(b >= a){ \n"// here warning should be displayed
" ;\n"
" }\n"
"}");
ASSERT_EQUALS("[test.cpp:4:10]: (style) Comparison of a variable having boolean value using relational (<, >, <= or >=) operator. [comparisonOfBoolWithBoolError]\n", errout_str());
// op: <=
check("int main(void){\n"
" bool a = true;\n"
" bool b = false;\n"
" if(b <= a){ \n"// here warning should be displayed
" ;\n"
" }\n"
"}");
ASSERT_EQUALS("[test.cpp:4:10]: (style) Comparison of a variable having boolean value using relational (<, >, <= or >=) operator. [comparisonOfBoolWithBoolError]\n", errout_str());
ASSERT_EQUALS("[test.cpp:5:10]: (style) Comparison of a variable having boolean value using relational (<, >, <= or >=) operator. [comparisonOfBoolWithBoolError]\n", errout_str());
}
voidbitwiseOnBoolean() { // 3062
check("void f(_Bool a, _Bool b) {\n"
" if(a & b) {}\n"
"}");
ASSERT_EQUALS("[test.cpp:2:10]: (style, inconclusive) Boolean expression 'a' is used in bitwise operation. Did you mean '&&'? [bitwiseOnBoolean]\n", errout_str());
check("void f(_Bool a, _Bool b) {\n"
" if(a | b) {}\n"
"}");
ASSERT_EQUALS("[test.cpp:2:10]: (style, inconclusive) Boolean expression 'a' is used in bitwise operation. Did you mean '||'? [bitwiseOnBoolean]\n", errout_str());
check("void f(bool a, bool b) {\n"
" if(a & !b) {}\n"
"}");
ASSERT_EQUALS("[test.cpp:2:10]: (style, inconclusive) Boolean expression 'a' is used in bitwise operation. Did you mean '&&'? [bitwiseOnBoolean]\n", errout_str());
check("void f(bool a, bool b) {\n"
" if(a | !b) {}\n"
"}");
ASSERT_EQUALS("[test.cpp:2:10]: (style, inconclusive) Boolean expression 'a' is used in bitwise operation. Did you mean '||'? [bitwiseOnBoolean]\n", errout_str());
check("bool a, b;\n"
"void f() {\n"
" if(a & b) {}\n"
"}");
ASSERT_EQUALS("[test.cpp:3:10]: (style, inconclusive) Boolean expression 'a' is used in bitwise operation. Did you mean '&&'? [bitwiseOnBoolean]\n", errout_str());
check("bool a, b;\n"
"void f() {\n"
" if(a & !b) {}\n"
"}");
ASSERT_EQUALS("[test.cpp:3:10]: (style, inconclusive) Boolean expression 'a' is used in bitwise operation. Did you mean '&&'? [bitwiseOnBoolean]\n", errout_str());
check("bool a, b;\n"
"void f() {\n"
" if(a | b) {}\n"
"}");
ASSERT_EQUALS("[test.cpp:3:10]: (style, inconclusive) Boolean expression 'a' is used in bitwise operation. Did you mean '||'? [bitwiseOnBoolean]\n", errout_str());
check("bool a, b;\n"
"void f() {\n"
" if(a | !b) {}\n"
"}");
ASSERT_EQUALS("[test.cpp:3:10]: (style, inconclusive) Boolean expression 'a' is used in bitwise operation. Did you mean '||'? [bitwiseOnBoolean]\n", errout_str());
check("void f(bool a, int b) {\n"
" if(a & b) {}\n"
"}");
ASSERT_EQUALS("[test.cpp:2:10]: (style, inconclusive) Boolean expression 'a' is used in bitwise operation. Did you mean '&&'? [bitwiseOnBoolean]\n", errout_str());
check("void f(int a, bool b) {\n"
" if(a & b) {}\n"
"}");
ASSERT_EQUALS("[test.cpp:2:10]: (style, inconclusive) Boolean expression 'b' is used in bitwise operation. Did you mean '&&'? [bitwiseOnBoolean]\n", errout_str());
check("void f(int a, int b) {\n"
" if((a > 0) & (b < 0)) {}\n"
"}");
ASSERT_EQUALS("[test.cpp:2:16]: (style, inconclusive) Boolean expression 'a>0' is used in bitwise operation. Did you mean '&&'? [bitwiseOnBoolean]\n", errout_str());
check("void f(bool a, int b) {\n"
" if(a | b) {}\n"
"}");
ASSERT_EQUALS("[test.cpp:2:10]: (style, inconclusive) Boolean expression 'a' is used in bitwise operation. Did you mean '||'? [bitwiseOnBoolean]\n", errout_str());
check("void f(int a, bool b) {\n"
" if(a | b) {}\n"
"}");
ASSERT_EQUALS("[test.cpp:2:10]: (style, inconclusive) Boolean expression 'b' is used in bitwise operation. Did you mean '||'? [bitwiseOnBoolean]\n", errout_str());
check("int f(bool a, int b) {\n"
" return a | b;\n"
"}");
ASSERT_EQUALS("", errout_str());
check("bool f(bool a, int b) {\n"
" return a | b;\n"
"}");
ASSERT_EQUALS("[test.cpp:2:14]: (style, inconclusive) Boolean expression 'a' is used in bitwise operation. Did you mean '||'? [bitwiseOnBoolean]\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());
check("void f(bool b) {\n"// #9405
" class C { void foo(bool &b) {} };\n"
"}");
ASSERT_EQUALS("", errout_str());
check("bool f();\n"
"bool g() {\n"
" return f() | f();\n"
"}\n");
ASSERT_EQUALS("", errout_str());
check("uint8 outcode(float p) {\n"
" float d = 0.;\n"
" return ((p - xm >= d) << 1) | (x - p > d);\n"
"}\n");
ASSERT_EQUALS("", errout_str());
check("int g();\n"// #10655
"void f(bool b) {\n"
" if (g() | b) {}\n"
"}\n");
ASSERT_EQUALS("[test.cpp:3:13]: (style, inconclusive) Boolean expression 'b' is used in bitwise operation. Did you mean '||'? [bitwiseOnBoolean]\n", errout_str());
check("int g();\n"
"void f(bool b) {\n"
" if (b | g()) {}\n"
"}\n");
ASSERT_EQUALS("", errout_str());
check("int g();\n"
"bool f(bool b, bool c) {\n"
" return b | g() | c;\n"
"}\n");
ASSERT_EQUALS("[test.cpp:3:20]: (style, inconclusive) Boolean expression 'c' is used in bitwise operation. Did you mean '||'? [bitwiseOnBoolean]\n", errout_str());
check("void f(int i) {\n"// #4233
" bool b = true, c = false;\n"
" b &= i;\n"
" c |= i;\n"
" if (b || c) {}\n"
"}\n");
ASSERT_EQUALS("[test.cpp:3:7]: (style, inconclusive) Boolean expression 'b' is used in bitwise operation. [bitwiseOnBoolean]\n"
"[test.cpp:4:7]: (style, inconclusive) Boolean expression 'c' is used in bitwise operation. [bitwiseOnBoolean]\n",
errout_str());
check("void f(int i, int j, bool b) {\n"
" i &= b;\n"
" j |= b;\n"
" if (b || c) {}\n"
"}\n");
ASSERT_EQUALS("", errout_str());
check("bool f(bool b, int i) {\n"
" b &= (i == 5);\n"
" return b;\n"
"}\n");
ASSERT_EQUALS("", errout_str());
check("struct S { bool b{}; };\n"// #12455
"void f(const std::unordered_map<int, S> m) {\n"
" for (const auto& e : m) {\n"
" S s;\n"
" s.b |= e.second.b;\n"
" (void)s.b;\n"
" }\n"
"}\n");
ASSERT_EQUALS("", errout_str());
}
voidincrementBoolean() {
check("bool bValue = true;\n"
"void f() { bValue++; }");
ASSERT_EQUALS("[test.cpp:2:12]: (style) Incrementing a variable of type 'bool' with postfix operator++ is deprecated by the C++ Standard. You should assign it the value 'true' instead. [incrementboolean]\n", errout_str());
check("void f(bool test){\n"
" test++;\n"
"}");
ASSERT_EQUALS("[test.cpp:2:5]: (style) Incrementing a variable of type 'bool' with postfix operator++ is deprecated by the C++ Standard. You should assign it the value 'true' instead. [incrementboolean]\n", errout_str());
check("void f(bool* test){\n"
" (*test)++;\n"
"}");
ASSERT_EQUALS("[test.cpp:2:6]: (style) Incrementing a variable of type 'bool' with postfix operator++ is deprecated by the C++ Standard. You should assign it the value 'true' instead. [incrementboolean]\n", errout_str());
check("void f(bool* test){\n"
" test[0]++;\n"
"}");
ASSERT_EQUALS("[test.cpp:2:9]: (style) Incrementing a variable of type 'bool' with postfix operator++ is deprecated by the C++ Standard. You should assign it the value 'true' instead. [incrementboolean]\n", errout_str());