This commit is contained in:
chrchr-github 2024-01-02 15:49:47 +01:00 committed by GitHub
parent 98b9244bcb
commit 14627ca6d2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 69 additions and 0 deletions

View File

@ -1697,6 +1697,24 @@ private:
ASSERT_EQUALS( ASSERT_EQUALS(
"[test.cpp:3] -> [test.cpp:6]: (warning) Either the condition 'x<2' is redundant or the array 'a[3]' is accessed at index 3, which is out of bounds.\n", "[test.cpp:3] -> [test.cpp:6]: (warning) Either the condition 'x<2' is redundant or the array 'a[3]' is accessed at index 3, which is out of bounds.\n",
errout.str()); errout.str());
check("void f() {\n" // #2199
" char a[5];\n"
" for (int i = 0; i < 5; i++) {\n"
" i += 8;\n"
" a[i] = 0;\n"
" }\n"
"}\n"
"void g() {\n"
" char a[5];\n"
" for (int i = 0; i < 5; i++) {\n"
" a[i + 7] = 0;\n"
" }\n"
"}\n");
ASSERT_EQUALS(
"[test.cpp:5]: (error) Array 'a[5]' accessed at index 8, which is out of bounds.\n"
"[test.cpp:11]: (error) Array 'a[5]' accessed at index 11, which is out of bounds.\n",
errout.str());
} }
void array_index_59() // #10413 void array_index_59() // #10413

View File

@ -5117,6 +5117,39 @@ private:
" if (s.empty() || s.size() < 1) {}\n" " if (s.empty() || s.size() < 1) {}\n"
"}\n"); "}\n");
ASSERT_EQUALS("[test.cpp:2] -> [test.cpp:2]: (style) Condition 's.size()<1' is always false\n", errout.str()); ASSERT_EQUALS("[test.cpp:2] -> [test.cpp:2]: (style) Condition 's.size()<1' is always false\n", errout.str());
check("void bar(std::vector<int>& vv) {\n" // #11464
" class F {\n"
" public:\n"
" F(int, std::vector<int>& lv) : mV(lv) {\n"
" mV.push_back(0);\n"
" }\n"
" private:\n"
" std::vector<int>& mV;\n"
" } fi(1, vv);\n"
"}\n"
"void g() {\n"
" std::vector<int> v;\n"
" bar(v);\n"
" if (v.empty()) {}\n"
"}\n");
ASSERT_EQUALS("", errout.str());
check("struct F {\n"
" F(int, std::vector<int>&lv) : mV(lv) {\n"
" mV.push_back(0);\n"
" }\n"
" std::vector<int>& mV;\n"
"};\n"
"void g(std::vector<int>& vv) {\n"
" F(1, vv);\n"
"}\n"
"void f() {\n"
" std::vector<int> v;\n"
" g(v);\n"
" if (v.empty()) {}\n"
"}\n");
ASSERT_EQUALS("", errout.str());
} }
void alwaysTrueLoop() void alwaysTrueLoop()

View File

@ -3451,6 +3451,24 @@ private:
" g(U::V(&t));\n" " g(U::V(&t));\n"
"}\n"); "}\n");
ASSERT_EQUALS("[test.cpp:6]: (style) Parameter 't' can be declared as reference to const\n", errout.str()); ASSERT_EQUALS("[test.cpp:6]: (style) Parameter 't' can be declared as reference to const\n", errout.str());
check("void f1(std::vector<int>& v) {\n" // #11207
" auto it = v.cbegin();\n"
" while (it != v.cend()) {\n"
" if (*it > 12) {}\n"
" ++it;\n"
" }\n"
"}\n"
"void f2(std::vector<int>& v) {\n"
" auto it = v.begin();\n"
" while (it != v.end()) {\n"
" if (*it > 12) {}\n"
" ++it;\n"
" }\n"
"}\n");
ASSERT_EQUALS("[test.cpp:1]: (style) Parameter 'v' can be declared as reference to const\n"
"[test.cpp:8]: (style) Parameter 'v' can be declared as reference to const\n",
errout.str());
} }
void constParameterCallback() { void constParameterCallback() {