Skip to content

Commit

Permalink
Fix #12518 FN constParameterPointer with value cast (#6130)
Browse files Browse the repository at this point in the history
  • Loading branch information
chrchr-github committed Mar 17, 2024
1 parent 4b9724b commit e2b4b3c
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 0 deletions.
4 changes: 4 additions & 0 deletions lib/checkother.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1591,6 +1591,10 @@ void CheckOther::checkConstPointer()
continue;
} else if (Token::simpleMatch(gparent, "[") && gparent->astOperand2() == parent)
continue;
else if (gparent && gparent->isCast() && gparent->valueType() &&
((gparent->valueType()->pointer == 0 && gparent->valueType()->reference == Reference::None) ||
(var->valueType() && parent->valueType()->isConst(var->valueType()->pointer))))
continue;
else if (const Token* ftok = getTokenArgumentFunction(parent, argn)) {
bool inconclusive{};
if (!isVariableChangedByFunctionCall(ftok->next(), vt->pointer, var->declarationId(), mSettings, &inconclusive) && !inconclusive)
Expand Down
11 changes: 11 additions & 0 deletions test/testother.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4086,6 +4086,17 @@ class TestOther : public TestFixture {
"}\n");
ASSERT_EQUALS("[test.cpp:1]: (style) Parameter 'p' can be declared as pointer to const\n",
errout_str());

check("void f(int *src, int* dst) {\n" // #12518
" *dst++ = (int)*src++;\n"
" *dst++ = static_cast<int>(*src++);\n"
" *dst = (int)*src;\n"
"}\n"
"void g(int* dst) {\n"
" (int&)*dst = 5;\n"
"}\n");
ASSERT_EQUALS("[test.cpp:1]: (style) Parameter 'src' can be declared as pointer to const\n",
errout_str());
}

void switchRedundantAssignmentTest() {
Expand Down

0 comments on commit e2b4b3c

Please sign in to comment.