From 14ffdab45523cc7bfd7d71abd24b2be398f7ad19 Mon Sep 17 00:00:00 2001 From: chrchr Date: Thu, 14 Mar 2024 18:51:39 +0100 Subject: [PATCH 1/2] Fix #12518 FN constParameterPointer with value cast --- lib/checkother.cpp | 4 ++++ test/testother.cpp | 11 +++++++++++ 2 files changed, 15 insertions(+) diff --git a/lib/checkother.cpp b/lib/checkother.cpp index dcc6b444665..05ac6fc502d 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->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() { From 7b6327948f16a3725263f0b6051fd538380519ca Mon Sep 17 00:00:00 2001 From: chrchr Date: Thu, 14 Mar 2024 18:59:00 +0100 Subject: [PATCH 2/2] nullptr check --- lib/checkother.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/checkother.cpp b/lib/checkother.cpp index 05ac6fc502d..cce598aaf7c 100644 --- a/lib/checkother.cpp +++ b/lib/checkother.cpp @@ -1591,7 +1591,7 @@ void CheckOther::checkConstPointer() continue; } else if (Token::simpleMatch(gparent, "[") && gparent->astOperand2() == parent) continue; - else if (gparent->isCast() && gparent->valueType() && + 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;