diff --git a/lib/checkother.cpp b/lib/checkother.cpp index dcc6b444665..cce598aaf7c 100644 --- a/lib/checkother.cpp +++ b/lib/checkother.cpp @@ -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) diff --git a/test/testother.cpp b/test/testother.cpp index 8c556a27aa1..b7031723301 100644 --- a/test/testother.cpp +++ b/test/testother.cpp @@ -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(*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() {