From 7696bd13574c7859e8f011da3c3d74ed96c1e654 Mon Sep 17 00:00:00 2001 From: chrchr-github <78114321+chrchr-github@users.noreply.github.com> Date: Wed, 31 May 2023 16:51:03 +0200 Subject: [PATCH] Fix #11225 FN constParameter with cast (#5100) * Fix #11225 FN constParameter with cast * Add const --- lib/checkbufferoverrun.cpp | 2 +- lib/checkclass.cpp | 2 +- lib/checknullpointer.cpp | 2 +- lib/checkother.cpp | 4 ++++ lib/checkuninitvar.cpp | 2 +- test/testother.cpp | 16 ++++++++++++++++ 6 files changed, 24 insertions(+), 4 deletions(-) diff --git a/lib/checkbufferoverrun.cpp b/lib/checkbufferoverrun.cpp index ee8f1506d..ce3f69d73 100644 --- a/lib/checkbufferoverrun.cpp +++ b/lib/checkbufferoverrun.cpp @@ -959,7 +959,7 @@ bool CheckBufferOverrun::analyseWholeProgram(const CTU::FileInfo *ctu, const std const std::map> callsMap = ctu->getCallsMap(); - for (Check::FileInfo *fi1 : fileInfo) { + for (const Check::FileInfo* fi1 : fileInfo) { const MyFileInfo *fi = dynamic_cast(fi1); if (!fi) continue; diff --git a/lib/checkclass.cpp b/lib/checkclass.cpp index 3afef417d..85fd349a6 100644 --- a/lib/checkclass.cpp +++ b/lib/checkclass.cpp @@ -3262,7 +3262,7 @@ bool CheckClass::analyseWholeProgram(const CTU::FileInfo *ctu, const std::list all; - for (Check::FileInfo *fi1 : fileInfo) { + for (const Check::FileInfo* fi1 : fileInfo) { const MyFileInfo *fi = dynamic_cast(fi1); if (!fi) continue; diff --git a/lib/checknullpointer.cpp b/lib/checknullpointer.cpp index 5b615c959..2f0ee2742 100644 --- a/lib/checknullpointer.cpp +++ b/lib/checknullpointer.cpp @@ -596,7 +596,7 @@ bool CheckNullPointer::analyseWholeProgram(const CTU::FileInfo *ctu, const std:: const std::map> callsMap = ctu->getCallsMap(); - for (Check::FileInfo *fi1 : fileInfo) { + for (const Check::FileInfo* fi1 : fileInfo) { const MyFileInfo *fi = dynamic_cast(fi1); if (!fi) continue; diff --git a/lib/checkother.cpp b/lib/checkother.cpp index 9f6334771..73cfe8e42 100644 --- a/lib/checkother.cpp +++ b/lib/checkother.cpp @@ -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); } diff --git a/lib/checkuninitvar.cpp b/lib/checkuninitvar.cpp index 786c528d1..4ed0a6d7e 100644 --- a/lib/checkuninitvar.cpp +++ b/lib/checkuninitvar.cpp @@ -1698,7 +1698,7 @@ bool CheckUninitVar::analyseWholeProgram(const CTU::FileInfo *ctu, const std::li const std::map> callsMap = ctu->getCallsMap(); - for (Check::FileInfo *fi1 : fileInfo) { + for (const Check::FileInfo* fi1 : fileInfo) { const MyFileInfo *fi = dynamic_cast(fi1); if (!fi) continue; diff --git a/test/testother.cpp b/test/testother.cpp index 5b23e951f..9e54183b9 100644 --- a/test/testother.cpp +++ b/test/testother.cpp @@ -3173,6 +3173,22 @@ private: " }\n" "};\n"); 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(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() {