Fix constPointer FP #11674, TODOs (#4976)

* Fix constPointer TODOs

* Fix #11674 FP constParameterPointer when function signature is fixed

* Format
This commit is contained in:
chrchr-github 2023-04-18 20:36:14 +02:00 committed by GitHub
parent 71f28fca23
commit e70a888833
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 21 additions and 8 deletions

View File

@ -1545,7 +1545,9 @@ void CheckOther::checkConstPointer()
const ValueType* const vt = tok->valueType();
if (!vt)
continue;
if ((vt->pointer != 1 && !(vt->pointer == 2 && var->isArray())) || (vt->constness & 1) || vt->reference != Reference::None)
if ((vt->pointer != 1 && !(vt->pointer == 2 && var->isArray())) || (vt->constness & 1))
continue;
if (var->typeStartToken()->isTemplateArg())
continue;
if (std::find(nonConstPointers.cbegin(), nonConstPointers.cend(), var) != nonConstPointers.cend())
continue;
@ -1618,7 +1620,7 @@ void CheckOther::checkConstPointer()
continue;
if (p->isArgument() && p->typeStartToken() && p->typeStartToken()->isSimplifiedTypedef() && !(Token::simpleMatch(p->typeEndToken(), "*") && !p->typeEndToken()->isSimplifiedTypedef()))
continue;
constVariableError(p, nullptr);
constVariableError(p, p->isArgument() ? p->scope()->function : nullptr);
}
}
}

View File

@ -3298,7 +3298,7 @@ private:
" for (const auto& h : v)\n"
" if (h) {}\n"
"}\n");
TODO_ASSERT_EQUALS("[test.cpp:5]: (style) Variable 'h' can be declared as pointer to const\n", "", errout.str());
ASSERT_EQUALS("[test.cpp:5]: (style) Variable 'h' can be declared as pointer to const\n", errout.str());
check("void f(const std::vector<int*>& v) {\n"
" for (const auto& p : v)\n"
@ -3306,7 +3306,7 @@ private:
" for (const auto* p : v)\n"
" if (p == nullptr) {}\n"
"}\n");
TODO_ASSERT_EQUALS("[test.cpp:2]: (style) Variable 'p' can be declared as pointer to const\n", "", errout.str());
ASSERT_EQUALS("[test.cpp:2]: (style) Variable 'p' can be declared as pointer to const\n", errout.str());
check("void f(std::vector<int*>& v) {\n"
" for (const auto& p : v)\n"
@ -3318,10 +3318,9 @@ private:
" for (const int* p : v)\n"
" if (p == nullptr) {}\n"
"}\n");
TODO_ASSERT_EQUALS("[test.cpp:1]: (style) Parameter 'v' can be declared as reference to const\n"
"[test.cpp:2]: (style) Variable 'p' can be declared as pointer to const\n",
"[test.cpp:1]: (style) Parameter 'v' can be declared as reference to const\n",
errout.str());
ASSERT_EQUALS("[test.cpp:1]: (style) Parameter 'v' can be declared as reference to const\n"
"[test.cpp:2]: (style) Variable 'p' can be declared as pointer to const\n",
errout.str());
check("void f(std::vector<const int*>& v) {\n"
" for (const auto& p : v)\n"
@ -3511,6 +3510,18 @@ private:
" g(c);\n"
"}\n");
ASSERT_EQUALS("[test.cpp:3]: (style) Parameter 'c' can be declared as pointer to const\n", errout.str());
check("typedef void (*cb_t)(int*);\n" // #11674
"void cb(int* p) {\n"
" if (*p) {}\n"
"}\n"
"void g(cb_t);\n"
"void f() {\n"
" g(cb);\n"
"}\n");
ASSERT_EQUALS("[test.cpp:7] -> [test.cpp:2]: (style) Parameter 'p' can be declared as pointer to const. "
"However it seems that 'cb' is a callback function, if 'p' is declared with const you might also need to cast function pointer(s).\n",
errout.str());
}
void switchRedundantAssignmentTest() {