Fix #11225 FN constParameter with cast (#5100)

* Fix #11225 FN constParameter with cast

* Add const
This commit is contained in:
chrchr-github 2023-05-31 16:51:03 +02:00 committed by GitHub
parent 8324b75ee0
commit 7696bd1357
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 24 additions and 4 deletions

View File

@ -959,7 +959,7 @@ bool CheckBufferOverrun::analyseWholeProgram(const CTU::FileInfo *ctu, const std
const std::map<std::string, std::list<const CTU::FileInfo::CallBase *>> callsMap = ctu->getCallsMap(); const std::map<std::string, std::list<const CTU::FileInfo::CallBase *>> callsMap = ctu->getCallsMap();
for (Check::FileInfo *fi1 : fileInfo) { for (const Check::FileInfo* fi1 : fileInfo) {
const MyFileInfo *fi = dynamic_cast<const MyFileInfo*>(fi1); const MyFileInfo *fi = dynamic_cast<const MyFileInfo*>(fi1);
if (!fi) if (!fi)
continue; continue;

View File

@ -3262,7 +3262,7 @@ bool CheckClass::analyseWholeProgram(const CTU::FileInfo *ctu, const std::list<C
std::unordered_map<std::string, MyFileInfo::NameLoc> all; std::unordered_map<std::string, MyFileInfo::NameLoc> all;
for (Check::FileInfo *fi1 : fileInfo) { for (const Check::FileInfo* fi1 : fileInfo) {
const MyFileInfo *fi = dynamic_cast<const MyFileInfo*>(fi1); const MyFileInfo *fi = dynamic_cast<const MyFileInfo*>(fi1);
if (!fi) if (!fi)
continue; continue;

View File

@ -596,7 +596,7 @@ bool CheckNullPointer::analyseWholeProgram(const CTU::FileInfo *ctu, const std::
const std::map<std::string, std::list<const CTU::FileInfo::CallBase *>> callsMap = ctu->getCallsMap(); const std::map<std::string, std::list<const CTU::FileInfo::CallBase *>> callsMap = ctu->getCallsMap();
for (Check::FileInfo *fi1 : fileInfo) { for (const Check::FileInfo* fi1 : fileInfo) {
const MyFileInfo *fi = dynamic_cast<const MyFileInfo*>(fi1); const MyFileInfo *fi = dynamic_cast<const MyFileInfo*>(fi1);
if (!fi) if (!fi)
continue; continue;

View File

@ -1641,6 +1641,10 @@ void CheckOther::checkConstPointer()
} }
} }
} }
else if (Token::simpleMatch(parent, "(")) {
if (parent->isCast() && parent->valueType() && var->valueType() && parent->valueType()->isConst(var->valueType()->pointer))
continue;
}
} }
nonConstPointers.emplace_back(var); nonConstPointers.emplace_back(var);
} }

View File

@ -1698,7 +1698,7 @@ bool CheckUninitVar::analyseWholeProgram(const CTU::FileInfo *ctu, const std::li
const std::map<std::string, std::list<const CTU::FileInfo::CallBase *>> callsMap = ctu->getCallsMap(); const std::map<std::string, std::list<const CTU::FileInfo::CallBase *>> callsMap = ctu->getCallsMap();
for (Check::FileInfo *fi1 : fileInfo) { for (const Check::FileInfo* fi1 : fileInfo) {
const MyFileInfo *fi = dynamic_cast<const MyFileInfo*>(fi1); const MyFileInfo *fi = dynamic_cast<const MyFileInfo*>(fi1);
if (!fi) if (!fi)
continue; continue;

View File

@ -3173,6 +3173,22 @@ private:
" }\n" " }\n"
"};\n"); "};\n");
ASSERT_EQUALS("", errout.str()); ASSERT_EQUALS("", errout.str());
check("struct A {\n" // #11225
" A();\n"
" virtual ~A();\n"
"};\n"
"struct B : A {};\n"
"void f(A* a) {\n"
" const B* b = dynamic_cast<const B*>(a);\n"
"}\n"
"void g(A* a) {\n"
" const B* b = (const B*)a;\n"
"}\n");
ASSERT_EQUALS("[test.cpp:10]: (style) C-style pointer casting\n"
"[test.cpp:6]: (style) Parameter 'a' can be declared as pointer to const\n"
"[test.cpp:9]: (style) Parameter 'a' can be declared as pointer to const\n",
errout.str());
} }
void constParameterCallback() { void constParameterCallback() {