Skip to content

Commit

Permalink
Fix #12129 FN (regression): constParameterPointer (danmar#5605)
Browse files Browse the repository at this point in the history
  • Loading branch information
chrchr-github authored Nov 1, 2023
1 parent 67b61a0 commit 29001b6
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 8 deletions.
5 changes: 2 additions & 3 deletions lib/checkother.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
14 changes: 9 additions & 5 deletions lib/tokenlist.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
7 changes: 7 additions & 0 deletions test/testother.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3837,6 +3837,13 @@ class TestOther : public TestFixture {
" 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() {
Expand Down

0 comments on commit 29001b6

Please sign in to comment.