From 29001b651b95c290a301525f24064ec82c80460b Mon Sep 17 00:00:00 2001 From: chrchr-github <78114321+chrchr-github@users.noreply.github.com> Date: Wed, 1 Nov 2023 09:49:19 +0100 Subject: [PATCH] Fix #12129 FN (regression): constParameterPointer (#5605) --- lib/checkother.cpp | 5 ++--- lib/tokenlist.cpp | 14 +++++++++----- test/testother.cpp | 7 +++++++ 3 files changed, 18 insertions(+), 8 deletions(-) diff --git a/lib/checkother.cpp b/lib/checkother.cpp index 3cd33d2e5..7679274f4 100644 --- a/lib/checkother.cpp +++ b/lib/checkother.cpp @@ -1613,14 +1613,13 @@ void CheckOther::checkConstPointer() const int indirect = p->isArray() ? p->dimensions().size() : 1; if (isVariableChanged(start, p->scope()->bodyEnd, indirect, p->declarationId(), false, mSettings, mTokenizer->isCPP())) continue; - if (p->isArgument() && p->typeStartToken() && p->typeStartToken()->isSimplifiedTypedef() && !(Token::simpleMatch(p->typeEndToken(), "*") && !p->typeEndToken()->isSimplifiedTypedef())) - continue; - if (p->typeStartToken() && !p->typeStartToken()->originalName().empty()) + if (p->typeStartToken() && p->typeStartToken()->isSimplifiedTypedef() && !(Token::simpleMatch(p->typeEndToken(), "*") && !p->typeEndToken()->isSimplifiedTypedef())) continue; constVariableError(p, p->isArgument() ? p->scope()->function : nullptr); } } } + void CheckOther::constVariableError(const Variable *var, const Function *function) { if (!var) { diff --git a/lib/tokenlist.cpp b/lib/tokenlist.cpp index 1d27dc810..1e90b9406 100644 --- a/lib/tokenlist.cpp +++ b/lib/tokenlist.cpp @@ -1926,20 +1926,24 @@ void TokenList::simplifyPlatformTypes() Token *typeToken; if (platformtype->mConstPtr) { tok->str("const"); - tok->insertToken("*"); - tok->insertToken(platformtype->mType); + tok->isSimplifiedTypedef(true); + tok->insertToken("*")->isSimplifiedTypedef(true); + tok->insertToken(platformtype->mType)->isSimplifiedTypedef(true); typeToken = tok; } else if (platformtype->mPointer) { tok->str(platformtype->mType); + tok->isSimplifiedTypedef(true); typeToken = tok; - tok->insertToken("*"); + tok->insertToken("*")->isSimplifiedTypedef(true); } else if (platformtype->mPtrPtr) { tok->str(platformtype->mType); + tok->isSimplifiedTypedef(true); typeToken = tok; - tok->insertToken("*"); - tok->insertToken("*"); + tok->insertToken("*")->isSimplifiedTypedef(true); + tok->insertToken("*")->isSimplifiedTypedef(true); } else { tok->str(platformtype->mType); + tok->isSimplifiedTypedef(true); typeToken = tok; } if (platformtype->mSigned) diff --git a/test/testother.cpp b/test/testother.cpp index 420014708..62173127c 100644 --- a/test/testother.cpp +++ b/test/testother.cpp @@ -3837,6 +3837,13 @@ private: " qsort(p, nmemb, size, cmp);\n" "}\n"); ASSERT_EQUALS("", errout.str()); + + check("void g(bool *r, std::size_t *b) {\n" // #12129 + " if (*r && *b >= 5) {}\n" + "}\n"); + ASSERT_EQUALS("[test.cpp:1]: (style) Parameter 'r' can be declared as pointer to const\n" + "[test.cpp:1]: (style) Parameter 'b' can be declared as pointer to const\n", + errout.str()); } void switchRedundantAssignmentTest() {