Refactorization: Removed unnecessary \n and spaces in strings

Merged from LCppC.
This commit is contained in:
PKEuS 2021-02-20 12:58:42 +01:00 committed by Daniel Marjamäki
parent 25678a9faf
commit cf1937294a
35 changed files with 1284 additions and 1381 deletions

View File

@ -63,8 +63,7 @@ private:
" if (b) { a = 1+2 };\n"
" return a;\n"
"}\n"
"assert(foo() == 3); \n"
);
"assert(foo() == 3);");
ASSERT_EQUALS("", errout.str());
check(
@ -72,8 +71,7 @@ private:
" int b=a+1;\n"
" return b;\n"
"}\n"
"assert(foo(1) == 2); \n"
);
"assert(foo(1) == 2);");
ASSERT_EQUALS("", errout.str());
}
@ -84,8 +82,7 @@ private:
" a = 1+2;\n"
" return a;\n"
"}\n"
"assert(foo() == 3); \n"
);
"assert(foo() == 3);");
ASSERT_EQUALS("[test.cpp:6]: (warning) Assert statement calls a function which may have desired side effects: 'foo'.\n", errout.str());
// Ticket #4937 "false positive: Assert calls a function which may have desired side effects"
@ -97,7 +94,7 @@ private:
"};\n"
"void foo() {\n"
" assert( !SquarePack::isRank1Or8(push2) );\n"
"}\n");
"}");
ASSERT_EQUALS("", errout.str());
check("struct SquarePack {\n"
@ -108,7 +105,7 @@ private:
"};\n"
"void foo() {\n"
" assert( !SquarePack::isRank1Or8(push2) );\n"
"}\n");
"}");
ASSERT_EQUALS("[test.cpp:8]: (warning) Assert statement calls a function which may have desired side effects: 'isRank1Or8'.\n", errout.str());
check("struct SquarePack {\n"
@ -119,7 +116,7 @@ private:
"};\n"
"void foo() {\n"
" assert( !SquarePack::isRank1Or8(push2) );\n"
"}\n");
"}");
ASSERT_EQUALS("[test.cpp:8]: (warning) Assert statement calls a function which may have desired side effects: 'isRank1Or8'.\n", errout.str());
check("struct SquarePack {\n"
@ -130,7 +127,7 @@ private:
"};\n"
"void foo() {\n"
" assert( !SquarePack::isRank1Or8(push2) );\n"
"}\n");
"}");
ASSERT_EQUALS("", errout.str());
}
@ -172,60 +169,54 @@ private:
" int a; a = 0;\n"
" assert(a = 2);\n"
" return a;\n"
"}\n"
);
"}");
ASSERT_EQUALS("[test.cpp:3]: (warning) Assert statement modifies 'a'.\n", errout.str());
check("void f(int a) {\n"
" assert(a == 2);\n"
" return a;\n"
"}\n"
);
"}");
ASSERT_EQUALS("", errout.str());
check("void f(int a, int b) {\n"
" assert(a == 2 && (b = 1));\n"
" return a;\n"
"}\n"
);
"}");
ASSERT_EQUALS("[test.cpp:2]: (warning) Assert statement modifies 'b'.\n", errout.str());
check("void f() {\n"
" int a; a = 0;\n"
" assert(a += 2);\n"
" return a;\n"
"}\n"
);
"}");
ASSERT_EQUALS("[test.cpp:3]: (warning) Assert statement modifies 'a'.\n", errout.str());
check("void f() {\n"
" int a; a = 0;\n"
" assert(a *= 2);\n"
" return a;\n"
"}\n"
);
"}");
ASSERT_EQUALS("[test.cpp:3]: (warning) Assert statement modifies 'a'.\n", errout.str());
check("void f() {\n"
" int a; a = 0;\n"
" assert(a -= 2);\n"
" return a;\n"
"}\n"
);
"}");
ASSERT_EQUALS("[test.cpp:3]: (warning) Assert statement modifies 'a'.\n", errout.str());
check("void f() {\n"
" int a = 0;\n"
" assert(a--);\n"
" return a;\n"
"}\n");
"}");
ASSERT_EQUALS("[test.cpp:3]: (warning) Assert statement modifies 'a'.\n", errout.str());
check("void f() {\n"
" int a = 0;\n"
" assert(--a);\n"
" return a;\n"
"}\n");
"}");
ASSERT_EQUALS("[test.cpp:3]: (warning) Assert statement modifies 'a'.\n", errout.str());
check("void f() {\n"
@ -234,7 +225,7 @@ private:
" auto const expected = someOtherValue;\n"
" return tmp == expected;\n"
" }));\n"
"}\n");
"}");
ASSERT_EQUALS("", errout.str());
}

File diff suppressed because it is too large Load Diff

View File

@ -127,7 +127,7 @@ private:
"void f() {\n"
" S s = {0};\n"
" *s.p = true;\n"
"}\n");
"}");
ASSERT_EQUALS("", errout.str());
check("struct S {\n"
@ -136,7 +136,7 @@ private:
"void f() {\n"
" S s = {0};\n"
" s.p = true;\n"
"}\n");
"}");
ASSERT_EQUALS("[test.cpp:6]: (error) Boolean value assigned to pointer.\n", errout.str());
// ticket #5627 - false positive: template
@ -210,7 +210,7 @@ private:
"void f() {\n"
" S s = {0};\n"
" s.p = true;\n"
"}\n");
"}");
ASSERT_EQUALS("[test.cpp:6]: (style) Boolean value assigned to floating point variable.\n", errout.str());
check("struct S {\n"
@ -219,7 +219,7 @@ private:
"void f() {\n"
" S s = {0};\n"
" *s.p[0] = true;\n"
"}\n");
"}");
ASSERT_EQUALS("[test.cpp:6]: (style) Boolean value assigned to floating point variable.\n", errout.str());
}
@ -227,100 +227,88 @@ private:
check("void f(int x) {\n"
" if ((x && 0x0f)==6)\n"
" a++;\n"
"}\n"
);
"}");
ASSERT_EQUALS("[test.cpp:2]: (warning) Comparison of a boolean expression with an integer other than 0 or 1.\n", errout.str());
check("void f(int x) {\n"
" if ((x && 0x0f)==0)\n"
" a++;\n"
"}\n"
);
"}");
ASSERT_EQUALS("", errout.str());
check("void f(int x) {\n"
" if ((x || 0x0f)==6)\n"
" a++;\n"
"}\n"
);
"}");
ASSERT_EQUALS("[test.cpp:2]: (warning) Comparison of a boolean expression with an integer other than 0 or 1.\n", errout.str());
check("void f(int x) {\n"
" if ((x || 0x0f)==0)\n"
" a++;\n"
"}\n"
);
"}");
ASSERT_EQUALS("", errout.str());
check("void f(int x) {\n"
" if ((x & 0x0f)==6)\n"
" a++;\n"
"}\n"
);
"}");
ASSERT_EQUALS("", errout.str());
check("void f(int x) {\n"
" if ((x | 0x0f)==6)\n"
" a++;\n"
"}\n"
);
"}");
ASSERT_EQUALS("", errout.str());
check("void f(int x) {\n"
" if ((5 && x)==3)\n"
" a++;\n"
"}\n"
);
"}");
ASSERT_EQUALS("[test.cpp:2]: (warning) Comparison of a boolean expression with an integer other than 0 or 1.\n", errout.str());
check("void f(int x) {\n"
" if ((5 && x)==3 || (8 && x)==9)\n"
" a++;\n"
"}\n"
);
"}");
ASSERT_EQUALS("[test.cpp:2]: (warning) Comparison of a boolean expression with an integer other than 0 or 1.\n", errout.str());
check("void f(int x) {\n"
" if ((5 && x)!=3)\n"
" a++;\n"
"}\n"
);
"}");
ASSERT_EQUALS("[test.cpp:2]: (warning) Comparison of a boolean expression with an integer other than 0 or 1.\n", errout.str());
check("void f(int x) {\n"
" if ((5 && x) > 3)\n"
" a++;\n"
"}\n"
);
"}");
ASSERT_EQUALS("[test.cpp:2]: (warning) Comparison of a boolean expression with an integer other than 0 or 1.\n", errout.str());
check("void f(int x) {\n"
" if ((5 && x) > 0)\n"
" a++;\n"
"}\n"
);
"}");
ASSERT_EQUALS("", errout.str());
check("void f(int x) {\n"
" if ((5 && x) < 0)\n"
" a++;\n"
"}\n"
"}"
);
ASSERT_EQUALS("[test.cpp:2]: (warning) Comparison of a boolean expression with an integer.\n", errout.str());
check("void f(int x) {\n"
" if ((5 && x) < 1)\n"
" a++;\n"
"}\n"
);
"}");
ASSERT_EQUALS("", errout.str());
check("void f(int x) {\n"
" if ((5 && x) > 1)\n"
" a++;\n"
"}\n"
"}"
);
ASSERT_EQUALS("[test.cpp:2]: (warning) Comparison of a boolean expression with an integer.\n", errout.str());
@ -328,85 +316,75 @@ private:
check("void f(int x) {\n"
" if (0 < (5 && x))\n"
" a++;\n"
"}\n"
);
"}");
ASSERT_EQUALS("", errout.str());
check("void f(int x) {\n"
" if (0 > (5 && x))\n"
" a++;\n"
"}\n"
"}"
);
ASSERT_EQUALS("[test.cpp:2]: (warning) Comparison of a boolean expression with an integer.\n", errout.str());
check("void f(int x) {\n"
" if (1 > (5 && x))\n"
" a++;\n"
"}\n"
);
"}");
ASSERT_EQUALS("", errout.str());
check("void f(int x) {\n"
" if (1 < (5 && x))\n"
" a++;\n"
"}\n"
"}"
);
ASSERT_EQUALS("[test.cpp:2]: (warning) Comparison of a boolean expression with an integer.\n", errout.str());
check("void f(bool x ) {\n"
" if ( x > false )\n"
" a++;\n"
"}\n"
);
"}");
ASSERT_EQUALS("[test.cpp:2]: (warning) Comparison of a boolean value using relational operator (<, >, <= or >=).\n", errout.str());
check("void f(bool x ) {\n"
" if ( false < x )\n"
" a++;\n"
"}\n"
);
"}");
ASSERT_EQUALS("[test.cpp:2]: (warning) Comparison of a boolean value using relational operator (<, >, <= or >=).\n", errout.str());
check("void f(bool x ) {\n"
" if ( x < false )\n"
" a++;\n"
"}\n"
);
"}");
ASSERT_EQUALS("[test.cpp:2]: (warning) Comparison of a boolean value using relational operator (<, >, <= or >=).\n", errout.str());
check("void f(bool x ) {\n"
" if ( false > x )\n"
" a++;\n"
"}\n"
);
"}");
ASSERT_EQUALS("[test.cpp:2]: (warning) Comparison of a boolean value using relational operator (<, >, <= or >=).\n", errout.str());
check("void f(bool x ) {\n"
" if ( x >= false )\n"
" a++;\n"
"}\n"
);
"}");
ASSERT_EQUALS("[test.cpp:2]: (warning) Comparison of a boolean value using relational operator (<, >, <= or >=).\n", errout.str());
check("void f(bool x ) {\n"
" if ( false >= x )\n"
" a++;\n"
"}\n"
);
"}");
ASSERT_EQUALS("[test.cpp:2]: (warning) Comparison of a boolean value using relational operator (<, >, <= or >=).\n", errout.str());
check("void f(bool x ) {\n"
" if ( x <= false )\n"
" a++;\n"
"}\n"
);
"}");
ASSERT_EQUALS("[test.cpp:2]: (warning) Comparison of a boolean value using relational operator (<, >, <= or >=).\n", errout.str());
check("void f(bool x ) {\n"
" if ( false <= x )\n"
" a++;\n"
"}\n"
);
"}");
ASSERT_EQUALS("[test.cpp:2]: (warning) Comparison of a boolean value using relational operator (<, >, <= or >=).\n", errout.str());
check("typedef int (*func)(bool invert);\n"
@ -1180,25 +1158,25 @@ private:
check("bool f(void) {\n"
" auto x = [](void) { return -1; };\n"
" return false;\n"
"}\n");
"}");
ASSERT_EQUALS("", errout.str());
check("bool f(void) {\n"
" auto x = [](void) { return -1; };\n"
" return 2;\n"
"}\n");
"}");
ASSERT_EQUALS("[test.cpp:3]: (style) Non-boolean value returned from function returning bool\n", errout.str());
check("bool f(void) {\n"
" auto x = [](void) -> int { return -1; };\n"
" return false;\n"
"}\n");
"}");
ASSERT_EQUALS("", errout.str());
check("bool f(void) {\n"
" auto x = [](void) -> int { return -1; };\n"
" return 2;\n"
"}\n");
"}");
ASSERT_EQUALS("[test.cpp:3]: (style) Non-boolean value returned from function returning bool\n", errout.str());
}

View File

@ -371,11 +371,11 @@ private:
ASSERT_EQUALS("", errout.str());
check("int x[5];\n"
"int a = x[10];\n");
"int a = x[10];");
ASSERT_EQUALS("[test.cpp:2]: (error) Array 'x[5]' accessed at index 10, which is out of bounds.\n", errout.str());
check("int x[5];\n"
"int a = (x)[10];\n");
"int a = (x)[10];");
ASSERT_EQUALS("[test.cpp:2]: (error) Array 'x[5]' accessed at index 10, which is out of bounds.\n", errout.str());
}
@ -1509,7 +1509,7 @@ private:
" i++;\n"
" }\n"
" arr[k];\n"
"}\n");
"}");
ASSERT_EQUALS("", errout.str());
}
@ -1521,7 +1521,7 @@ private:
"void g() {\n"
" f(\"12345678\");\n"
" f(\"12345\");\n"
"}\n");
"}");
ASSERT_EQUALS("", errout.str());
}
@ -1640,7 +1640,7 @@ private:
" vertices[i][1][2] = 4.0;\n"
" vertices[i][1][3] = 5.0;\n"
" }\n"
"}\n");
"}");
ASSERT_EQUALS("", errout.str());
{
@ -2137,7 +2137,7 @@ private:
ASSERT_EQUALS("", errout.str());
check("class ActorSprite { static ImageSet * targetCursorImages[2][10]; };\n"
"ImageSet *ActorSprite::targetCursorImages[2][10];\n");
"ImageSet *ActorSprite::targetCursorImages[2][10];");
ASSERT_EQUALS("", errout.str());
}
@ -2208,7 +2208,7 @@ private:
check("constexpr int blockLen = 10;\n"
"void foo(std::array<uint8_t, blockLen * 2>& a) {\n"
" a[2] = 2;\n"
"}\n");
"}");
ASSERT_EQUALS("", errout.str());
}
@ -2546,7 +2546,7 @@ private:
" const char *src = \"AAAAAAAAAAAAAAAAAAAAA\";\n"
" for (size_t i = 0; i <= 4; i++)\n"
" dst[i] = src[i];\n"
"} } }\n");
"} } }");
ASSERT_EQUALS("[test.cpp:6]: (error) Array 'dst[4]' accessed at index 4, which is out of bounds.\n", errout.str());
}
@ -2599,8 +2599,7 @@ private:
"int main(){\n"
" testChar tc1 = \"\";\n"
" tc1[5]='a';\n"
"} \n"
);
"}");
ASSERT_EQUALS("", errout.str());
}
@ -2610,8 +2609,7 @@ private:
check("struct S { int m[9]; };\n"
"int f(S * s) {\n"
" return s->m[sizeof(s->m)];\n"
"}\n"
);
"}");
ASSERT_EQUALS("[test.cpp:3]: (error) Array 's->m[9]' accessed at index 36, which is out of bounds.\n", errout.str());
}
@ -2873,7 +2871,7 @@ private:
" const char d[] = \"0123456789\";\n"
" char *cp = d + 3;\n"
" return cp - d;\n"
"}\n");
"}");
ASSERT_EQUALS("", errout.str());
}
@ -3683,7 +3681,7 @@ private:
// Test with invalid code that there is no segfault
check("char baz[100];\n"
"strncpy(baz, \"var\", 100)\n");
"strncpy(baz, \"var\", 100)");
ASSERT_EQUALS("", errout.str());
// Test that there are no duplicate error messages
@ -3725,13 +3723,13 @@ private:
check("void bar() {\n"
" char buf[4];\n"
" strncpy(buf, \"ab\", 4);\n"
"}\n");
"}");
ASSERT_EQUALS("", errout.str());
check("void bar() {\n"
" char buf[4];\n"
" strncpy(buf, \"abcde\", 4);\n"
"}\n");
"}");
ASSERT_EQUALS("[test.cpp:3]: (warning, inconclusive) The buffer 'buf' may not be null-terminated after the call to strncpy().\n", errout.str());
}
@ -4195,7 +4193,7 @@ private:
" int *a;\n"
" a = new int[-1];\n"
" delete [] a;\n"
"}\n");
"}");
ASSERT_EQUALS("[test.cpp:4]: (error) Memory allocation size is negative.\n", errout.str());
check("void f()\n"
@ -4203,7 +4201,7 @@ private:
" int *a;\n"
" a = malloc( -10 );\n"
" free(a);\n"
"}\n");
"}");
ASSERT_EQUALS("[test.cpp:4]: (error) Memory allocation size is negative.\n", errout.str());
check("void f()\n"
@ -4211,7 +4209,7 @@ private:
" int *a;\n"
" a = malloc( -10);\n"
" free(a);\n"
"}\n");
"}");
ASSERT_EQUALS("[test.cpp:4]: (error) Memory allocation size is negative.\n", errout.str());
check("void f()\n"
@ -4219,7 +4217,7 @@ private:
" int *a;\n"
" a = alloca( -10 );\n"
" free(a);\n"
"}\n");
"}");
ASSERT_EQUALS("[test.cpp:4]: (error) Memory allocation size is negative.\n", errout.str());
}
@ -4234,7 +4232,7 @@ private:
check("int x, y;\n"
"int a[-1];\n"
"int b[x?1:-1];\n"
"int c[x?y:-1];\n");
"int c[x?y:-1];");
ASSERT_EQUALS("", errout.str());
}
@ -4242,7 +4240,7 @@ private:
check("void f() {\n"
" char arr[10];\n"
" p = arr + 20;\n"
"}\n");
"}");
ASSERT_EQUALS("[test.cpp:3]: (portability) Undefined behaviour, pointer arithmetic 'arr+20' is out of bounds.\n", errout.str());
}
@ -4404,7 +4402,7 @@ private:
check("int f() {\n"
" int i;\n"
" return (&i)[1];\n"
"}\n");
"}");
ASSERT_EQUALS(
"[test.cpp:3] -> [test.cpp:3]: (error) The address of local variable 'i' is accessed at non-zero index.\n",
errout.str());
@ -4412,7 +4410,7 @@ private:
check("int f(int j) {\n"
" int i;\n"
" return (&i)[j];\n"
"}\n");
"}");
ASSERT_EQUALS(
"[test.cpp:3] -> [test.cpp:3]: (warning) The address of local variable 'i' might be accessed at non-zero index.\n",
errout.str());
@ -4420,34 +4418,34 @@ private:
check("int f() {\n"
" int i;\n"
" return (&i)[0];\n"
"}\n");
"}");
ASSERT_EQUALS("", errout.str());
check("int f(int * i) {\n"
" return i[1];\n"
"}\n");
"}");
ASSERT_EQUALS("", errout.str());
check("int f(std::vector<int> i) {\n"
" return i[1];\n"
"}\n");
"}");
ASSERT_EQUALS("", errout.str());
check("int f(std::vector<int> i) {\n"
" return i.data()[1];\n"
"}\n");
"}");
ASSERT_EQUALS("", errout.str());
check("int* f(std::vector<int>& i) {\n"
" return &(i[1]);\n"
"}\n");
"}");
ASSERT_EQUALS("", errout.str());
check("struct A { int i; int j; };\n"
"int f() {\n"
" A x;\n"
" return (&x.i)[0];\n"
"}\n");
"}");
ASSERT_EQUALS("", errout.str());
check("struct A { int i; int j; };\n"
@ -4455,7 +4453,7 @@ private:
" A x;\n"
" int * i = &x.i;\n"
" return i[0];\n"
"}\n");
"}");
ASSERT_EQUALS("", errout.str());
check("void f() {\n"
@ -4463,7 +4461,7 @@ private:
" std::map<int, int*> m;\n"
" m[0] = &x;\n"
" m[1] = &x;\n"
"}\n");
"}");
ASSERT_EQUALS("", errout.str());
check("int f() {\n"
@ -4471,7 +4469,7 @@ private:
" std::map<int, int*> m;\n"
" m[0] = &x;\n"
" return m[0][1];\n"
"}\n");
"}");
ASSERT_EQUALS(
"[test.cpp:4] -> [test.cpp:5]: (error) The address of local variable 'x' is accessed at non-zero index.\n",
errout.str());
@ -4482,7 +4480,7 @@ private:
" m[0] = &x;\n"
" m[1] = y;\n"
" return m[1][1];\n"
"}\n");
"}");
ASSERT_EQUALS("", errout.str());
check("void print(char** test);\n"

View File

@ -321,7 +321,7 @@ private:
" test = std::move(s.test);\n"
" return *this;\n"
" }\n"
"};\n");
"};");
ASSERT_EQUALS("", errout.str());
// #8337 - False positive in copy constructor detection
@ -1126,7 +1126,7 @@ private:
"class A::B\n"
"{\n"
" B & operator=(const B & b) { return b; }\n"
"};\n");
"};");
ASSERT_EQUALS("[test.cpp:6]: (style) 'operator=' should return reference to 'this' instance.\n", errout.str());
checkOpertorEqRetRefThis(
@ -1137,7 +1137,7 @@ private:
"{\n"
" B & operator=(const B &);\n"
"};\n"
"A::B & A::B::operator=(const A::B & b) { return b; }\n");
"A::B & A::B::operator=(const A::B & b) { return b; }");
ASSERT_EQUALS("[test.cpp:8]: (style) 'operator=' should return reference to 'this' instance.\n", errout.str());
checkOpertorEqRetRefThis(
@ -1147,7 +1147,7 @@ private:
"class A::B\n"
"{\n"
" A::B & operator=(const A::B & b) { return b; }\n"
"};\n");
"};");
ASSERT_EQUALS("[test.cpp:6]: (style) 'operator=' should return reference to 'this' instance.\n", errout.str());
checkOpertorEqRetRefThis(
@ -1158,7 +1158,7 @@ private:
"{\n"
" A::B & operator=(const A::B &);\n"
"};\n"
"A::B & A::B::operator=(const A::B & b) { return b; }\n");
"A::B & A::B::operator=(const A::B & b) { return b; }");
ASSERT_EQUALS("[test.cpp:8]: (style) 'operator=' should return reference to 'this' instance.\n", errout.str());
checkOpertorEqRetRefThis(
@ -1168,7 +1168,7 @@ private:
"class A::B\n"
"{\n"
" B & operator=(const B & b) { return b; }\n"
"};\n");
"};");
ASSERT_EQUALS("[test.cpp:6]: (style) 'operator=' should return reference to 'this' instance.\n", errout.str());
checkOpertorEqRetRefThis(
@ -1179,7 +1179,7 @@ private:
"{\n"
" B & operator=(const B &);\n"
"};\n"
"A::B & A::B::operator=(const A::B & b) { return b; }\n");
"A::B & A::B::operator=(const A::B & b) { return b; }");
ASSERT_EQUALS("[test.cpp:8]: (style) 'operator=' should return reference to 'this' instance.\n", errout.str());
checkOpertorEqRetRefThis(
@ -1189,7 +1189,7 @@ private:
"class A::B\n"
"{\n"
" A::B & operator=(const A::B & b) { return b; }\n"
"};\n");
"};");
ASSERT_EQUALS("[test.cpp:6]: (style) 'operator=' should return reference to 'this' instance.\n", errout.str());
checkOpertorEqRetRefThis(
@ -1200,7 +1200,7 @@ private:
"{\n"
" A::B & operator=(const A::B &);\n"
"};\n"
"A::B & A::B::operator=(const A::B & b) { return b; }\n");
"A::B & A::B::operator=(const A::B & b) { return b; }");
ASSERT_EQUALS("[test.cpp:8]: (style) 'operator=' should return reference to 'this' instance.\n", errout.str());
}
@ -1228,7 +1228,7 @@ private:
"class NS::szp\n"
"{\n"
" szp &operator =(int *other) {}\n"
"};\n");
"};");
ASSERT_EQUALS("[test.cpp:6]: (error) No 'return' statement in non-void function causes undefined behavior.\n", errout.str());
checkOpertorEqRetRefThis(
@ -1249,7 +1249,7 @@ private:
"class NS::szp\n"
"{\n"
" NS::szp &operator =(int *other) {}\n"
"};\n");
"};");
ASSERT_EQUALS("[test.cpp:6]: (error) No 'return' statement in non-void function causes undefined behavior.\n", errout.str());
checkOpertorEqRetRefThis(
@ -1270,7 +1270,7 @@ private:
"class A::szp\n"
"{\n"
" szp &operator =(int *other) {}\n"
"};\n");
"};");
ASSERT_EQUALS("[test.cpp:6]: (error) No 'return' statement in non-void function causes undefined behavior.\n", errout.str());
checkOpertorEqRetRefThis(
@ -1291,7 +1291,7 @@ private:
"class A::szp\n"
"{\n"
" A::szp &operator =(int *other) {}\n"
"};\n");
"};");
ASSERT_EQUALS("[test.cpp:6]: (error) No 'return' statement in non-void function causes undefined behavior.\n", errout.str());
checkOpertorEqRetRefThis(
@ -1363,7 +1363,7 @@ private:
" }\n"
" return *this;\n"
" }\n"
"};\n");
"};");
ASSERT_EQUALS("", errout.str());
}
@ -1460,7 +1460,7 @@ private:
" basic_fbstring& replace() {\n"
" return replaceImplDiscr();\n"
" }\n"
"};\n");
"};");
ASSERT_EQUALS("", errout.str());
}
@ -3341,7 +3341,7 @@ private:
checkThisSubtraction("; *this = *this-x ;\n"
"this-x ;\n"
"this-x ;\n");
"this-x ;");
ASSERT_EQUALS("[test.cpp:2]: (warning) Suspicious pointer subtraction. Did you intend to write '->'?\n"
"[test.cpp:3]: (warning) Suspicious pointer subtraction. Did you intend to write '->'?\n", errout.str());
}
@ -4495,8 +4495,7 @@ private:
"{return m_strVal.c_str();}\n"
"private:\n"
"std::string m_strVal;\n"
"};\n"
);
"};");
ASSERT_EQUALS("", errout.str());
checkConst("class A{\n"
@ -4506,8 +4505,7 @@ private:
"{return m_strVal.c_str();}\n"
"private:\n"
"std::string m_strVal;\n"
"};\n"
);
"};");
ASSERT_EQUALS("[test.cpp:4]: (style, inconclusive) Technically the member function 'A::strGetString' can be const.\n", errout.str());
checkConst("class A{\n"
@ -4517,8 +4515,7 @@ private:
"{return m_strVal.c_str();}\n"
"private:\n"
"std::string m_strVal;\n"
"};\n"
);
"};");
ASSERT_EQUALS("[test.cpp:4]: (style, inconclusive) Technically the member function 'A::strGetString1' can be const.\n", errout.str());
checkConst("class A{\n"
@ -4528,8 +4525,7 @@ private:
"{return m_strVec.size();}\n"
"private:\n"
"std::vector<std::string> m_strVec;\n"
"};\n"
);
"};");
ASSERT_EQUALS("[test.cpp:4]: (style, inconclusive) Technically the member function 'A::strGetSize' can be const.\n", errout.str());
checkConst("class A{\n"
@ -4539,8 +4535,7 @@ private:
"{return m_strVec.empty();}\n"
"private:\n"
"std::vector<std::string> m_strVec;\n"
"};\n"
);
"};");
ASSERT_EQUALS("[test.cpp:4]: (style, inconclusive) Technically the member function 'A::strGetEmpty' can be const.\n", errout.str());
}
@ -4551,8 +4546,7 @@ private:
" std::swap<float>(delays_[index1], delays_[index2]);\n"
"}\n"
"float delays_[4];\n"
"};\n"
);
"};");
ASSERT_EQUALS("", errout.str());
checkConst("struct DelayBase {\n"
@ -4595,8 +4589,7 @@ private:
" x=xPos;\n"
" y=yPos;\n"
" }\n"
"};\n"
);
"};");
ASSERT_EQUALS("", errout.str());
checkConst("class AA : public P {\n"
@ -5672,7 +5665,7 @@ private:
" TemplateClass<int> a;\n"
" TemplateClass<float> b;\n"
" return 0;\n"
"}\n");
"}");
ASSERT_EQUALS("", errout.str());
}
@ -5682,7 +5675,7 @@ private:
" void f(int v) { g((char *) &v); }\n"
" void g(char *) { n++; }\n"
" int n;\n"
"};\n");
"};");
ASSERT_EQUALS("", errout.str());
}
@ -5696,7 +5689,7 @@ private:
"public:\n"
" const std::list<std::shared_ptr<int>>& get() { return m_test.m_list; }\n"
" TestList<std::shared_ptr<int>> m_test;\n"
"};\n");
"};");
ASSERT_EQUALS("[test.cpp:8]: (style, inconclusive) Technically the member function 'Test::get' can be const.\n", errout.str());
}
@ -5814,8 +5807,7 @@ private:
" {\n"
" v = 0;\n"
" }\n"
"};\n"
);
"};");
ASSERT_EQUALS("", errout.str());
}
@ -5827,8 +5819,7 @@ private:
" {\n"
" v[0] = \"Happy new year!\";\n"
" }\n"
"};\n"
);
"};");
ASSERT_EQUALS("", errout.str());
}
@ -6839,16 +6830,14 @@ private:
" int x;\n"
" Fred(int x);\n"
"};\n"
"Fred::Fred(int x) : x(x) { }\n"
);
"Fred::Fred(int x) : x(x) { }");
ASSERT_EQUALS("", errout.str());
checkSelfInitialization("class Fred {\n"
" int x;\n"
" Fred(int x);\n"
"};\n"
"Fred::Fred(int x) : x{x} { }\n"
);
"Fred::Fred(int x) : x{x} { }");
ASSERT_EQUALS("", errout.str());
checkSelfInitialization("class Fred {\n"
@ -6915,21 +6904,21 @@ private:
" A();\n"
"};\n"
"A::A()\n"
"{f();}\n");
"{f();}");
ASSERT_EQUALS("[test.cpp:7] -> [test.cpp:3]: (style) Virtual function 'f' is called from constructor 'A()' at line 7. Dynamic binding is not used.\n", errout.str());
checkVirtualFunctionCall("class A {\n"
" virtual int f();\n"
" A() {f();}\n"
"};\n"
"int A::f() { return 1; }\n");
"int A::f() { return 1; }");
ASSERT_EQUALS("[test.cpp:3] -> [test.cpp:2]: (style) Virtual function 'f' is called from constructor 'A()' at line 3. Dynamic binding is not used.\n", errout.str());
checkVirtualFunctionCall("class A : B {\n"
" int f() override;\n"
" A() {f();}\n"
"};\n"
"int A::f() { return 1; }\n");
"int A::f() { return 1; }");
ASSERT_EQUALS("[test.cpp:3] -> [test.cpp:2]: (style) Virtual function 'f' is called from constructor 'A()' at line 3. Dynamic binding is not used.\n", errout.str());
checkVirtualFunctionCall("class B {\n"
@ -6939,14 +6928,14 @@ private:
" int f();\n"
" A() {f();}\n"
"};\n"
"int A::f() { return 1; }\n");
"int A::f() { return 1; }");
ASSERT_EQUALS("[test.cpp:6] -> [test.cpp:5]: (style) Virtual function 'f' is called from constructor 'A()' at line 6. Dynamic binding is not used.\n", errout.str());
checkVirtualFunctionCall("class A\n"
"{\n"
" A() { A::f(); }\n"
" virtual void f() {}\n"
"};\n");
"};");
ASSERT_EQUALS("", errout.str());
}
@ -6957,7 +6946,7 @@ private:
" A();\n"
"};\n"
"A::A()\n"
"{pure();}\n");
"{pure();}");
ASSERT_EQUALS("[test.cpp:7] -> [test.cpp:3]: (warning) Call of pure virtual function 'pure' in constructor.\n", errout.str());
checkVirtualFunctionCall("class A\n"
@ -6967,7 +6956,7 @@ private:
" int m;\n"
"};\n"
"A::A():m(A::pure())\n"
"{}\n");
"{}");
ASSERT_EQUALS("[test.cpp:7] -> [test.cpp:3]: (warning) Call of pure virtual function 'pure' in constructor.\n", errout.str());
checkVirtualFunctionCall("class A\n"
@ -6977,7 +6966,7 @@ private:
" int m;\n"
"};\n"
"A::~A()\n"
"{pure();}\n");
"{pure();}");
ASSERT_EQUALS("[test.cpp:8] -> [test.cpp:3]: (warning) Call of pure virtual function 'pure' in destructor.\n", errout.str());
checkVirtualFunctionCall("class A\n"
@ -6988,7 +6977,7 @@ private:
" A();\n"
"};\n"
"A::A()\n"
"{nonpure();}\n");
"{nonpure();}");
ASSERT_EQUALS("[test.cpp:9] -> [test.cpp:5] -> [test.cpp:3]: (warning) Call of pure virtual function 'pure' in constructor.\n", errout.str());
checkVirtualFunctionCall("class A\n"
@ -7000,7 +6989,7 @@ private:
" int m;\n"
"};\n"
"A::A():m(nonpure())\n"
"{}\n");
"{}");
TODO_ASSERT_EQUALS("[test.cpp:9] -> [test.cpp:5] -> [test.cpp:3]: (warning) Call of pure virtual function 'pure' in constructor.\n", "", errout.str());
checkVirtualFunctionCall("class A\n"
@ -7012,7 +7001,7 @@ private:
" int m;\n"
"};\n"
"A::~A()\n"
"{nonpure();}\n");
"{nonpure();}");
ASSERT_EQUALS("[test.cpp:10] -> [test.cpp:5] -> [test.cpp:3]: (warning) Call of pure virtual function 'pure' in destructor.\n", errout.str());
checkVirtualFunctionCall("class A\n"
@ -7021,7 +7010,7 @@ private:
" A(bool b);\n"
"};\n"
"A::A(bool b)\n"
"{if (b) pure();}\n");
"{if (b) pure();}");
ASSERT_EQUALS("[test.cpp:7] -> [test.cpp:3]: (warning) Call of pure virtual function 'pure' in constructor.\n", errout.str());
checkVirtualFunctionCall("class A\n"
@ -7061,7 +7050,7 @@ private:
" A(const A & a);\n"
"};\n"
"A::A(const A & a)\n"
"{a.pure();}\n");
"{a.pure();}");
ASSERT_EQUALS("", errout.str());
checkVirtualFunctionCall("class A\n"
@ -7074,7 +7063,7 @@ private:
" virtual void pure()=0;\n"
"};\n"
"A::A()\n"
"{B b; b.pure();}\n");
"{B b; b.pure();}");
ASSERT_EQUALS("", errout.str());
}
@ -7087,7 +7076,7 @@ private:
"A::A()\n"
"{pureWithBody();}\n"
"void A::pureWithBody()\n"
"{}\n");
"{}");
ASSERT_EQUALS("", errout.str());
checkVirtualFunctionCall("class A\n"
@ -7100,7 +7089,7 @@ private:
"A::A()\n"
"{nonpure();}\n"
"void A::pureWithBody()\n"
"{}\n");
"{}");
ASSERT_EQUALS("", errout.str());
}
@ -7114,7 +7103,7 @@ private:
" A();\n"
"};\n"
"A::A()\n"
"{nonpure(false);}\n");
"{nonpure(false);}");
ASSERT_EQUALS("", errout.str());
checkVirtualFunctionCall("class A\n"
@ -7125,7 +7114,7 @@ private:
" A();\n"
"};\n"
"A::A()\n"
"{nonpure(false);}\n");
"{nonpure(false);}");
ASSERT_EQUALS("", errout.str());
checkVirtualFunctionCall("class A\n"
@ -7140,7 +7129,7 @@ private:
" A();\n"
"};\n"
"A::A()\n"
"{nonpure(false);}\n");
"{nonpure(false);}");
ASSERT_EQUALS("", errout.str());
}

File diff suppressed because it is too large Load Diff

View File

@ -442,7 +442,7 @@ private:
check("class Fred {\n"
" int x=1;\n"
" int *y=0;\n"
"};\n");
"};");
ASSERT_EQUALS("", errout.str());
}
@ -455,7 +455,7 @@ private:
" {}\n"
"private:\n"
" int x;\n"
"};\n");
"};");
ASSERT_EQUALS("", errout.str());
check("class Fred : public Base<A, B> {"
@ -466,7 +466,7 @@ private:
" {}\n"
"private:\n"
" int x;\n"
"};\n");
"};");
ASSERT_EQUALS("", errout.str());
}
@ -534,7 +534,7 @@ private:
void noConstructor7() {
// ticket #4391
check("short bar;\n"
"class foo;\n");
"class foo;");
ASSERT_EQUALS("", errout.str());
}
@ -542,7 +542,7 @@ private:
// ticket #4404
check("class LineSegment;\n"
"class PointArray { };\n"
"void* tech_ = NULL;\n");
"void* tech_ = NULL;");
ASSERT_EQUALS("", errout.str());
}
@ -573,7 +573,7 @@ private:
" virtual ~A();\n"
"private:\n"
" wxTimer *WxTimer1;\n"
"};\n");
"};");
ASSERT_EQUALS("", errout.str());
}
@ -602,15 +602,15 @@ private:
// ticket #3190 "SymbolDatabase: Parse of sub class constructor fails"
void forwardDeclaration() {
check("class foo;\n"
"int bar;\n");
"int bar;");
ASSERT_EQUALS("", errout.str());
check("class foo;\n"
"class foo;\n");
"class foo;");
ASSERT_EQUALS("", errout.str());
check("class foo{};\n"
"class foo;\n");
"class foo;");
ASSERT_EQUALS("", errout.str());
}
@ -1121,7 +1121,7 @@ private:
" int foo_;\n"
"};\n"
"Foo::Foo() : Foo(0) {}\n"
"Foo::Foo(int foo) : foo_(foo) {}\n");
"Foo::Foo(int foo) : foo_(foo) {}");
ASSERT_EQUALS("", errout.str());
// Noexcept ctors
@ -2277,8 +2277,7 @@ private:
"Fred::Fred(struct C c, D d) { }\n"
"Fred::Fred(E e, struct F f) { }\n"
"Fred::Fred(G g, H h) { }\n"
"Fred::Fred(struct I i, struct J j) { }\n"
);
"Fred::Fred(struct I i, struct J j) { }");
ASSERT_EQUALS("[test.cpp:10]: (warning) Member variable 'Fred::x' is not initialized in the constructor.\n"
"[test.cpp:11]: (warning) Member variable 'Fred::x' is not initialized in the constructor.\n"
"[test.cpp:12]: (warning) Member variable 'Fred::x' is not initialized in the constructor.\n"
@ -2470,7 +2469,7 @@ private:
"}\n"
"using namespace NS;\n"
"MyClass::~MyClass() { }\n"
"MyClass::MyClass() : SomeVar(false) { }\n");
"MyClass::MyClass() : SomeVar(false) { }");
ASSERT_EQUALS("", errout.str());
}
@ -2488,7 +2487,7 @@ private:
"}\n"
"void MyClass::Restart() {\n"
" m_retCode = 0;\n"
"}\n");
"}");
ASSERT_EQUALS("", errout.str());
}
@ -2501,7 +2500,7 @@ private:
" {\n"
" if (1) {}\n"
" }\n"
"};\n");
"};");
ASSERT_EQUALS("[test.cpp:5]: (warning) Member variable 'Foo::member' is not initialized in the constructor.\n", errout.str());
check("class Foo {\n"
" friend class Bar;\n"
@ -2511,7 +2510,7 @@ private:
" {\n"
" while (1) {}\n"
" }\n"
"};\n");
"};");
ASSERT_EQUALS("[test.cpp:5]: (warning) Member variable 'Foo::member' is not initialized in the constructor.\n", errout.str());
check("class Foo {\n"
" friend class Bar;\n"
@ -2521,7 +2520,7 @@ private:
" {\n"
" for (;;) {}\n"
" }\n"
"};\n");
"};");
ASSERT_EQUALS("[test.cpp:5]: (warning) Member variable 'Foo::member' is not initialized in the constructor.\n", errout.str());
}
@ -3535,7 +3534,7 @@ private:
"public:\n"
" C() _STLP_NOTHROW {}\n"
" C(const C&) _STLP_NOTHROW {}\n"
"};\n");
"};");
ASSERT_EQUALS("", errout.str());
}
@ -3890,7 +3889,7 @@ private:
"class C {\n"
" private:\n"
" A* b;\n"
"};\n");
"};");
ASSERT_EQUALS("", errout.str());
check("template<class T> class A{};\n"
@ -3902,7 +3901,7 @@ private:
" bool m_value;\n"
"};\n"
"template<class T1, class T2>\n"
"A<B<T1, T2>>::A() : m_value(false) {}\n");
"A<B<T1, T2>>::A() : m_value(false) {}");
ASSERT_EQUALS("", errout.str());
}

View File

@ -326,13 +326,13 @@ private:
"void func3() noexcept(true) { throw 1; }\n"
"void func4() noexcept(false) { throw 1; }\n"
"void func5() noexcept(true) { func1(); }\n"
"void func6() noexcept(false) { func1(); }\n");
"void func6() noexcept(false) { func1(); }");
ASSERT_EQUALS("[test.cpp:2]: (error) Exception thrown in function declared not to throw exceptions.\n"
"[test.cpp:3]: (error) Exception thrown in function declared not to throw exceptions.\n"
"[test.cpp:5]: (error) Exception thrown in function declared not to throw exceptions.\n", errout.str());
// avoid false positives
check("const char *func() noexcept { return 0; }\n");
check("const char *func() noexcept { return 0; }");
ASSERT_EQUALS("", errout.str());
}
@ -341,12 +341,12 @@ private:
"void func2() throw() { throw 1; }\n"
"void func3() throw(int) { throw 1; }\n"
"void func4() throw() { func1(); }\n"
"void func5() throw(int) { func1(); }\n");
"void func5() throw(int) { func1(); }");
ASSERT_EQUALS("[test.cpp:2]: (error) Exception thrown in function declared not to throw exceptions.\n"
"[test.cpp:4]: (error) Exception thrown in function declared not to throw exceptions.\n", errout.str());
// avoid false positives
check("const char *func() throw() { return 0; }\n");
check("const char *func() throw() { return 0; }");
ASSERT_EQUALS("", errout.str());
}
@ -377,12 +377,12 @@ private:
void nothrowAttributeThrow() {
check("void func1() throw(int) { throw 1; }\n"
"void func2() __attribute((nothrow)); void func2() { throw 1; }\n"
"void func3() __attribute((nothrow)); void func3() { func1(); }\n");
"void func3() __attribute((nothrow)); void func3() { func1(); }");
ASSERT_EQUALS("[test.cpp:2]: (error) Exception thrown in function declared not to throw exceptions.\n"
"[test.cpp:3]: (error) Exception thrown in function declared not to throw exceptions.\n", errout.str());
// avoid false positives
check("const char *func() __attribute((nothrow)); void func1() { return 0; }\n");
check("const char *func() __attribute((nothrow)); void func1() { return 0; }");
ASSERT_EQUALS("", errout.str());
}
@ -391,19 +391,19 @@ private:
" void copyMemberValues() throw () {\n"
" copyMemberValues();\n"
" }\n"
"};\n");
"};");
ASSERT_EQUALS("", errout.str());
}
void nothrowDeclspecThrow() {
check("void func1() throw(int) { throw 1; }\n"
"void __declspec(nothrow) func2() { throw 1; }\n"
"void __declspec(nothrow) func3() { func1(); }\n");
"void __declspec(nothrow) func3() { func1(); }");
ASSERT_EQUALS("[test.cpp:2]: (error) Exception thrown in function declared not to throw exceptions.\n"
"[test.cpp:3]: (error) Exception thrown in function declared not to throw exceptions.\n", errout.str());
// avoid false positives
check("const char *func() __attribute((nothrow)); void func1() { return 0; }\n");
check("const char *func() __attribute((nothrow)); void func1() { return 0; }");
ASSERT_EQUALS("", errout.str());
}
};

View File

@ -518,7 +518,7 @@ private:
" size_t l1 = strlen(&s1.x);\n"
" size_t l2 = strlen(&s2.x);\n"
" return l1 + l2;\n"
"}\n");
"}");
TODO_ASSERT_EQUALS("[test.cpp:9]: (error) Invalid strlen() argument nr 1. A nul-terminated string is required.\n", "", errout.str());
check("const char x = 'x'; size_t f() { return strlen(&x); }");
@ -537,13 +537,13 @@ private:
" char * a = \"Hello world\";\n"
" char ** b = &a;\n"
" return strlen(&b[0][0]);\n"
"}\n");
"}");
ASSERT_EQUALS("", errout.str());
check("size_t f() {\n"
" char ca[] = \"asdf\";\n"
" return strlen((char*) &ca);\n"
"}\n");
"}");
ASSERT_EQUALS("", errout.str());
// #5225
@ -554,7 +554,7 @@ private:
" strcat(str, &d);\n"
" puts(str);\n"
" return 0;\n"
"}\n");
"}");
ASSERT_EQUALS("[test.cpp:5]: (error) Invalid strcat() argument nr 2. A nul-terminated string is required.\n", errout.str());
}
@ -1221,7 +1221,7 @@ private:
check("template <typename... a> uint8_t b(std::tuple<uint8_t> d) {\n"
" std::tuple<a...> c{std::move(d)};\n"
" return std::get<0>(c);\n"
"}\n");
"}");
ASSERT_EQUALS("", errout.str());
check("struct A { int x; };\n"
@ -1229,7 +1229,7 @@ private:
"A f(int x, Ts... xs) {\n"
" return {std::move(x), static_cast<int>(xs)...};\n"
"}\n"
"A g() { return f(1); }\n");
"A g() { return f(1); }");
ASSERT_EQUALS("", errout.str());
}
@ -1257,17 +1257,17 @@ private:
void memsetZeroBytes() {
check("void f() {\n"
" memset(p, 10, 0x0);\n"
"}\n");
"}");
ASSERT_EQUALS("[test.cpp:2]: (warning) memset() called to fill 0 bytes.\n", errout.str());
check("void f() {\n"
" memset(p, sizeof(p), 0);\n"
"}\n");
"}");
ASSERT_EQUALS("[test.cpp:2]: (warning) memset() called to fill 0 bytes.\n", errout.str());
check("void f() {\n"
" memset(p, sizeof(p), i);\n"
"}\n");
"}");
ASSERT_EQUALS("", errout.str());
// #6269 false positives in case of overloaded standard library functions
@ -1282,7 +1282,7 @@ private:
// #7285
check("void f() {\n"
" memset(&tm, sizeof(tm), 0);\n"
"}\n");
"}");
ASSERT_EQUALS("[test.cpp:2]: (warning) memset() called to fill 0 bytes.\n", errout.str());
}
@ -1293,7 +1293,7 @@ private:
" memset(is, 1.0f, 40);\n"
" int* is2 = new int[10];\n"
" memset(is2, 0.1f, 40);\n"
"}\n");
"}");
ASSERT_EQUALS("[test.cpp:3]: (portability) The 2nd memset() argument '1.0f' is a float, its representation is implementation defined.\n"
"[test.cpp:5]: (portability) The 2nd memset() argument '0.1f' is a float, its representation is implementation defined.\n", errout.str());
@ -1301,19 +1301,19 @@ private:
" int* is = new int[10];\n"
" float g = computeG();\n"
" memset(is, g, 40);\n"
"}\n");
"}");
ASSERT_EQUALS("[test.cpp:4]: (portability) The 2nd memset() argument 'g' is a float, its representation is implementation defined.\n", errout.str());
check("void f() {\n"
" int* is = new int[10];\n"
" memset(is, 0.0f, 40);\n"
"}\n");
"}");
ASSERT_EQUALS("", errout.str());
check("void f() {\n" // FP
" float x = 2.3f;\n"
" memset(a, (x?64:0), 40);\n"
"}\n");
"}");
ASSERT_EQUALS("", errout.str());
check("void f() {\n"
@ -1321,7 +1321,7 @@ private:
" memset(ss, 256, 4);\n"
" short ss2[2];\n"
" memset(ss2, -129, 4);\n"
"}\n");
"}");
ASSERT_EQUALS("[test.cpp:3]: (warning) The 2nd memset() argument '256' doesn't fit into an 'unsigned char'.\n"
"[test.cpp:5]: (warning) The 2nd memset() argument '-129' doesn't fit into an 'unsigned char'.\n", errout.str());
@ -1336,14 +1336,14 @@ private:
" memset(cs2, 255, 30);\n"
" char cs3[30];\n"
" memset(cs3, 0, 30);\n"
"}\n");
"}");
ASSERT_EQUALS("", errout.str());
check("void f() {\n"
" int is[10];\n"
" const int i = g();\n"
" memset(is, 1.0f + i, 40);\n"
"}\n");
"}");
ASSERT_EQUALS("[test.cpp:4]: (portability) The 2nd memset() argument '1.0f+i' is a float, its representation is implementation defined.\n", errout.str());
}
};

View File

@ -582,7 +582,7 @@ private:
checkCode("struct R1 {\n"
" int a;\n"
" R1 () : a { }\n"
"};\n");
"};");
}
void garbageCode30() {
@ -989,7 +989,7 @@ private:
" ~A() { printf(\"A d'tor\\n\"); }\n"
"};\n"
" const A& foo(const A& arg) { return arg; }\n"
" foo(A(12)).Var\n");
" foo(A(12)).Var");
}
void garbageCode128() {
@ -1246,7 +1246,7 @@ private:
}
void garbageCode158() { // #3238
checkCode("__FBSDID(\"...\");\n");
checkCode("__FBSDID(\"...\");");
}
void garbageCode159() { // #7119
@ -1331,7 +1331,7 @@ private:
" void f() {\n"
" s.operator A<A<int> >();\n"
" }\n"
"}\n");
"}");
checkCode( // #6034
"template<template<typename...> class T, typename... Args>\n"
@ -1345,8 +1345,7 @@ private:
"\n"
"int main() {\n"
" foo<int_<0> >::value;\n"
"}\n"
);
"}");
checkCode( // #6117
"template <typename ...> struct something_like_tuple\n"
@ -1364,8 +1363,7 @@ private:
"\n"
"typedef something_like_tuple<char, int, float> something_like_tuple_t;\n"
"SA ((is_last<float, something_like_tuple_t>::value == false));\n"
"SA ((is_last<int, something_like_tuple_t>::value == false));\n"
);
"SA ((is_last<int, something_like_tuple_t>::value == false));");
checkCode( // #6225
"template <typename...>\n"
@ -1375,8 +1373,7 @@ private:
" template <typename T>\n"
" struct A {};\n"
" using AliasA = A<T>;\n"
"}\n"
);
"}");
// #3449
ASSERT_EQUALS("template < typename T > struct A ;\n"
@ -1493,7 +1490,7 @@ private:
" pOut->CreateObject( nIndex, GDI_BRUSH, new WinMtfFillStyle( ReadColor(), ( nStyle == BS_HOLLOW ) ? TRUE : FALSE ) );\n"
" return bStatus;\n"
" };\n"
"}\n");
"}");
}
// #8151 - segfault due to incorrect template syntax
@ -1766,20 +1763,20 @@ private:
"template< class T >\n"
"template< class Predicate > int\n"
"List<T>::DeleteIf( const Predicate &pred )\n"
"{}\n");
"{}");
// #8749
checkCode(
"struct A {\n"
" void operator+=(A&) && = delete;\n"
"};\n");
"};");
// #8788
checkCode(
"struct foo;\n"
"void f() {\n"
" auto fn = []() -> foo* { return new foo(); };\n"
"}\n");
"}");
}
};

View File

@ -307,13 +307,13 @@ private:
check("struct A { void operator()(int); };\n"
"void f() {\n"
"A{}(0);\n"
"}\n");
"}");
ASSERT_EQUALS("", errout.str());
check("template<class> struct A { void operator()(int); };\n"
"void f() {\n"
"A<int>{}(0);\n"
"}\n");
"}");
ASSERT_EQUALS("", errout.str());
}
@ -342,7 +342,7 @@ private:
check("void foo(int,const char*,int);\n"
"void f(int value) {\n"
" foo(42,\"test\",42),(value&42);\n"
"}\n");
"}");
ASSERT_EQUALS("[test.cpp:3]: (warning) Found suspicious operator ','\n", errout.str());
}

View File

@ -2059,7 +2059,7 @@ private:
" scanf(\"%lf\",&v4[0]);\n"
" myvector<char *> v5(1);\n"
" scanf(\"%10s\",v5[0]);\n"
"}\n");
"}");
ASSERT_EQUALS("", errout.str());
{
@ -2089,7 +2089,7 @@ private:
check("void g() {\n"
" const char c[]=\"42\";\n"
" scanf(\"%s\", c);\n"
"}\n");
"}");
ASSERT_EQUALS("[test.cpp:3]: (warning) %s in format string (no. 1) requires a 'char *' but the argument type is 'const char *'.\n"
"[test.cpp:3]: (warning) scanf() without field width limits can crash with huge input data.\n", errout.str());
}
@ -2759,7 +2759,7 @@ private:
check("void f(int len, int newline) {\n"
" printf(\"%s\", newline ? a : str + len);\n"
" printf(\"%s\", newline + newline);\n"
"}\n");
"}");
ASSERT_EQUALS("[test.cpp:3]: (warning) %s in format string (no. 1) requires 'char *' but the argument type is 'signed int'.\n", errout.str());
check("struct Fred { int i; } f;\n"
@ -2829,27 +2829,27 @@ private:
// #4984
check("void f(double *x) {\n"
" printf(\"%f\", x[0]);\n"
"}\n");
"}");
ASSERT_EQUALS("", errout.str());
check("int array[10];\n"
"int * foo() { return array; }\n"
"void f() {\n"
" printf(\"%f\", foo()[0]);\n"
"}\n");
"}");
ASSERT_EQUALS("[test.cpp:4]: (warning) %f in format string (no. 1) requires 'double' but the argument type is 'signed int'.\n", errout.str());
check("struct Base { int length() { } };\n"
"struct Derived : public Base { };\n"
"void foo(Derived * d) {\n"
" printf(\"%f\", d.length());\n"
"}\n");
"}");
ASSERT_EQUALS("[test.cpp:4]: (warning) %f in format string (no. 1) requires 'double' but the argument type is 'signed int'.\n", errout.str());
check("std::vector<int> v;\n"
"void foo() {\n"
" printf(\"%d %u %f\", v[0], v[0], v[0]);\n"
"}\n");
"}");
ASSERT_EQUALS("[test.cpp:3]: (warning) %u in format string (no. 2) requires 'unsigned int' but the argument type is 'signed int'.\n"
"[test.cpp:3]: (warning) %f in format string (no. 3) requires 'double' but the argument type is 'signed int'.\n", errout.str());
@ -2857,7 +2857,7 @@ private:
check("int bar(int a);\n"
"void foo() {\n"
" printf(\"%d\", bar(0));\n"
"}\n");
"}");
ASSERT_EQUALS("", errout.str());
check("std::vector<int> v;\n"
@ -3103,20 +3103,20 @@ private:
check("void foo() {\n"
" printf(\"%f %d\", static_cast<int>(1.0f), reinterpret_cast<const void *>(0));\n"
"}\n");
"}");
ASSERT_EQUALS("[test.cpp:2]: (warning) %f in format string (no. 1) requires 'double' but the argument type is 'signed int'.\n"
"[test.cpp:2]: (warning) %d in format string (no. 2) requires 'int' but the argument type is 'const void *'.\n", errout.str());
check("void foo() {\n"
" UNKNOWN * u;\n"
" printf(\"%d %x %u %f\", u[i], u[i], u[i], u[i]);\n"
"}\n");
"}");
ASSERT_EQUALS("", errout.str());
check("void foo() {\n"
" long * l;\n"
" printf(\"%d %x %u %f\", l[i], l[i], l[i], l[i]);\n"
"}\n");
"}");
ASSERT_EQUALS("[test.cpp:3]: (warning) %d in format string (no. 1) requires 'int' but the argument type is 'signed long'.\n"
"[test.cpp:3]: (warning) %x in format string (no. 2) requires 'unsigned int' but the argument type is 'signed long'.\n"
"[test.cpp:3]: (warning) %u in format string (no. 3) requires 'unsigned int' but the argument type is 'signed long'.\n"
@ -3137,27 +3137,27 @@ private:
" printf(\"%u\",v6[0]);\n"
" myvector<char *> v7(1,0);\n"
" printf(\"%s\",v7[0]);\n"
"}\n");
"}");
ASSERT_EQUALS("", errout.str());
check("std::vector<char> v;\n" // #5151
"void foo() {\n"
" printf(\"%c %u %f\", v.at(32), v.at(32), v.at(32));\n"
"}\n");
"}");
ASSERT_EQUALS("[test.cpp:3]: (warning) %u in format string (no. 2) requires 'unsigned int' but the argument type is 'char'.\n"
"[test.cpp:3]: (warning) %f in format string (no. 3) requires 'double' but the argument type is 'char'.\n", errout.str());
// #5195 (segmentation fault)
check("void T::a(const std::vector<double>& vx) {\n"
" printf(\"%f\", vx.at(0));\n"
"}\n");
"}");
ASSERT_EQUALS("", errout.str());
// #5486
check("void foo() {\n"
" ssize_t test = 0;\n"
" printf(\"%zd\", test);\n"
"}\n");
"}");
ASSERT_EQUALS("", errout.str());
// #6009
@ -3165,7 +3165,7 @@ private:
"extern int IntByReturnValue();\n"
"void MyFunction() {\n"
" printf( \"%s - %s\", StringByReturnValue(), IntByReturnValue() );\n"
"}\n");
"}");
ASSERT_EQUALS("[test.cpp:4]: (warning) %s in format string (no. 1) requires 'char *' but the argument type is 'std::string'.\n"
"[test.cpp:4]: (warning) %s in format string (no. 2) requires 'char *' but the argument type is 'signed int'.\n", errout.str());
@ -3178,7 +3178,7 @@ private:
" Array<int, 10> array1;\n"
" Array<float, 10> array2;\n"
" printf(\"%u %u\", array1[0], array2[0]);\n"
"}\n");
"}");
ASSERT_EQUALS("[test.cpp:9]: (warning) %u in format string (no. 1) requires 'unsigned int' but the argument type is 'int'.\n"
"[test.cpp:9]: (warning) %u in format string (no. 2) requires 'unsigned int' but the argument type is 'float'.\n", errout.str());
@ -4711,7 +4711,7 @@ private:
void testTernary() { // ticket #6182
check("void test(const std::string &val) {\n"
" printf(\"%s\", val.empty() ? \"I like to eat bananas\" : val.c_str());\n"
"}\n");
"}");
ASSERT_EQUALS("", errout.str());
}
@ -4719,30 +4719,30 @@ private:
check("void test() {\n"
" unsigned const x = 5;\n"
" printf(\"%u\", x);\n"
"}\n");
"}");
ASSERT_EQUALS("", errout.str());
}
void testAstType() { // ticket #7014
check("void test() {\n"
" printf(\"%c\", \"hello\"[0]);\n"
"}\n");
"}");
ASSERT_EQUALS("", errout.str());
check("void test() {\n"
" printf(\"%lld\", (long long)1);\n"
"}\n");
"}");
ASSERT_EQUALS("", errout.str());
check("void test() {\n"
" printf(\"%i\", (short *)x);\n"
"}\n");
"}");
ASSERT_EQUALS("[test.cpp:2]: (warning) %i in format string (no. 1) requires 'int' but the argument type is 'signed short *'.\n", errout.str());
check("int (*fp)();\n" // #7178 - function pointer call
"void test() {\n"
" printf(\"%i\", fp());\n"
"}\n");
"}");
ASSERT_EQUALS("", errout.str());
}
@ -4750,7 +4750,7 @@ private:
check("void foo() {\n"
" printf(\"%u %lu %llu\", 0U, 0UL, 0ULL);\n"
" printf(\"%u %lu %llu\", 0u, 0ul, 0ull);\n"
"}\n");
"}");
ASSERT_EQUALS("", errout.str());
}
@ -4806,17 +4806,17 @@ private:
void testPrintfParenthesis() { // #8489
check("void f(int a) {\n"
" printf(\"%f\", (a >> 24) & 0xff);\n"
"}\n");
"}");
ASSERT_EQUALS("[test.cpp:2]: (warning) %f in format string (no. 1) requires 'double' but the argument type is 'signed int'.\n", errout.str());
check("void f(int a) {\n"
" printf(\"%f\", 0xff & (a >> 24));\n"
"}\n");
"}");
ASSERT_EQUALS("[test.cpp:2]: (warning) %f in format string (no. 1) requires 'double' but the argument type is 'signed int'.\n", errout.str());
check("void f(int a) {\n"
" printf(\"%f\", ((a >> 24) + 1) & 0xff);\n"
"}\n");
"}");
ASSERT_EQUALS("[test.cpp:2]: (warning) %f in format string (no. 1) requires 'double' but the argument type is 'signed int'.\n", errout.str());
}
};

View File

@ -230,7 +230,7 @@ private:
" free(buf);\n"
" else\n"
" free(new_buf);\n"
"}\n");
"}");
ASSERT_EQUALS("", errout.str());
}
@ -244,7 +244,7 @@ private:
" }\n"
" free(pData);\n"
" return true;\n"
"}\n");
"}");
ASSERT_EQUALS("", errout.str());
}
@ -256,7 +256,7 @@ private:
" if (!m_buf) {\n"
" m_buf = origBuf;\n"
" }\n"
"}\n");
"}");
ASSERT_EQUALS("", errout.str());
}
@ -264,7 +264,7 @@ private:
check("void foo()\n"
"{\n"
" x = realloc(x,100);\n"
"}\n");
"}");
ASSERT_EQUALS("", errout.str());
}
@ -274,7 +274,7 @@ private:
" pa = pb = malloc(10);\n"
" pa = realloc(pa, 20);"
" exit();\n"
"}\n");
"}");
ASSERT_EQUALS("", errout.str());
}
@ -285,7 +285,7 @@ private:
" if (!p)\n"
" error();\n"
" usep(p);\n"
"}\n");
"}");
ASSERT_EQUALS("", errout.str());
}
@ -317,7 +317,7 @@ private:
" if (!p)\n"
" error();\n"
" usep(p);\n"
"}\n");
"}");
ASSERT_EQUALS("", errout.str());
}
@ -328,7 +328,7 @@ private:
" if( m_options == NULL )\n"
" return false;\n"
" return true;\n"
"}\n");
"}");
ASSERT_EQUALS("[test.cpp:3]: (error) Common realloc mistake: \'m_options\' nulled but not freed upon failure\n", errout.str());
}
@ -338,7 +338,7 @@ private:
" if (zLine) {\n"
" free(zLine);\n"
" }\n"
"}\n");
"}");
ASSERT_EQUALS("", errout.str());
}
@ -1559,7 +1559,7 @@ private:
" data_ = new char[42];\n"
" delete data_;\n"
" data_ = 0;\n"
"}\n");
"}");
ASSERT_EQUALS("[test.cpp:17]: (warning) Possible leak in public function. The pointer 'data_' is not deallocated before it is allocated.\n"
"[test.cpp:18]: (error) Mismatching allocation and deallocation: Foo::data_\n", errout.str());
@ -1582,7 +1582,7 @@ private:
" data_ = new char[42];\n"
" delete data_;\n"
" data_ = 0;\n"
"}\n");
"}");
ASSERT_EQUALS("[test.cpp:17]: (warning) Possible leak in public function. The pointer 'data_' is not deallocated before it is allocated.\n"
"[test.cpp:18]: (error) Mismatching allocation and deallocation: Foo::data_\n", errout.str());
}
@ -2438,18 +2438,18 @@ private:
void resourceLeak() {
check("void foo() {\n"
" fopen(\"file.txt\", \"r\");\n"
"}\n");
"}");
ASSERT_EQUALS("[test.cpp:2]: (error) Return value of allocation function 'fopen' is not stored.\n", errout.str());
check("void foo() {\n"
" FILE f* = fopen(\"file.txt\", \"r\");\n"
" freopen(\"file.txt\", \"r\", f);\n"
"}\n");
"}");
ASSERT_EQUALS("[test.cpp:3]: (error) Return value of allocation function 'freopen' is not stored.\n", errout.str());
check("void foo() {\n"
" freopen(\"file.txt\", \"r\", stdin);\n"
"}\n");
"}");
ASSERT_EQUALS("", errout.str());
check("struct Holder {\n"
@ -2459,7 +2459,7 @@ private:
"};\n"
"void foo() {\n"
" Holder h ( fopen(\"file.txt\", \"r\"));\n"
"}\n");
"}");
ASSERT_EQUALS("", errout.str());
check("struct Holder {\n"
@ -2469,7 +2469,7 @@ private:
"};\n"
"void foo() {\n"
" Holder ( fopen(\"file.txt\", \"r\"));\n"
"}\n");
"}");
ASSERT_EQUALS("", errout.str());
check("struct Holder {\n"
@ -2479,7 +2479,7 @@ private:
"};\n"
"void foo() {\n"
" Holder h { fopen(\"file.txt\", \"r\")};\n"
"}\n");
"}");
ASSERT_EQUALS("", errout.str());
check("struct Holder {\n"
@ -2489,7 +2489,7 @@ private:
"};\n"
"void foo() {\n"
" Holder h = fopen(\"file.txt\", \"r\");\n"
"}\n");
"}");
ASSERT_EQUALS("", errout.str());
check("struct Holder {\n"
@ -2499,7 +2499,7 @@ private:
"};\n"
"void foo() {\n"
" Holder { fopen(\"file.txt\", \"r\")};\n"
"}\n");
"}");
ASSERT_EQUALS("", errout.str());
check("struct Holder {\n"
@ -2509,7 +2509,7 @@ private:
"};\n"
"void foo() {\n"
" Holder { 0, fopen(\"file.txt\", \"r\")};\n"
"}\n");
"}");
ASSERT_EQUALS("", errout.str());
}

View File

@ -275,7 +275,7 @@ private:
" while (tok) tok = tok->next();\n"
" }\n"
" tok->str();\n"
"}\n");
"}");
ASSERT_EQUALS("[test.cpp:3] -> [test.cpp:5]: (warning) Either the condition 'tok' is redundant or there is possible null pointer dereference: tok.\n", errout.str());
check("int foo(const Token *tok)\n"
@ -300,7 +300,7 @@ private:
" while (d && d->i == 0)\n"
" d = d->c;\n"
" if (!d) throw;\n"
"}\n");
"}");
ASSERT_EQUALS("", errout.str());
check("struct b {\n"
@ -1429,7 +1429,7 @@ private:
" int i = s ? s->value + 1\n"
" : s->value - 1; // <-- null ptr dereference\n"
" return i;\n"
"}\n");
"}");
TODO_ASSERT_EQUALS("[test.cpp:4]: (warning) Possible null pointer dereference: s\n", "", errout.str());
}
@ -1573,7 +1573,7 @@ private:
"void f(struct A *a) {\n"
" if (a->x == NULL) {}\n"
" *(a->x);\n"
"}\n");
"}");
ASSERT_EQUALS(
"[test.cpp:3] -> [test.cpp:4]: (warning) Either the condition 'a->x==NULL' is redundant or there is possible null pointer dereference: a->x.\n",
errout.str());
@ -1584,7 +1584,7 @@ private:
"void f(struct A *a) {\n"
" if (a->x == nullptr) {}\n"
" *(a->x);\n"
"}\n");
"}");
ASSERT_EQUALS(
"[test.cpp:3] -> [test.cpp:4]: (warning) Either the condition 'a->x==nullptr' is redundant or there is possible null pointer dereference: a->x.\n",
errout.str());
@ -1595,7 +1595,7 @@ private:
"void f(struct A *a) {\n"
" if (a->g() == nullptr) {}\n"
" *(a->g());\n"
"}\n");
"}");
ASSERT_EQUALS(
"[test.cpp:3] -> [test.cpp:4]: (warning) Either the condition 'a->g()==nullptr' is redundant or there is possible null pointer dereference: a->g().\n",
errout.str());
@ -1604,7 +1604,7 @@ private:
"void f(struct A *a) {\n"
" if (a->g() == nullptr) {}\n"
" *(a->g());\n"
"}\n");
"}");
ASSERT_EQUALS("", errout.str());
}
@ -1613,7 +1613,7 @@ private:
"void f(struct A *a) {\n"
" if (a->g() == nullptr) {}\n"
" *(a->g());\n"
"}\n");
"}");
ASSERT_EQUALS(
"[test.cpp:3] -> [test.cpp:4]: (warning) Either the condition 'a->g()==nullptr' is redundant or there is possible null pointer dereference: a->g().\n",
errout.str());
@ -1626,7 +1626,7 @@ private:
" if (x) {\n"
" (void)*a->x;\n"
" }\n"
"}\n");
"}");
ASSERT_EQUALS("", errout.str());
}
@ -1638,7 +1638,7 @@ private:
" if ( w == 0.0 )\n"
" return 0;\n"
" return b->get();\n"
"}\n");
"}");
ASSERT_EQUALS("", errout.str());
// #9423
check("extern F* GetF();\n"
@ -1655,7 +1655,7 @@ private:
" if (!lPtrOk)\n"
" return;\n"
" lPtr->Clear();\n"
"}\n");
"}");
ASSERT_EQUALS("", errout.str());
}
@ -1672,7 +1672,7 @@ private:
" g();\n"
" a d = *c->b();\n"
" return d;\n"
"}\n");
"}");
ASSERT_EQUALS("", errout.str());
check("struct a {\n"
@ -1688,7 +1688,7 @@ private:
" e();\n"
" a d = *c->b();\n"
" return d;\n"
"}\n");
"}");
ASSERT_EQUALS("", errout.str());
}
@ -1696,7 +1696,7 @@ private:
check("void f() {\n"
" char* p = new(std::nothrow) char[1];\n"
" if( p ) {}\n"
"}\n");
"}");
ASSERT_EQUALS("", errout.str());
}
@ -1705,7 +1705,7 @@ private:
" if(!p[0]) {}\n"
" const int *const a = p;\n"
" if(!a){}\n"
"}\n");
"}");
ASSERT_EQUALS("[test.cpp:4] -> [test.cpp:2]: (warning) Either the condition '!a' is redundant or there is possible null pointer dereference: p.\n", errout.str());
}
@ -1714,7 +1714,7 @@ private:
"auto f(T& x) -> decltype(x);\n"
"int& g(int* x) {\n"
" return f(*x);\n"
"}\n");
"}");
ASSERT_EQUALS("", errout.str());
}
@ -1724,7 +1724,7 @@ private:
" if(n > 10) q = p;\n"
" *p +=2;\n"
" if(n < 120) *q+=12;\n"
"}\n");
"}");
ASSERT_EQUALS("[test.cpp:5]: (warning) Possible null pointer dereference: q\n", errout.str());
check("void f(int *p, int n) {\n"
@ -1732,7 +1732,7 @@ private:
" if(n > 10) q = p;\n"
" *p +=2;\n"
" if(n > 10) *q+=12;\n"
"}\n");
"}");
ASSERT_EQUALS("", errout.str());
}
@ -1746,7 +1746,7 @@ private:
" }\n"
" }\n"
" }\n"
"}\n");
"}");
ASSERT_EQUALS(
"[test.cpp:2] -> [test.cpp:6]: (warning) Either the condition '!p' is redundant or there is possible null pointer dereference: p.\n",
errout.str());
@ -1762,7 +1762,7 @@ private:
" if (c(e, \"\"))\n"
" return nullptr;\n"
" return e->b();\n"
"}\n");
"}");
ASSERT_EQUALS("", errout.str());
}
@ -1772,7 +1772,7 @@ private:
" if(b) c = b;\n"
" if (!c) c = &a;\n"
" return *c;\n"
"}\n");
"}");
ASSERT_EQUALS("", errout.str());
check("int f(int a, int* b) {\n"
@ -1781,7 +1781,7 @@ private:
" bool d = !c;\n"
" if (d) c = &a;\n"
" return *c;\n"
"}\n");
"}");
ASSERT_EQUALS("", errout.str());
check("struct A { int* x; };\n"
@ -1791,7 +1791,7 @@ private:
" if(b) c.x = b;\n"
" if (!c.x) c.x = &a;\n"
" return *c.x;\n"
"}\n");
"}");
ASSERT_EQUALS("", errout.str());
check("struct A { int* x; };\n"
@ -1802,7 +1802,7 @@ private:
" bool d = !c.x;\n"
" if (d) c.x = &a;\n"
" return *c.x;\n"
"}\n");
"}");
ASSERT_EQUALS("", errout.str());
check("struct A { int* x; };\n"
@ -1825,7 +1825,7 @@ private:
"}\n"
"void bar() {\n"
" f(0, 0);\n"
"}\n");
"}");
ASSERT_EQUALS("[test.cpp:3]: (warning) Possible null pointer dereference: params\n", errout.str());
}
@ -1841,7 +1841,7 @@ private:
"int bar() {\n"
" int **array = NULL;\n"
" foo (array, 0);\n"
"}\n");
"}");
ASSERT_EQUALS("", errout.str());
}
@ -1851,7 +1851,7 @@ private:
" while (tok3->astParent() && tok3->str() == \",\")\n"
" tok3 = tok3->astParent();\n"
" if (tok3 && tok3->str() == \"(\") {}\n"
"}\n");
"}");
ASSERT_EQUALS(
"[test.cpp:5] -> [test.cpp:3]: (warning) Either the condition 'tok3' is redundant or there is possible null pointer dereference: tok3.\n",
errout.str());
@ -1864,7 +1864,7 @@ private:
" }\n"
" if (!t1 || !t2)\n"
" return;\n"
"}\n");
"}");
ASSERT_EQUALS("", errout.str());
check("bool f(int* i);\n"
@ -1872,7 +1872,7 @@ private:
" while(f(i) && *i == 0)\n"
" i++;\n"
" if (!i) {}\n"
"}\n");
"}");
ASSERT_EQUALS("", errout.str());
}
@ -1884,7 +1884,7 @@ private:
" ListEntry *prev = NULL;\n"
" for (ListEntry *cursor = listHead; cursor != NULL; prev = cursor, cursor = cursor->next) {}\n"
" if (prev) {}\n"
"}\n");
"}");
ASSERT_EQUALS("", errout.str());
}
@ -2908,7 +2908,7 @@ private:
"void f(int* x) {\n"
" if (x)\n"
" g(x);\n"
"}\n");
"}");
ASSERT_EQUALS("", errout.str());
}
@ -3134,42 +3134,42 @@ private:
"void f(std::shared_ptr<Fred> p) {\n"
" if (p) {}\n"
" dostuff(p->x);\n"
"}\n");
"}");
ASSERT_EQUALS("[test.cpp:3] -> [test.cpp:4]: (warning) Either the condition 'p' is redundant or there is possible null pointer dereference: p.\n", errout.str());
check("struct Fred { int x; };\n"
"void f(std::shared_ptr<Fred> p) {\n"
" p = nullptr;\n"
" dostuff(p->x);\n"
"}\n");
"}");
ASSERT_EQUALS("[test.cpp:4]: (error) Null pointer dereference: p\n", errout.str());
check("struct Fred { int x; };\n"
"void f(std::unique_ptr<Fred> p) {\n"
" if (p) {}\n"
" dostuff(p->x);\n"
"}\n");
"}");
ASSERT_EQUALS("[test.cpp:3] -> [test.cpp:4]: (warning) Either the condition 'p' is redundant or there is possible null pointer dereference: p.\n", errout.str());
check("struct Fred { int x; };\n"
"void f(std::unique_ptr<Fred> p) {\n"
" p = nullptr;\n"
" dostuff(p->x);\n"
"}\n");
"}");
ASSERT_EQUALS("[test.cpp:4]: (error) Null pointer dereference: p\n", errout.str());
check("struct Fred { int x; };\n"
"void f() {\n"
" std::shared_ptr<Fred> p;\n"
" dostuff(p->x);\n"
"}\n");
"}");
ASSERT_EQUALS("[test.cpp:4]: (error) Null pointer dereference: p\n", errout.str());
check("struct Fred { int x; };\n"
"void f(std::shared_ptr<Fred> p) {\n"
" p.reset();\n"
" dostuff(p->x);\n"
"}\n");
"}");
ASSERT_EQUALS("[test.cpp:4]: (error) Null pointer dereference: p\n", errout.str());
check("struct Fred { int x; };\n"
@ -3177,7 +3177,7 @@ private:
" Fred * pp = nullptr;\n"
" p.reset(pp);\n"
" dostuff(p->x);\n"
"}\n");
"}");
ASSERT_EQUALS("[test.cpp:5]: (error) Null pointer dereference: p\n", errout.str());
check("struct Fred { int x; };\n"
@ -3185,28 +3185,28 @@ private:
" std::shared_ptr<Fred> p;\n"
" p.reset(&f);\n"
" dostuff(p->x);\n"
"}\n");
"}");
ASSERT_EQUALS("", errout.str());
check("struct Fred { int x; };\n"
"void f(std::shared_ptr<Fred> p) {\n"
" p.release();\n"
" dostuff(p->x);\n"
"}\n");
"}");
ASSERT_EQUALS("[test.cpp:4]: (error) Null pointer dereference: p\n", errout.str());
check("struct Fred { int x; };\n"
"void f() {\n"
" std::shared_ptr<Fred> p(nullptr);\n"
" dostuff(p->x);\n"
"}\n");
"}");
ASSERT_EQUALS("[test.cpp:4]: (error) Null pointer dereference: p\n", errout.str());
check("struct A {};\n"
"void f(int n) {\n"
" std::unique_ptr<const A*[]> p;\n"
" p.reset(new const A*[n]);\n"
"}\n");
"}");
ASSERT_EQUALS("", errout.str());
// #9216
@ -3217,7 +3217,7 @@ private:
"void g(std::unique_ptr<A> var) {\n"
" var->reset();\n"
" var->f();\n"
"}\n");
"}");
ASSERT_EQUALS("", errout.str());
// #9439
@ -3560,7 +3560,7 @@ private:
" unsigned int j;\n"
" for (j = 0; j < b[0].a->size; ++j) {\n"
" }\n"
"}\n");
"}");
ASSERT_EQUALS("", errout.str());
}
@ -3584,27 +3584,27 @@ private:
check("void foo(char *s) {\n"
" p = s - 20;\n"
"}\n"
"void bar() { foo(0); }\n");
"void bar() { foo(0); }");
ASSERT_EQUALS("[test.cpp:2]: (error) Overflow in pointer arithmetic, NULL pointer is subtracted.\n",
errout.str());
check("void foo(char *s) {\n"
" if (!s) {}\n"
" p = s - 20;\n"
"}\n");
"}");
ASSERT_EQUALS("[test.cpp:2] -> [test.cpp:3]: (warning) Either the condition '!s' is redundant or there is overflow in pointer subtraction.\n", errout.str());
check("void foo(char *s) {\n"
" s -= 20;\n"
"}\n"
"void bar() { foo(0); }\n");
"void bar() { foo(0); }");
ASSERT_EQUALS("[test.cpp:2]: (error) Overflow in pointer arithmetic, NULL pointer is subtracted.\n",
errout.str());
check("void foo(char *s) {\n"
" if (!s) {}\n"
" s -= 20;\n"
"}\n");
"}");
ASSERT_EQUALS("[test.cpp:2] -> [test.cpp:3]: (warning) Either the condition '!s' is redundant or there is overflow in pointer subtraction.\n", errout.str());
check("int* f8() { int *x = NULL; return --x; }");
@ -3618,37 +3618,37 @@ private:
check("void foo(char *s) {\n"
" char * p = s + 20;\n"
"}\n"
"void bar() { foo(0); }\n");
"void bar() { foo(0); }");
ASSERT_EQUALS("[test.cpp:2]: (error) Pointer addition with NULL pointer.\n", errout.str());
check("void foo(char *s) {\n"
" if (!s) {}\n"
" char * p = s + 20;\n"
"}\n");
"}");
ASSERT_EQUALS("[test.cpp:2] -> [test.cpp:3]: (warning) Either the condition '!s' is redundant or there is pointer arithmetic with NULL pointer.\n", errout.str());
check("void foo(char *s) {\n"
" char * p = 20 + s;\n"
"}\n"
"void bar() { foo(0); }\n");
"void bar() { foo(0); }");
ASSERT_EQUALS("[test.cpp:2]: (error) Pointer addition with NULL pointer.\n", errout.str());
check("void foo(char *s) {\n"
" if (!s) {}\n"
" char * p = 20 + s;\n"
"}\n");
"}");
ASSERT_EQUALS("[test.cpp:2] -> [test.cpp:3]: (warning) Either the condition '!s' is redundant or there is pointer arithmetic with NULL pointer.\n", errout.str());
check("void foo(char *s) {\n"
" s += 20;\n"
"}\n"
"void bar() { foo(0); }\n");
"void bar() { foo(0); }");
ASSERT_EQUALS("[test.cpp:2]: (error) Pointer addition with NULL pointer.\n", errout.str());
check("void foo(char *s) {\n"
" if (!s) {}\n"
" s += 20;\n"
"}\n");
"}");
ASSERT_EQUALS("[test.cpp:2] -> [test.cpp:3]: (warning) Either the condition '!s' is redundant or there is pointer arithmetic with NULL pointer.\n", errout.str());
check("int* f7() { int *x = NULL; return ++x; }");
@ -3659,7 +3659,7 @@ private:
check("class foo {};\n"
"const char* get() const { return 0; }\n"
"void f(foo x) { if (get()) x += get(); }\n");
"void f(foo x) { if (get()) x += get(); }");
ASSERT_EQUALS("", errout.str());
}
@ -3763,7 +3763,7 @@ private:
"void f(int* x) {\n"
" if (x)\n"
" g(x);\n"
"}\n");
"}");
ASSERT_EQUALS("", errout.str());
}
};

File diff suppressed because it is too large Load Diff

View File

@ -329,7 +329,7 @@ private:
"\n"
"void f() {\n"
" p++;\n"
"}\n");
"}");
ASSERT_EQUALS("", errout.str());
}

View File

@ -4563,11 +4563,11 @@ private:
void syntax_error_templates_1() {
// ok code.. using ">" for a comparison
tok("x<y>z> xyz;\n");
tok("x<y>z> xyz;");
ASSERT_EQUALS("", errout.str());
// ok code
tok("template<class T> operator<(T a, T b) { }\n");
tok("template<class T> operator<(T a, T b) { }");
ASSERT_EQUALS("", errout.str());
// ok code (ticket #1984)
@ -4604,7 +4604,7 @@ private:
" }\n"
"private:\n"
" A a;\n"
"};\n");
"};");
ASSERT_EQUALS("", errout.str());
//both of these should work but in cppcheck 2.1 only the first option will work (ticket #9843)

View File

@ -223,7 +223,7 @@ private:
" Y = sizeof(f(g() << h())) == sizeof(A),\n"
" Z = X & Y\n"
" };\n"
"};\n");
"};");
ASSERT_EQUALS("", errout.str());
}
@ -231,8 +231,7 @@ private:
check("void f() {\n"
" int a[10];\n"
" std::cout << sizeof(a) / sizeof(int) << std::endl;\n"
"}\n"
);
"}");
ASSERT_EQUALS("", errout.str());
check("void f() {\n"
@ -240,8 +239,7 @@ private:
" unsigned int b = 2;\n"
" int c[(a+b)];\n"
" std::cout << sizeof(c) / sizeof(int) << std::endl;\n"
"}\n"
);
"}");
ASSERT_EQUALS("", errout.str());
check("void f() {\n"
@ -249,8 +247,7 @@ private:
" unsigned int b[] = { 0 };\n"
" int c[a[b[0]]];\n"
" std::cout << sizeof(c) / sizeof(int) << std::endl;\n"
"}\n"
);
"}");
ASSERT_EQUALS("", errout.str());
@ -259,42 +256,36 @@ private:
" unsigned int b = 2;\n"
" int c[(a[0]+b)];\n"
" std::cout << sizeof(c) / sizeof(int) << std::endl;\n"
"}\n"
);
"}");
ASSERT_EQUALS("", errout.str());
check("void f() {\n"
" int a[] = { 1, 2, 3 };\n"
" std::cout << sizeof(a) / sizeof(int) << std::endl;\n"
"}\n"
);
"}");
ASSERT_EQUALS("", errout.str());
check("void f() {\n"
" int a[3] = { 1, 2, 3 };\n"
" std::cout << sizeof(a) / sizeof(int) << std::endl;\n"
"}\n"
);
"}");
ASSERT_EQUALS("", errout.str());
check("void f( int a[]) {\n"
" std::cout << sizeof(a) / sizeof(int) << std::endl;\n"
"}\n"
);
"}");
ASSERT_EQUALS("[test.cpp:2]: (warning) Using 'sizeof' on array given as "
"function argument returns size of a pointer.\n", errout.str());
check("void f( int a[]) {\n"
" std::cout << sizeof a / sizeof(int) << std::endl;\n"
"}\n"
);
"}");
ASSERT_EQUALS("[test.cpp:2]: (warning) Using 'sizeof' on array given as "
"function argument returns size of a pointer.\n", errout.str());
check("void f( int a[3] ) {\n"
" std::cout << sizeof(a) / sizeof(int) << std::endl;\n"
"}\n"
);
"}");
ASSERT_EQUALS("[test.cpp:2]: (warning) Using 'sizeof' on array given as "
"function argument returns size of a pointer.\n", errout.str());
@ -302,22 +293,19 @@ private:
"int f2(Fixname& f2v) {\n"
" int i = sizeof(f2v);\n"
" printf(\"sizeof f2v %d\", i);\n"
" }\n"
);
" }");
ASSERT_EQUALS("", errout.str());
check("void f(int *p) {\n"
" p[0] = 0;\n"
" int unused = sizeof(p);\n"
"}\n"
);
"}");
ASSERT_EQUALS("", errout.str());
check("void f() {\n"
" char p[] = \"test\";\n"
" int unused = sizeof(p);\n"
"}\n"
);
"}");
ASSERT_EQUALS("", errout.str());
// ticket #2495
@ -331,38 +319,33 @@ private:
" {1,0,1},\n"
" };\n"
" const int COL_MAX=sizeof(col)/sizeof(col[0]);\n"
"}\n"
);
"}");
ASSERT_EQUALS("", errout.str());
// ticket #155
check("void f() {\n"
" char buff1[1024*64],buff2[sizeof(buff1)*2];\n"
"}\n"
);
"}");
ASSERT_EQUALS("", errout.str());
// ticket #2510
check("void f( int a[], int b) {\n"
" std::cout << sizeof(a) / sizeof(int) << std::endl;\n"
"}\n"
);
"}");
ASSERT_EQUALS("[test.cpp:2]: (warning) Using 'sizeof' on array given as "
"function argument returns size of a pointer.\n", errout.str());
// ticket #2510
check("void f( int a[3] , int b[2] ) {\n"
" std::cout << sizeof(a) / sizeof(int) << std::endl;\n"
"}\n"
);
"}");
ASSERT_EQUALS("[test.cpp:2]: (warning) Using 'sizeof' on array given as "
"function argument returns size of a pointer.\n", errout.str());
// ticket #2510
check("void f() {\n"
" char buff1[1024*64],buff2[sizeof(buff1)*(2+1)];\n"
"}\n"
);
"}");
ASSERT_EQUALS("", errout.str());
}
@ -370,22 +353,22 @@ private:
void sizeofForNumericParameter() {
check("void f() {\n"
" std::cout << sizeof(10) << std::endl;\n"
"}\n");
"}");
ASSERT_EQUALS("[test.cpp:2]: (warning) Suspicious usage of 'sizeof' with a numeric constant as parameter.\n", errout.str());
check("void f() {\n"
" std::cout << sizeof(-10) << std::endl;\n"
"}\n");
"}");
ASSERT_EQUALS("[test.cpp:2]: (warning) Suspicious usage of 'sizeof' with a numeric constant as parameter.\n", errout.str());
check("void f() {\n"
" std::cout << sizeof 10 << std::endl;\n"
"}\n");
"}");
ASSERT_EQUALS("[test.cpp:2]: (warning) Suspicious usage of 'sizeof' with a numeric constant as parameter.\n", errout.str());
check("void f() {\n"
" std::cout << sizeof -10 << std::endl;\n"
"}\n");
"}");
ASSERT_EQUALS("[test.cpp:2]: (warning) Suspicious usage of 'sizeof' with a numeric constant as parameter.\n", errout.str());
}
@ -850,7 +833,7 @@ private:
" char x = *((char*)(foo.data+1));\n"
" foo.data++;\n"
" return x;\n"
"}\n");
"}");
ASSERT_EQUALS("[test.cpp:5]: (portability) 'foo.data' is of type 'void *'. When using void pointers in calculations, the behaviour is undefined.\n"
"[test.cpp:6]: (portability) 'foo.data' is of type 'void *'. When using void pointers in calculations, the behaviour is undefined.\n", errout.str());
@ -874,7 +857,7 @@ private:
"};\n"
"void f4(struct BOO* boo) {\n"
" char c = *((char*)boo->data.data + 1);\n"
"}\n");
"}");
ASSERT_EQUALS("", errout.str());
check("struct FOO {\n"
@ -882,7 +865,7 @@ private:
"};\n"
"char f(struct FOO* foo) {\n"
" *(foo[1].data + 1) = 0;\n"
"}\n");
"}");
ASSERT_EQUALS("[test.cpp:5]: (portability) 'foo[1].data' is of type 'void *'. When using void pointers in calculations, the behaviour is undefined.\n", errout.str());
check("struct FOO {\n"
@ -895,7 +878,7 @@ private:
// #6050 arithmetic on void**
check("void* array[10];\n"
"void** b = array + 3;\n");
"void** b = array + 3;");
ASSERT_EQUALS("", errout.str());
}
@ -904,7 +887,7 @@ private:
check("char strncat ();\n"
"int main () {\n"
" return strncat ();\n"
"}\n");
"}");
}
};

View File

@ -225,7 +225,7 @@ private:
checkNormal("void f(std::vector<int> v) {\n"
" v.front();\n"
" if (v.empty()) {}\n"
"}\n");
"}");
ASSERT_EQUALS("test.cpp:2:warning:Either the condition 'v.empty()' is redundant or expression 'v.front()' cause access out of bounds.\n"
"test.cpp:3:note:condition 'v.empty()'\n"
"test.cpp:2:note:Access out of bounds\n", errout.str());
@ -233,7 +233,7 @@ private:
checkNormal("void f(std::vector<int> v) {\n"
" if (v.size() == 3) {}\n"
" v[16] = 0;\n"
"}\n");
"}");
ASSERT_EQUALS("test.cpp:3:warning:Either the condition 'v.size()==3' is redundant or v size can be 3. Expression 'v[16]' cause access out of bounds.\n"
"test.cpp:2:note:condition 'v.size()==3'\n"
"test.cpp:3:note:Access out of bounds\n", errout.str());
@ -243,7 +243,7 @@ private:
" if (v.size() == 3) {\n"
" v[i] = 0;\n"
" }\n"
"}\n");
"}");
ASSERT_EQUALS("test.cpp:4:warning:Either the condition 'v.size()==3' is redundant or v size can be 3. Expression 'v[i]' cause access out of bounds.\n"
"test.cpp:3:note:condition 'v.size()==3'\n"
"test.cpp:4:note:Access out of bounds\n", errout.str());
@ -251,19 +251,19 @@ private:
checkNormal("void f(std::vector<int> v, int i) {\n"
" if (v.size() == 3 || i == 16) {}\n"
" v[i] = 0;\n"
"}\n");
"}");
ASSERT_EQUALS("", errout.str());
checkNormal("void f(std::map<int,int> x) {\n"
" if (x.empty()) { x[1] = 2; }\n"
"}\n");
"}");
ASSERT_EQUALS("", errout.str());
checkNormal("void f(std::string s) {\n"
" if (s.size() == 1) {\n"
" s[2] = 0;\n"
" }\n"
"}\n");
"}");
ASSERT_EQUALS("test.cpp:3:warning:Either the condition 's.size()==1' is redundant or s size can be 1. Expression 's[2]' cause access out of bounds.\n"
"test.cpp:2:note:condition 's.size()==1'\n"
"test.cpp:3:note:Access out of bounds\n", errout.str());
@ -273,47 +273,47 @@ private:
" std::string b[];\n"
" for (auto c : b)\n"
" c.data();\n"
"}\n");
"}");
ASSERT_EQUALS("", errout.str());
checkNormal("std::string f(std::string x) {\n"
" if (x.empty()) return {};\n"
" x[0];\n"
"}\n");
"}");
ASSERT_EQUALS("", errout.str());
checkNormal("std::string f(std::string x) {\n"
" if (x.empty()) return std::string{};\n"
" x[0];\n"
"}\n");
"}");
ASSERT_EQUALS("", errout.str());
checkNormal("void f() {\n"
" std::string s;\n"
" x = s.begin() + 1;\n"
"}\n");
"}");
ASSERT_EQUALS("test.cpp:3:error:Out of bounds access in expression 's.begin()+1' because 's' is empty.\n", errout.str());
checkNormal("void f(int x) {\n"
" std::string s;\n"
" x = s.begin() + x;\n"
"}\n");
"}");
ASSERT_EQUALS("test.cpp:3:error:Out of bounds access in expression 's.begin()+x' because 's' is empty and 'x' may be non-zero.\n", errout.str());
checkNormal("char fstr1(){const std::string s = \"<a><b>\"; return s[42]; }\n"
"wchar_t fwstr1(){const std::wstring s = L\"<a><b>\"; return s[42]; }\n");
"wchar_t fwstr1(){const std::wstring s = L\"<a><b>\"; return s[42]; }");
ASSERT_EQUALS("test.cpp:1:error:Out of bounds access in 's[42]', if 's' size is 6 and '42' is 42\n"
"test.cpp:2:error:Out of bounds access in 's[42]', if 's' size is 6 and '42' is 42\n", errout.str());
checkNormal("char fstr1(){const std::string s = \"<a><b>\"; return s[1]; }\n"
"wchar_t fwstr1(){const std::wstring s = L\"<a><b>\"; return s[1]; }\n");
"wchar_t fwstr1(){const std::wstring s = L\"<a><b>\"; return s[1]; }");
ASSERT_EQUALS("", errout.str());
checkNormal("int f() {\n"
" std::vector<int> v;\n"
" std::vector<int> * pv = &v;\n"
" return (*pv)[42];\n"
"}\n");
"}");
ASSERT_EQUALS("test.cpp:4:error:Out of bounds access in expression '(*pv)[42]' because '*pv' is empty.\n", errout.str());
checkNormal("void f() {\n"
@ -328,14 +328,14 @@ private:
" int x = 6;\n"
" if(b) ++x;\n"
" return s[x];\n"
"}\n");
"}");
ASSERT_EQUALS("test.cpp:5:error:Out of bounds access in 's[x]', if 's' size is 6 and 'x' is 6\n", errout.str());
checkNormal("void f() {\n"
" static const int N = 4;\n"
" std::array<int, N> x;\n"
" x[0] = 0;\n"
"}\n");
"}");
ASSERT_EQUALS("", errout.str());
checkNormal("void f(bool b) {\n"
@ -1165,7 +1165,7 @@ private:
"};\n"
"bool A::B::operator==(const A::B& b) const {\n"
" return std::tie(x, y, z) == std::tie(b.x, b.y, b.z);\n"
"}\n");
"}");
ASSERT_EQUALS("", errout.str());
}
@ -1173,29 +1173,29 @@ private:
// #9556
check("void f(int a, int b) {\n"
" if (&a == &b) {}\n"
"}\n");
"}");
ASSERT_EQUALS("", errout.str());
check("void f(int a, int b) {\n"
" if (std::for_each(&a, &b + 1, [](auto) {})) {}\n"
"}\n");
"}");
ASSERT_EQUALS("[test.cpp:2]: (error) Iterators of different containers 'a' and 'b' are used together.\n",
errout.str());
check("void f(int a, int b) {\n"
" if (std::for_each(&a, &b, [](auto) {})) {}\n"
"}\n");
"}");
ASSERT_EQUALS("[test.cpp:2]: (error) Iterators of different containers 'a' and 'b' are used together.\n",
errout.str());
check("void f(int a) {\n"
" if (std::for_each(&a, &a, [](auto) {})) {}\n"
"}\n");
"}");
ASSERT_EQUALS("[test.cpp:2]: (style) Same iterators expression are used for algorithm.\n", errout.str());
check("void f(int a) {\n"
" if (std::for_each(&a, &a + 1, [](auto) {})) {}\n"
"}\n");
"}");
ASSERT_EQUALS("", errout.str());
}
@ -1208,7 +1208,7 @@ private:
" bool operator()(const S& lhs, const S& rhs) const {\n"
" return &lhs.v != &rhs.v;\n"
" }\n"
"};\n");
"};");
ASSERT_EQUALS("", errout.str());
}
@ -1217,21 +1217,21 @@ private:
"std::vector<int>& g();\n"
"void foo() {\n"
" (void)std::find(f().begin(), g().end(), 0);\n"
"}\n");
"}");
ASSERT_EQUALS("[test.cpp:4]: (warning) Iterators to containers from different expressions 'f()' and 'g()' are used together.\n", errout.str());
check("std::vector<int>& f();\n"
"std::vector<int>& g();\n"
"void foo() {\n"
" if(f().begin() == g().end()) {}\n"
"}\n");
"}");
ASSERT_EQUALS("[test.cpp:4]: (warning) Iterators to containers from different expressions 'f()' and 'g()' are used together.\n", errout.str());
check("std::vector<int>& f();\n"
"std::vector<int>& g();\n"
"void foo() {\n"
" auto size = f().end() - g().begin();\n"
"}\n");
"}");
ASSERT_EQUALS("[test.cpp:4]: (warning) Iterators to containers from different expressions 'f()' and 'g()' are used together.\n", errout.str());
check("struct A {\n"
@ -1240,7 +1240,7 @@ private:
"};\n"
"void foo() {\n"
" (void)std::find(A().f().begin(), A().g().end(), 0);\n"
"}\n");
"}");
ASSERT_EQUALS("[test.cpp:6]: (warning) Iterators to containers from different expressions 'A().f()' and 'A().g()' are used together.\n", errout.str());
check("struct A {\n"
@ -1249,14 +1249,14 @@ private:
"};\n"
"void foo() {\n"
" (void)std::find(A{} .f().begin(), A{} .g().end(), 0);\n"
"}\n");
"}");
ASSERT_EQUALS("[test.cpp:6]: (warning) Iterators to containers from different expressions 'A{}.f()' and 'A{}.g()' are used together.\n", errout.str());
check("std::vector<int>& f();\n"
"std::vector<int>& g();\n"
"void foo() {\n"
" (void)std::find(begin(f()), end(g()), 0);\n"
"}\n");
"}");
ASSERT_EQUALS("[test.cpp:4]: (warning) Iterators to containers from different expressions 'f()' and 'g()' are used together.\n", errout.str());
check("struct A {\n"
@ -1265,14 +1265,14 @@ private:
"};\n"
"void foo() {\n"
" (void)std::find(A().f().begin(), A().f().end(), 0);\n"
"}\n");
"}");
ASSERT_EQUALS("", errout.str());
check("std::vector<int>& f();\n"
"std::vector<int>& g();\n"
"void foo() {\n"
" if(bar(f().begin()) == g().end()) {}\n"
"}\n");
"}");
ASSERT_EQUALS("", errout.str());
check("std::vector<int>& f();\n"
@ -1297,7 +1297,7 @@ private:
" (void)std::find(begin(f()) + 1, end(f()), 0);\n"
" (void)std::find(begin(f()), end(f()) - 1, 0);\n"
" (void)std::find(begin(f()) + 1, end(f()) - 1, 0);\n"
"}\n");
"}");
ASSERT_EQUALS("", errout.str());
check("std::vector<int>& f();\n"
@ -1307,65 +1307,65 @@ private:
" if(f().begin() == f().end()+1) {}\n"
" if(f().begin()+1 == f().end()) {}\n"
" if(f().begin()+1 == f().end()+1) {}\n"
"}\n");
"}");
ASSERT_EQUALS("", errout.str());
check("template<int N>\n"
"std::vector<int>& f();\n"
"void foo() {\n"
" if(f<1>().begin() == f<1>().end()) {}\n"
"}\n");
"}");
ASSERT_EQUALS("", errout.str());
check("void f() {\n"
" if (a.begin().x == b.begin().x) {}\n"
" if (begin(a).x == begin(b).x) {}\n"
"}\n");
"}");
ASSERT_EQUALS("", errout.str());
check("void f(std::list<int*> a, std::list<int*> b) {\n"
" if (*a.begin() == *b.begin()) {}\n"
"}\n");
"}");
ASSERT_EQUALS("", errout.str());
check("void foo() {\n"
" if(f().begin(1) == f().end()) {}\n"
"}\n");
"}");
ASSERT_EQUALS("", errout.str());
}
void iteratorSameExpression() {
check("void f(std::vector<int> v) {\n"
" std::for_each(v.begin(), v.begin(), [](int){});\n"
"}\n");
"}");
ASSERT_EQUALS("[test.cpp:2]: (style) Same iterators expression are used for algorithm.\n", errout.str());
check("std::vector<int>& g();\n"
"void f() {\n"
" std::for_each(g().begin(), g().begin(), [](int){});\n"
"}\n");
"}");
ASSERT_EQUALS("[test.cpp:3]: (style) Same iterators expression are used for algorithm.\n", errout.str());
check("void f(std::vector<int> v) {\n"
" std::for_each(v.end(), v.end(), [](int){});\n"
"}\n");
"}");
ASSERT_EQUALS("[test.cpp:2]: (style) Same iterators expression are used for algorithm.\n", errout.str());
check("std::vector<int>& g();\n"
"void f() {\n"
" std::for_each(g().end(), g().end(), [](int){});\n"
"}\n");
"}");
ASSERT_EQUALS("[test.cpp:3]: (style) Same iterators expression are used for algorithm.\n", errout.str());
check("std::vector<int>::iterator g();\n"
"void f(std::vector<int> v) {\n"
" std::for_each(g(), g(), [](int){});\n"
"}\n");
"}");
ASSERT_EQUALS("[test.cpp:3]: (style) Same iterators expression are used for algorithm.\n", errout.str());
check("void f(std::vector<int>::iterator it) {\n"
" std::for_each(it, it, [](int){});\n"
"}\n");
"}");
ASSERT_EQUALS("[test.cpp:2]: (style) Same iterators expression are used for algorithm.\n", errout.str());
}
@ -1374,13 +1374,13 @@ private:
" std::vector<int> a, b;\n"
" a.insert(b.end(), value);\n"
" return a;\n"
"}\n");
"}");
ASSERT_EQUALS("[test.cpp:3]: (error) Iterator 'b.end()' from different container 'a' are used together.\n", errout.str());
check("std::vector<int> f(std::vector<int> a, std::vector<int> b) {\n"
" a.erase(b.begin());\n"
" return a;\n"
"}\n");
"}");
ASSERT_EQUALS("[test.cpp:2]: (error) Iterator 'b.begin()' from different container 'a' are used together.\n", errout.str());
// #9973
@ -1643,7 +1643,7 @@ private:
" return a[x - 5];\n"
" else\n"
" return a[4 - x];\n"
"}\n");
"}");
ASSERT_EQUALS("", errout.str());
}
@ -3505,7 +3505,7 @@ private:
" EffectivityRange<int> er;\n"
" }\n"
" void shift() { EffectivityRangeData<int>::iterator it; }\n"
"};\n");
"};");
ASSERT_EQUALS("", errout.str());
}
@ -3515,7 +3515,7 @@ private:
" if (std::isalpha(*i) && i != str.end()) {\n"
" std::cout << *i;\n"
" }\n"
"}\n");
"}");
ASSERT_EQUALS("[test.cpp:2]: (warning) Possible dereference of an invalid iterator: i\n", errout.str());
check("void foo(std::string::iterator& i) {\n"
@ -3523,7 +3523,7 @@ private:
" else if (std::isalpha(*i) && i != str.end()) {\n"
" std::cout << *i;\n"
" }\n"
"}\n");
"}");
ASSERT_EQUALS("[test.cpp:3]: (warning) Possible dereference of an invalid iterator: i\n", errout.str());
// Test suggested correction doesn't report an error
@ -3531,7 +3531,7 @@ private:
" if (i != str.end() && std::isalpha(*i)) {\n"
" std::cout << *i;\n"
" }\n"
"}\n");
"}");
ASSERT_EQUALS("", errout.str());
// Test "while" with "&&" case
@ -3540,7 +3540,7 @@ private:
" std::cout << *i;\n"
" i ++;\n"
" }\n"
"}\n");
"}");
ASSERT_EQUALS("[test.cpp:2]: (warning) Possible dereference of an invalid iterator: i\n", errout.str());
check("void foo(std::string::iterator& i) {\n"
@ -3548,7 +3548,7 @@ private:
" std::cout << *i;\n"
" i ++;\n"
" } while (std::isalpha(*i) && i != str.end());\n"
"}\n");
"}");
ASSERT_EQUALS("[test.cpp:5]: (warning) Possible dereference of an invalid iterator: i\n", errout.str());
// Test "while" with "||" case
@ -3557,7 +3557,7 @@ private:
" std::cout << *i;\n"
" i ++;\n"
" }\n"
"}\n");
"}");
ASSERT_EQUALS("[test.cpp:2]: (warning) Possible dereference of an invalid iterator: i\n", errout.str());
// Test fix for "while" with "||" case
@ -3566,7 +3566,7 @@ private:
" std::cout << *i;\n"
" i ++;\n"
" }\n"
"}\n");
"}");
ASSERT_EQUALS("", errout.str());
// Test "for" with "&&" case
@ -3575,7 +3575,7 @@ private:
" std::cout << *i;\n"
" i ++;\n"
" }\n"
"}\n");
"}");
ASSERT_EQUALS("[test.cpp:2]: (warning) Possible dereference of an invalid iterator: i\n", errout.str());
// Test "for" with "||" case
@ -3584,7 +3584,7 @@ private:
" std::cout << *i;\n"
" i ++;\n"
" }\n"
"}\n");
"}");
ASSERT_EQUALS("[test.cpp:2]: (warning) Possible dereference of an invalid iterator: i\n", errout.str());
// Test that a dereference outside the condition part of a "for"
@ -3594,7 +3594,7 @@ private:
" std::cout << c;\n"
" i ++;\n"
" }\n"
"}\n");
"}");
ASSERT_EQUALS("", errout.str());
// Test that other "&&" terms in the condition don't invalidate the check
@ -3602,7 +3602,7 @@ private:
" if (*c && std::isalpha(*i) && i != str.end()) {\n"
" std::cout << *i;\n"
" }\n"
"}\n");
"}");
ASSERT_EQUALS("[test.cpp:2]: (warning) Possible dereference of an invalid iterator: i\n", errout.str());
// Test that dereference of different variable doesn't trigger a false positive
@ -3610,7 +3610,7 @@ private:
" if (std::isalpha(*c) && i != str.end()) {\n"
" std::cout << *c;\n"
" }\n"
"}\n");
"}");
ASSERT_EQUALS("", errout.str());
// Test case involving "rend()" instead of "end()"
@ -3619,7 +3619,7 @@ private:
" std::cout << *i;\n"
" i ++;\n"
" }\n"
"}\n");
"}");
ASSERT_EQUALS("[test.cpp:2]: (warning) Possible dereference of an invalid iterator: i\n", errout.str());
// Test that mixed "&&" and "||" don't result in a false positive
@ -3627,7 +3627,7 @@ private:
" if ((i == str.end() || *i) || (isFoo() && i != str.end())) {\n"
" std::cout << \"foo\";\n"
" }\n"
"}\n");
"}");
ASSERT_EQUALS("", errout.str());
check("void f() {\n"

View File

@ -922,7 +922,7 @@ private:
void VariableValueType1() {
GET_SYMBOL_DB("typedef uint8_t u8;\n"
"static u8 x;\n");
"static u8 x;");
const Variable* x = db->getVariableFromVarId(1);
ASSERT_EQUALS("x", x->name());
ASSERT(x->valueType()->isIntegral());
@ -930,7 +930,7 @@ private:
void VariableValueType2() {
GET_SYMBOL_DB("using u8 = uint8_t;\n"
"static u8 x;\n");
"static u8 x;");
const Variable* x = db->getVariableFromVarId(1);
ASSERT_EQUALS("x", x->name());
ASSERT(x->valueType()->isIntegral());
@ -939,7 +939,7 @@ private:
void VariableValueType3() {
// std::string::size_type
{
GET_SYMBOL_DB("void f(std::string::size_type x);\n");
GET_SYMBOL_DB("void f(std::string::size_type x);");
const Variable* const x = db->getVariableFromVarId(1);
ASSERT_EQUALS("x", x->name());
// TODO: Configure std::string::size_type somehow.
@ -948,7 +948,7 @@ private:
}
// std::wstring::size_type
{
GET_SYMBOL_DB("void f(std::wstring::size_type x);\n");
GET_SYMBOL_DB("void f(std::wstring::size_type x);");
const Variable* const x = db->getVariableFromVarId(1);
ASSERT_EQUALS("x", x->name());
// TODO: Configure std::wstring::size_type somehow.
@ -957,7 +957,7 @@ private:
}
// std::u16string::size_type
{
GET_SYMBOL_DB("void f(std::u16string::size_type x);\n");
GET_SYMBOL_DB("void f(std::u16string::size_type x);");
const Variable* const x = db->getVariableFromVarId(1);
ASSERT_EQUALS("x", x->name());
// TODO: Configure std::u16string::size_type somehow.
@ -966,7 +966,7 @@ private:
}
// std::u32string::size_type
{
GET_SYMBOL_DB("void f(std::u32string::size_type x);\n");
GET_SYMBOL_DB("void f(std::u32string::size_type x);");
const Variable* const x = db->getVariableFromVarId(1);
ASSERT_EQUALS("x", x->name());
// TODO: Configure std::u32string::size_type somehow.
@ -988,7 +988,7 @@ private:
void VariableValueType5() {
GET_SYMBOL_DB("class C {};\n"
"void foo(std::shared_ptr<C>* p) {}\n");
"void foo(std::shared_ptr<C>* p) {}");
const Variable* const p = db->getVariableFromVarId(1);
ASSERT(p->valueType());
@ -1294,7 +1294,7 @@ private:
void variableVolatile() {
GET_SYMBOL_DB("std::atomic<int> x;\n"
"volatile int y;\n");
"volatile int y;");
const Token *x = Token::findsimplematch(tokenizer.tokens(), "x");
ASSERT(x);
@ -1395,7 +1395,7 @@ private:
void getVariableFromVarIdBoundsCheck() {
GET_SYMBOL_DB("int x;\n"
"int y;\n");
"int y;");
const Variable* v = db->getVariableFromVarId(2);
// three elements: varId 0 also counts via a fake-entry
@ -1405,7 +1405,7 @@ private:
}
void hasRegularFunction() {
GET_SYMBOL_DB("void func() { }\n");
GET_SYMBOL_DB("void func() { }");
// 2 scopes: Global and Function
ASSERT(db && db->scopeList.size() == 2);
@ -1447,7 +1447,7 @@ private:
}
void hasInlineClassFunction() {
GET_SYMBOL_DB("class Fred { void func() { } };\n");
GET_SYMBOL_DB("class Fred { void func() { } };");
// 3 scopes: Global, Class, and Function
ASSERT(db && db->scopeList.size() == 3);
@ -1498,7 +1498,7 @@ private:
}
void hasMissingInlineClassFunction() {
GET_SYMBOL_DB("class Fred { void func(); };\n");
GET_SYMBOL_DB("class Fred { void func(); };");
// 2 scopes: Global and Class (no Function scope because there is no function implementation)
ASSERT(db && db->scopeList.size() == 2);
@ -1542,7 +1542,7 @@ private:
}
void hasClassFunction() {
GET_SYMBOL_DB("class Fred { void func(); }; void Fred::func() { }\n");
GET_SYMBOL_DB("class Fred { void func(); }; void Fred::func() { }");
// 3 scopes: Global, Class, and Function
ASSERT(db && db->scopeList.size() == 3);
@ -1564,7 +1564,7 @@ private:
}
void hasClassFunction_trailingReturnType() {
GET_SYMBOL_DB("class Fred { auto func() -> int; }; auto Fred::func() -> int { }\n");
GET_SYMBOL_DB("class Fred { auto func() -> int; }; auto Fred::func() -> int { }");
// 3 scopes: Global, Class, and Function
ASSERT(db && db->scopeList.size() == 3);
@ -1586,7 +1586,7 @@ private:
}
void hasRegularFunctionReturningFunctionPointer() {
GET_SYMBOL_DB("void (*func(int f))(char) { }\n");
GET_SYMBOL_DB("void (*func(int f))(char) { }");
// 2 scopes: Global and Function
ASSERT(db && db->scopeList.size() == 2);
@ -1605,7 +1605,7 @@ private:
}
void hasInlineClassFunctionReturningFunctionPointer() {
GET_SYMBOL_DB("class Fred { void (*func(int f))(char) { } };\n");
GET_SYMBOL_DB("class Fred { void (*func(int f))(char) { } };");
// 3 scopes: Global, Class, and Function
ASSERT(db && db->scopeList.size() == 3);
@ -1624,7 +1624,7 @@ private:
}
void hasMissingInlineClassFunctionReturningFunctionPointer() {
GET_SYMBOL_DB("class Fred { void (*func(int f))(char); };\n");
GET_SYMBOL_DB("class Fred { void (*func(int f))(char); };");
// 2 scopes: Global and Class (no Function scope because there is no function implementation)
ASSERT(db && db->scopeList.size() == 2);
@ -1643,7 +1643,7 @@ private:
}
void hasClassFunctionReturningFunctionPointer() {
GET_SYMBOL_DB("class Fred { void (*func(int f))(char); }; void (*Fred::func(int f))(char) { }\n");
GET_SYMBOL_DB("class Fred { void (*func(int f))(char); }; void (*Fred::func(int f))(char) { }");
// 3 scopes: Global, Class, and Function
ASSERT(db && db->scopeList.size() == 3);
@ -1662,7 +1662,7 @@ private:
}
void methodWithRedundantScope() {
GET_SYMBOL_DB("class Fred { void Fred::func() {} };\n");
GET_SYMBOL_DB("class Fred { void Fred::func() {} };");
// 3 scopes: Global, Class, and Function
ASSERT(db && db->scopeList.size() == 3);
@ -1971,7 +1971,7 @@ private:
void parseFunctionCorrect() {
// ticket 3188 - "if" statement parsed as function
GET_SYMBOL_DB("void func(i) int i; { if (i == 1) return; }\n");
GET_SYMBOL_DB("void func(i) int i; { if (i == 1) return; }");
ASSERT(db != nullptr);
// 3 scopes: Global, function, if
@ -1998,7 +1998,7 @@ private:
}
void hasGlobalVariables1() {
GET_SYMBOL_DB("int i;\n");
GET_SYMBOL_DB("int i;");
ASSERT(db && db->scopeList.size() == 1);
@ -2010,7 +2010,7 @@ private:
}
void hasGlobalVariables2() {
GET_SYMBOL_DB("int array[2][2];\n");
GET_SYMBOL_DB("int array[2][2];");
ASSERT(db && db->scopeList.size() == 1);
@ -2023,7 +2023,7 @@ private:
}
void hasGlobalVariables3() {
GET_SYMBOL_DB("int array[2][2] = { { 0, 0 }, { 0, 0 } };\n");
GET_SYMBOL_DB("int array[2][2] = { { 0, 0 }, { 0, 0 } };");
ASSERT(db && db->scopeList.size() == 1);
@ -2598,7 +2598,7 @@ private:
check("static void function_declaration_before(void) __attribute__((__used__));\n"
"static void function_declaration_before(void) {}\n"
"static void function_declaration_after(void) {}\n"
"static void function_declaration_after(void) __attribute__((__used__));\n");
"static void function_declaration_after(void) __attribute__((__used__));");
ASSERT_EQUALS("", errout.str());
check("main(int argc, char *argv[]) { }", true, "test.c");
@ -2769,7 +2769,7 @@ private:
// ticket 3437 (segmentation fault)
void symboldatabase22() {
check("template <class C> struct A {};\n"
"A<int> a;\n");
"A<int> a;");
ASSERT_EQUALS("", errout.str());
}
@ -2898,7 +2898,7 @@ private:
"::Foo::Sub::Sub() { }\n"
"class Foo;\n"
"class Bar;\n"
"class Sub;\n");
"class Sub;");
ASSERT(db && db->typeList.size() == 5);
std::list<Type>::const_iterator i = db->typeList.begin();
@ -2966,7 +2966,7 @@ private:
" Fred(const struct Barney & b) { barney = b; }\n"
"private:\n"
" struct Barney barney;\n"
"};\n");
"};");
ASSERT(db && db->typeList.size() == 3);
std::list<Type>::const_iterator i = db->typeList.begin();
@ -2999,17 +2999,17 @@ private:
check("void f() {\n"
" try { }\n"
" catch (std::bad_alloc) { }\n"
"}\n");
"}");
ASSERT_EQUALS("", errout.str());
}
void symboldatabase41() { // ticket #5197 (unknown macro)
GET_SYMBOL_DB("struct X1 { MACRO1 f(int spd) MACRO2; };\n");
GET_SYMBOL_DB("struct X1 { MACRO1 f(int spd) MACRO2; };");
ASSERT(db && db->findScopeByName("X1") && db->findScopeByName("X1")->functionList.size() == 1 && !db->findScopeByName("X1")->functionList.front().hasBody());
}
void symboldatabase42() { // only put variables in variable list
GET_SYMBOL_DB("void f() { extern int x(); }\n");
GET_SYMBOL_DB("void f() { extern int x(); }");
ASSERT(db != nullptr);
const Scope * const fscope = db ? db->findScopeByName("f") : nullptr;
ASSERT(fscope != nullptr);
@ -3019,7 +3019,7 @@ private:
void symboldatabase43() { // ticket #4738
check("void f() {\n"
" new int;\n"
"}\n");
"}");
ASSERT_EQUALS("", errout.str());
}
@ -3111,7 +3111,7 @@ private:
"}\n"
"using namespace NS;\n"
"MyClass::~MyClass() { }\n"
"MyClass::MyClass() { }\n");
"MyClass::MyClass() { }");
ASSERT(db && !db->functionScopes.empty() && db->functionScopes.front()->function && db->functionScopes.front()->function->functionScope == db->functionScopes.front());
const Token *f = Token::findsimplematch(tokenizer.tokens(), "MyClass ( ) ;");
@ -3126,7 +3126,7 @@ private:
GET_SYMBOL_DB("namespace Ns { class C; }\n"
"void f1() { char *p; *p = 0; }\n"
"class Ns::C* p;\n"
"void f2() { char *p; *p = 0; }\n");
"void f2() { char *p; *p = 0; }");
ASSERT(db != nullptr);
const Token *f = Token::findsimplematch(tokenizer.tokens(), "p ; void f2");
ASSERT_EQUALS(true, db && f && f->variable());
@ -3163,7 +3163,7 @@ private:
"static const float f3 = 2 * std::foo3(a, b);\n"
"static const float f4 = c * foo4(a, b);\n"
"static const int i1 = 2 & foo5(a, b);\n"
"static const bool b1 = 2 > foo6(a, b);\n");
"static const bool b1 = 2 > foo6(a, b);");
ASSERT(db != nullptr);
ASSERT(findFunctionByName("foo1", &db->scopeList.front()) == nullptr);
ASSERT(findFunctionByName("foo2", &db->scopeList.front()) == nullptr);
@ -3248,7 +3248,7 @@ private:
}
{
GET_SYMBOL_DB_C("friend f1();\n"
"friend f2() { }\n");
"friend f2() { }");
ASSERT(db != nullptr);
ASSERT_EQUALS(2U, db->scopeList.size());
@ -3816,7 +3816,7 @@ private:
" ~impl() { }\n"
" impl(const impl &) { }\n"
" void foo(const impl &, const impl &) const { }\n"
"};\n");
"};");
ASSERT(db != nullptr);
ASSERT(db && db->scopeList.size() == 7);
@ -3859,7 +3859,7 @@ private:
"template <typename A> Fred<A>::impl::impl() { }\n"
"template <typename A> Fred<A>::impl::~impl() { }\n"
"template <typename A> Fred<A>::impl::impl(const Fred<A>::impl &) { }\n"
"template <typename A> void Fred<A>::impl::foo(const Fred<A>::impl &, const Fred<A>::impl &) const { }\n");
"template <typename A> void Fred<A>::impl::foo(const Fred<A>::impl &, const Fred<A>::impl &) const { }");
ASSERT(db != nullptr);
ASSERT(db && db->scopeList.size() == 7);
@ -3990,7 +3990,7 @@ private:
"template <typename A> NS::Fred<A>::impl::impl() { }\n"
"template <typename A> NS::Fred<A>::impl::~impl() { }\n"
"template <typename A> NS::Fred<A>::impl::impl(const NS::Fred<A>::impl &) { }\n"
"template <typename A> void NS::Fred<A>::impl::foo(const NS::Fred<A>::impl &, const NS::Fred<A>::impl &) const { }\n");
"template <typename A> void NS::Fred<A>::impl::foo(const NS::Fred<A>::impl &, const NS::Fred<A>::impl &) const { }");
ASSERT(db != nullptr);
ASSERT(db && db->scopeList.size() == 8);
@ -4076,7 +4076,7 @@ private:
"template <typename A> NS::Fred<A>::impl::impl() { }\n"
"template <typename A> NS::Fred<A>::impl::~impl() { }\n"
"template <typename A> NS::Fred<A>::impl::impl(const NS::Fred<A>::impl &) { }\n"
"template <typename A> void NS::Fred<A>::impl::foo(const NS::Fred<A>::impl &, const NS::Fred<A>::impl &) const { }\n");
"template <typename A> void NS::Fred<A>::impl::foo(const NS::Fred<A>::impl &, const NS::Fred<A>::impl &) const { }");
ASSERT(db != nullptr);
ASSERT(db && db->scopeList.size() == 8);
@ -4169,7 +4169,7 @@ private:
"template <typename A> Fred<A>::impl::impl() { }\n"
"template <typename A> Fred<A>::impl::~impl() { }\n"
"template <typename A> Fred<A>::impl::impl(const Fred<A>::impl &) { }\n"
"template <typename A> void Fred<A>::impl::foo(const Fred<A>::impl &, const Fred<A>::impl &) const { }\n");
"template <typename A> void Fred<A>::impl::foo(const Fred<A>::impl &, const Fred<A>::impl &) const { }");
ASSERT(db != nullptr);
ASSERT(db && db->scopeList.size() == 8);
@ -4822,7 +4822,7 @@ private:
"int b[A];\n"
"int c[A + 2];\n"
"int d[10 + B];\n"
"int e[A + B];\n");
"int e[A + B];");
ASSERT(db != nullptr);
ASSERT_EQUALS(2U, db->scopeList.size());
@ -4916,7 +4916,7 @@ private:
"char array9[sizeof(EL)];\n"
"char array10[sizeof(L)];\n"
"char array11[sizeof(ELL)];\n"
"char array12[sizeof(LL)];\n");
"char array12[sizeof(LL)];");
ASSERT(db != nullptr);
ASSERT(db->variableList().size() == 13); // the first one is not used
@ -5419,7 +5419,7 @@ private:
"void S::g() &&{ }\n"
"void S::g() const { }\n"
"void S::g() const & { }\n"
"void S::g() const &&{ }\n");
"void S::g() const &&{ }");
ASSERT_EQUALS("", errout.str());
const Token *f = Token::findsimplematch(tokenizer.tokens(), "f ( ) {");
@ -6463,7 +6463,7 @@ private:
GET_SYMBOL_DB("void func1() noexcept;\n"
"void func2() noexcept { }\n"
"void func3() noexcept(true);\n"
"void func4() noexcept(true) { }\n");
"void func4() noexcept(true) { }");
ASSERT_EQUALS("", errout.str());
ASSERT_EQUALS(true, db != nullptr); // not null
@ -6474,7 +6474,7 @@ private:
}
void noexceptFunction2() {
GET_SYMBOL_DB("template <class T> void self_assign(T& t) noexcept(noexcept(t = t)) {t = t; }\n");
GET_SYMBOL_DB("template <class T> void self_assign(T& t) noexcept(noexcept(t = t)) {t = t; }");
ASSERT_EQUALS("", errout.str());
ASSERT_EQUALS(true, db != nullptr); // not null
@ -6535,7 +6535,7 @@ private:
" A a;\n"
" B(B&& b) noexcept\n"
" :a(std::move(b.a)) { }\n"
"};\n");
"};");
ASSERT_EQUALS("", errout.str());
ASSERT(db != nullptr); // not null
const Scope *b = db->findScopeByName("B");
@ -6553,7 +6553,7 @@ private:
GET_SYMBOL_DB("void func1() throw();\n"
"void func2() throw() { }\n"
"void func3() throw(int);\n"
"void func4() throw(int) { }\n");
"void func4() throw(int) { }");
ASSERT_EQUALS("", errout.str());
ASSERT(db != nullptr); // not null
@ -6604,7 +6604,7 @@ private:
void nothrowAttributeFunction() {
GET_SYMBOL_DB("void func() __attribute__((nothrow));\n"
"void func() { }\n");
"void func() { }");
ASSERT_EQUALS("", errout.str());
ASSERT_EQUALS(true, db != nullptr); // not null
@ -6614,7 +6614,7 @@ private:
}
void nothrowDeclspecFunction() {
GET_SYMBOL_DB("void __declspec(nothrow) func() { }\n");
GET_SYMBOL_DB("void __declspec(nothrow) func() { }");
ASSERT_EQUALS("", errout.str());
ASSERT_EQUALS(true, db != nullptr); // not null
@ -6847,7 +6847,7 @@ private:
GET_SYMBOL_DB("class Fred {\n"
" void foo(const std::string & a = \"\");\n"
"};\n"
"Fred::foo(const std::string & b) { }\n");
"Fred::foo(const std::string & b) { }");
ASSERT(db && db->scopeList.size() == 3);
std::list<Scope>::const_iterator scope = db->scopeList.begin();

View File

@ -1083,7 +1083,7 @@ private:
}
void canFindMatchingBracketsWithTooManyClosing() const {
givenACodeSampleToTokenize var("X< 1>2 > x1;\n");
givenACodeSampleToTokenize var("X< 1>2 > x1;");
const Token* const t = var.tokens()->next()->findClosingBracket();
ASSERT_EQUALS(">", t->str());
@ -1091,7 +1091,7 @@ private:
}
void canFindMatchingBracketsWithTooManyOpening() const {
givenACodeSampleToTokenize var("X < (2 < 1) > x1;\n");
givenACodeSampleToTokenize var("X < (2 < 1) > x1;");
const Token* t = var.tokens()->next()->findClosingBracket();
ASSERT(t != nullptr && t->str() == ">");

View File

@ -3295,7 +3295,7 @@ private:
"{}\n"), InternalError);
tokenizeAndStringify("void foo(int, int)\n"
"{}\n");
"{}");
ASSERT_EQUALS("", errout.str());
// #3848 - Don't hang

View File

@ -171,7 +171,7 @@ private:
" if (k > 32)\n"
" return 0;\n"
" return rm>> k;\n"
"}\n");
"}");
ASSERT_EQUALS(
"[test.cpp:4] -> [test.cpp:6]: (warning) Shifting signed 32-bit value by 31 bits is undefined behaviour. See condition at line 4.\n",
errout.str());
@ -183,7 +183,7 @@ private:
" return 0;\n"
" else\n"
" return rm>> k;\n"
"}\n");
"}");
ASSERT_EQUALS(
"[test.cpp:4] -> [test.cpp:7]: (warning) Shifting signed 32-bit value by 31 bits is undefined behaviour. See condition at line 4.\n",
errout.str());
@ -195,14 +195,14 @@ private:
" return 0;\n"
" else\n"
" return rm>> k;\n"
"}\n");
"}");
ASSERT_EQUALS("", errout.str());
check("static long long f(int x, long long y) {\n"
" if (x >= 64)\n"
" return 0;\n"
" return -(y << (x-1));\n"
"}\n");
"}");
ASSERT_EQUALS("", errout.str());
}
@ -258,7 +258,7 @@ private:
check("unsigned int dostuff(int x) {\n" // x is signed
" if (x==0) {}\n"
" return (x-1)*sizeof(int);\n"
"}\n");
"}");
ASSERT_EQUALS("[test.cpp:2] -> [test.cpp:3]: (warning) Expression 'x-1' can have a negative value. That is converted to an unsigned value and used in an unsigned calculation.\n", errout.str());
check("unsigned int f1(signed int x, unsigned int y) {" // x is signed
@ -360,27 +360,27 @@ private:
check("void f(void) {\n"
" return (int)1E100;\n"
"}\n");
"}");
ASSERT_EQUALS("[test.cpp:2]: (error) Undefined behaviour: float () to integer conversion overflow.\n", removeFloat(errout.str()));
check("void f(void) {\n"
" return (int)-1E100;\n"
"}\n");
"}");
ASSERT_EQUALS("[test.cpp:2]: (error) Undefined behaviour: float () to integer conversion overflow.\n", removeFloat(errout.str()));
check("void f(void) {\n"
" return (short)1E6;\n"
"}\n");
"}");
ASSERT_EQUALS("[test.cpp:2]: (error) Undefined behaviour: float () to integer conversion overflow.\n", removeFloat(errout.str()));
check("void f(void) {\n"
" return (unsigned char)256.0;\n"
"}\n");
"}");
ASSERT_EQUALS("[test.cpp:2]: (error) Undefined behaviour: float () to integer conversion overflow.\n", removeFloat(errout.str()));
check("void f(void) {\n"
" return (unsigned char)255.5;\n"
"}\n");
"}");
ASSERT_EQUALS("", removeFloat(errout.str()));
check("void f(void) {\n"

View File

@ -1586,7 +1586,7 @@ private:
" vertices[i][1][2] = 4.0;\n"
" vertices[i][1][3] = 5.0;\n"
" }\n"
"}\n");
"}");
ASSERT_EQUALS("[test.cpp:4]: (error) Uninitialized variable: vertices\n",
errout.str());
}
@ -3184,7 +3184,7 @@ private:
" struct AB ab;\n"
" ab.a = 0;\n"
" do_something(ab);\n"
"}\n");
"}");
ASSERT_EQUALS("", errout.str());
checkUninitVar("struct AB { int a; int b; };\n"
@ -3389,7 +3389,7 @@ private:
" struct AB ab;\n"
" ab.set();\n"
" x = ab;\n"
"}\n");
"}");
ASSERT_EQUALS("", errout.str());
checkUninitVar("struct AB { int a; int get() const; };\n"
@ -3397,7 +3397,7 @@ private:
" struct AB ab;\n"
" ab.get();\n"
" x = ab;\n"
"}\n");
"}");
ASSERT_EQUALS("[test.cpp:5]: (error) Uninitialized struct member: ab.a\n", errout.str());
checkUninitVar("struct AB { int a; void dostuff() {} };\n"
@ -3405,7 +3405,7 @@ private:
" struct AB ab;\n"
" ab.dostuff();\n"
" x = ab;\n"
"}\n");
"}");
TODO_ASSERT_EQUALS("error", "", errout.str());
}
@ -4419,7 +4419,7 @@ private:
" int i;\n"
" int* v = &i;\n"
" sscanf(\"0\", \"%d\", v);\n"
"}\n");
"}");
ASSERT_EQUALS("", errout.str());
valueFlowUninit("void test(int p) {\n"
@ -4428,7 +4428,7 @@ private:
" f = 0;\n"
" if (p > 1)\n"
" f += 1;\n"
"}\n");
"}");
ASSERT_EQUALS("", errout.str());
valueFlowUninit("unsigned char get();\n"
@ -4479,7 +4479,7 @@ private:
"void f(void) {\n"
" someType_t gVar;\n"
" bar(&gVar);\n"
"}\n");
"}");
ASSERT_EQUALS("[test.cpp:9] -> [test.cpp:5]: (error) Uninitialized variable: flags\n", errout.str());
valueFlowUninit("typedef struct\n"
@ -4489,7 +4489,7 @@ private:
"void f(void) {\n"
" someType_t gVar;\n"
" if(gVar.flags[1] == 42){}\n"
"}\n");
"}");
ASSERT_EQUALS("[test.cpp:7]: (error) Uninitialized variable: flags\n", errout.str());
valueFlowUninit("struct pc_data {\n"
@ -4500,7 +4500,7 @@ private:
"void f() {\n"
" struct pc_data *pcdata;\n"
" if ( *pcdata->wampiryzm.strefa == '\\0' ) { }\n"
"}\n");
"}");
ASSERT_EQUALS("[test.cpp:8]: (error) Uninitialized variable: pcdata\n", errout.str());
// # 9293
@ -4513,7 +4513,7 @@ private:
" struct S s1;\n"
" int * x = &s1.x;\n"
" struct S s2 = {*x, 0};\n"
"}\n");
"}");
ASSERT_EQUALS("[test.cpp:8] -> [test.cpp:9]: (error) Uninitialized variable: *x\n", errout.str());
valueFlowUninit("struct S {\n"
@ -4526,7 +4526,7 @@ private:
" struct S s2;\n"
" int * x = &s1.x;\n"
" s2.x = *x;\n"
"}\n");
"}");
ASSERT_EQUALS("[test.cpp:9] -> [test.cpp:10]: (error) Uninitialized variable: *x\n", errout.str());
valueFlowUninit("void f(bool * x) {\n"
@ -4535,7 +4535,7 @@ private:
"void g() {\n"
" bool b;\n"
" f(&b);\n"
"}\n");
"}");
ASSERT_EQUALS("", errout.str());
valueFlowUninit("void f(bool * x) {\n"
@ -4545,7 +4545,7 @@ private:
"void g() {\n"
" bool x;\n"
" f(&x);\n"
"}\n");
"}");
ASSERT_EQUALS("", errout.str());
valueFlowUninit("void f() {\n"
@ -4553,7 +4553,7 @@ private:
" bool * x = &b;\n"
" if (x != nullptr)\n"
" x = 1;\n"
"}\n");
"}");
ASSERT_EQUALS("", errout.str());
valueFlowUninit("struct A { bool b; };"
@ -4563,14 +4563,14 @@ private:
"void g() {\n"
" A b;\n"
" f(&b);\n"
"}\n");
"}");
ASSERT_EQUALS("", errout.str());
valueFlowUninit("std::string f() {\n"
" std::ostringstream ostr;\n"
" ostr << \"\";\n"
" return ostr.str();\n"
"}\n");
"}");
ASSERT_EQUALS("", errout.str());
// #9281
valueFlowUninit("struct s {\n"
@ -4585,7 +4585,7 @@ private:
"void a() {\n"
" struct s s1;\n"
" b(&s1);\n"
"}\n");
"}");
ASSERT_EQUALS("", errout.str());
// # 9290
@ -4596,7 +4596,7 @@ private:
" A * c;\n"
" c->x = 42;\n"
" return c->x;\n"
"}\n");
"}");
ASSERT_EQUALS("[test.cpp:6]: (error) Uninitialized variable: c\n"
"[test.cpp:7]: (error) Uninitialized variable: c\n",
errout.str());
@ -4608,7 +4608,7 @@ private:
" A c;\n"
" c.x = 42;\n"
" return c.x;\n"
"}\n");
"}");
ASSERT_EQUALS("", errout.str());
valueFlowUninit("struct A {\n"
@ -4621,7 +4621,7 @@ private:
"double b() {\n"
" A c;\n"
" return d(&c);\n"
"}\n");
"}");
ASSERT_EQUALS("", errout.str());
// # 9302
@ -4634,7 +4634,7 @@ private:
" vz.typ = 42;\n"
" if (pvz->typ == 0)\n"
" return;\n"
"}\n");
"}");
ASSERT_EQUALS("", errout.str());
// # 9305
@ -4650,7 +4650,7 @@ private:
" set( pb);\n"
" if (pb->x)\n"
" return;\n"
"}\n");
"}");
ASSERT_EQUALS("", errout.str());
// # 9348
@ -4661,7 +4661,7 @@ private:
"void g() {\n"
" int i;\n"
" f(&i);\n"
"}\n");
"}");
ASSERT_EQUALS("", errout.str());
// # 9631
@ -4672,7 +4672,7 @@ private:
" size_t bytesCopied;\n"
" bool copied_all = true;\n"
" g(&copied_all, 5, 6, &bytesCopied);\n"
"}\n");
"}");
ASSERT_EQUALS("[test.cpp:7] -> [test.cpp:2]: (error) Uninitialized variable: *buflen\n", errout.str());
}
@ -4684,7 +4684,7 @@ private:
"void f() {\n"
" C *c;\n"
" if (c->x() == 4) {}\n"
"}\n");
"}");
ASSERT_EQUALS("[test.cpp:6]: (error) Uninitialized variable: c\n", errout.str());
}
@ -4696,7 +4696,7 @@ private:
"int main() {\n"
" Foo* foo;\n"
" foo->b\n"
"}\n");
"}");
ASSERT_EQUALS("[test.cpp:7]: (error) Uninitialized variable: foo\n", errout.str());
}

View File

@ -226,7 +226,7 @@ private:
"private:\n"
" template<typename T> void foo( T t ) const;\n"
"};\n"
"template<typename T> void X::foo( T t ) const { }\n");
"template<typename T> void X::foo( T t ) const { }");
ASSERT_EQUALS("[test.cpp:3]: (style) The function 'bar' is never used.\n", errout.str());
}
@ -257,19 +257,19 @@ private:
void unusedError() {
check("void foo() {}\n"
"int main()\n");
"int main()");
ASSERT_EQUALS("[test.cpp:1]: (style) The function 'foo' is never used.\n", errout.str());
check("void foo() const {}\n"
"int main()\n");
"int main()");
ASSERT_EQUALS("[test.cpp:1]: (style) The function 'foo' is never used.\n", errout.str());
check("void foo() const throw() {}\n"
"int main()\n");
"int main()");
ASSERT_EQUALS("[test.cpp:1]: (style) The function 'foo' is never used.\n", errout.str());
check("void foo() throw() {}\n"
"int main()\n");
"int main()");
ASSERT_EQUALS("[test.cpp:1]: (style) The function 'foo' is never used.\n", errout.str());
}
@ -427,7 +427,7 @@ private:
void lineNumber() {
check("void foo() {}\n"
"void bar() {}\n"
"int main()\n");
"int main()");
ASSERT_EQUALS("[test.cpp:2]: (style) The function 'bar' is never used.\n"
"[test.cpp:1]: (style) The function 'foo' is never used.\n", errout.str());
}
@ -486,7 +486,7 @@ private:
"A operator ++ (const A &){ return A(); }\n"
"A operator -- (const A &, const int){ return A(); }\n"
"A operator -- (const A &){ return A(); }\n"
"A operator , (const A &, const A &){ return A(); }\n");
"A operator , (const A &, const A &){ return A(); }");
ASSERT_EQUALS("", errout.str());

View File

@ -416,7 +416,7 @@ private:
"}\n"
"class A::B {"
" B() { A a; a.f(); }\n"
"}\n");
"}");
ASSERT_EQUALS("", errout.str());
}

View File

@ -1464,7 +1464,7 @@ private:
"{\n"
" printf(\"var.struct1.a = %d\", var.struct1.a);\n"
" return 1;\n"
"}\n");
"}");
ASSERT_EQUALS("", errout.str());
}
@ -3136,7 +3136,7 @@ private:
functionVariableUsage("Padding fun() {\n"
" Distance d = DISTANCE;\n"
" return (Padding){ d, d, d, d };\n"
"}\n");
"}");
ASSERT_EQUALS("", errout.str());
}
@ -3203,7 +3203,7 @@ private:
functionVariableUsage("void fun() {\n"
" int x;\n"
" while (c) { x=10; }\n"
"}\n");
"}");
ASSERT_EQUALS("[test.cpp:3]: (style) Variable 'x' is assigned a value that is never used.\n", errout.str());
functionVariableUsage("void dostuff(int x);\n"
@ -3213,7 +3213,7 @@ private:
" dostuff(x);\n"
" if (y) { x=10; break; }\n"
" }\n"
"}\n");
"}");
ASSERT_EQUALS("[test.cpp:6]: (style) Variable 'x' is assigned a value that is never used.\n", errout.str());
functionVariableUsage("void dostuff(int &x);\n"
@ -3223,7 +3223,7 @@ private:
" dostuff(x);\n"
" if (y) { x=10; break; }\n"
" }\n"
"}\n");
"}");
ASSERT_EQUALS("", errout.str()); // TODO : in this special case we can ignore that x is aliased. x is local and there are no function calls after the assignment
functionVariableUsage("void fun() {\n"
@ -3232,13 +3232,13 @@ private:
" dostuff(x);\n"
" x = 10;\n"
" }\n"
"}\n");
"}");
ASSERT_EQUALS("", errout.str());
functionVariableUsage("void fun() {\n"
" int x = 0;\n"
" while (x < 10) { x = x + 1; }\n"
"}\n");
"}");
ASSERT_EQUALS("", errout.str());
functionVariableUsage("void fun()\n"
@ -5469,7 +5469,7 @@ private:
" goto label;\n"
" }\n"
" return false;\n"
"}\n");
"}");
ASSERT_EQUALS("[test.cpp:5]: (style) Variable 'X' is assigned a value that is never used.\n", errout.str());
// #4558
@ -5721,8 +5721,7 @@ private:
" static int fpUnread{0};\n"
" const int var{fpUnread++};\n"
" return var;\n"
"}\n"
);
"}");
ASSERT_EQUALS("", errout.str());
}
@ -5730,8 +5729,7 @@ private:
functionVariableUsage(
"void fun(Value value) {\n"
" value[10] = 123;\n"
"}\n"
);
"}");
ASSERT_EQUALS("", errout.str());
functionVariableUsage(
@ -5810,8 +5808,7 @@ private:
" else\n"
" (*pos).second = number;\n"
" }\n"
"};\n"
);
"};");
ASSERT_EQUALS("", errout.str());
}

View File

@ -167,7 +167,7 @@ private:
" return va_arg(ap, const char*);\n"
" });\n"
" va_end(ap);\n"
"}\n");
"}");
ASSERT_EQUALS("", errout.str());
check("void f(int n, ...)\n"
@ -179,7 +179,7 @@ private:
" {\n"
" return va_arg(ap, const char*);\n"
" });\n"
"}\n");
"}");
ASSERT_EQUALS("[test.cpp:10]: (error) va_list 'ap' was opened but not closed by va_end().\n", errout.str());
}

View File

@ -1556,7 +1556,7 @@ private:
"\n"
"out:\n"
" if (abc) {}\n"
"}\n");
"}");
}
void valueFlowBeforeConditionForward() {

View File

@ -476,7 +476,7 @@ private:
"{\n"
" std::string str(\"test\");\n"
" str.clear();\n"
"}\n");
"}");
const char expected[] = "1: void func ( )\n"
"2: {\n"
@ -514,8 +514,7 @@ private:
}
void varid11() {
const std::string actual = tokenize(
"class Foo;\n");
const std::string actual = tokenize("class Foo;");
const char expected[] = "1: class Foo ;\n";
@ -527,7 +526,7 @@ private:
"static void a()\n"
"{\n"
" class Foo *foo;\n"
"}\n");
"}");
const char expected[] = "1: static void a ( )\n"
"2: {\n"
@ -1278,8 +1277,7 @@ private:
"std::list<boost::wave::token_id> tokens;\n"
"static std::vector<CvsProcess*> ex1;\n"
"extern std::vector<CvsProcess*> ex2;\n"
"std::map<int, 1> m;\n"
);
"std::map<int, 1> m;");
const char expected[] = "1: list < int > ints@1 ;\n"
"2: list < int > :: iterator it@2 ;\n"
@ -1313,7 +1311,7 @@ private:
"{\n"
" int *a;\n"
" delete a;\n"
"}\n");
"}");
const char expected[] = "1: void f ( )\n"
"2: {\n"
@ -1364,7 +1362,7 @@ private:
"void f3(const std::string &s)\n"
"{\n"
" s.size();\n"
"}\n");
"}");
const char expected[] = "1: void f1 ( int & p@1 )\n"
"2: {\n"
@ -1408,7 +1406,7 @@ private:
" std::vector<int> b;\n"
" std::vector<int> &a = b;\n"
" std::vector<int> *c = &b;\n"
"}\n");
"}");
const char expected[] = "1: void f ( )\n"
"2: {\n"
@ -1428,7 +1426,7 @@ private:
"public:\n"
" std::string name1;\n"
" std::string name2;\n"
"};\n");
"};");
const char expected[] = "1: class Foo\n"
"2: {\n"
@ -1454,7 +1452,7 @@ private:
" POINT pOutput = { 0 , 0 };\n"
" int x = pOutput.x;\n"
" int y = pOutput.y;\n"
"}\n");
"}");
const char expected[] = "1: class foo\n"
"2: {\n"
@ -1489,7 +1487,7 @@ private:
"void Bar::f()\n"
"{\n"
" foo.x = x;\n"
"}\n");
"}");
const char expected[] = "1: struct Foo {\n"
"2: int x@1 ;\n"
"3: } ;\n"
@ -2199,7 +2197,7 @@ private:
"{\n"
"public:\n"
" void operator=(const Foo &);\n"
"};\n");
"};");
const char expected[] = "1: class Foo\n"
"2: {\n"
@ -2213,7 +2211,7 @@ private:
const std::string actual = tokenize(
"struct Foo {\n"
" void * operator new [](int);\n"
"};\n");
"};");
const char expected[] = "1: struct Foo {\n"
"2: void * operatornew[] ( int ) ;\n"
"3: } ;\n";
@ -2225,7 +2223,7 @@ private:
void varid_throw() { // ticket #1723
const std::string actual = tokenize(
"UserDefinedException* pe = new UserDefinedException();\n"
"throw pe;\n");
"throw pe;");
const char expected[] = "1: UserDefinedException * pe@1 ; pe@1 = new UserDefinedException ( ) ;\n"
"2: throw pe@1 ;\n";
@ -2716,7 +2714,7 @@ private:
"Fred::foo1()\n"
"{\n"
" i = 0;\n"
"}\n");
"}");
const char expected[] = "1: class Fred\n"
"2: {\n"
@ -2752,7 +2750,7 @@ private:
"void Fred::f()\n"
"{\n"
" i = 0;\n"
"}\n");
"}");
const char expected[] = "1: class Fred\n"
"2: { void f ( ) ; } ;\n"
@ -2784,7 +2782,7 @@ private:
"void A::f()\n"
"{\n"
" i = 0;\n"
"}\n");
"}");
const char expected[] = "1: class Fred\n"
"2: { int i@1 ; void f ( ) ; } ;\n"
@ -2812,7 +2810,7 @@ private:
"{\n"
" if (i) { }\n"
" i = 0;\n"
"}\n");
"}");
const char expected[] = "1: class Fred\n"
"2: { int i@1 ; void f ( ) ; } ;\n"
@ -2834,7 +2832,7 @@ private:
" A *a;\n"
" B() : a(new A)\n"
" { }\n"
"};\n");
"};");
const char expected[] = "1: class A { } ;\n"
"2: class B\n"