From 831aec5c03db1d8e936a48982871dc44b08aeb1a Mon Sep 17 00:00:00 2001 From: chrchr-github <78114321+chrchr-github@users.noreply.github.com> Date: Thu, 16 Nov 2023 17:43:20 +0100 Subject: [PATCH] Fix #12185 FN constVariablePointer with stream (#5659) --- lib/checkother.cpp | 2 +- test/cfg/opencv2.cpp | 2 +- test/testother.cpp | 9 ++++++++- 3 files changed, 10 insertions(+), 3 deletions(-) diff --git a/lib/checkother.cpp b/lib/checkother.cpp index abb323ed0d1..18407800aa3 100644 --- a/lib/checkother.cpp +++ b/lib/checkother.cpp @@ -1580,7 +1580,7 @@ void CheckOther::checkConstPointer() } } else { int argn = -1; - if (Token::Match(parent, "%oror%|%comp%|&&|?|!|-")) + if (Token::Match(parent, "%oror%|%comp%|&&|?|!|-|<<")) continue; if (Token::simpleMatch(parent, "(") && Token::Match(parent->astOperand1(), "if|while")) continue; diff --git a/test/cfg/opencv2.cpp b/test/cfg/opencv2.cpp index 577dd77f2ea..349dc42b2d4 100644 --- a/test/cfg/opencv2.cpp +++ b/test/cfg/opencv2.cpp @@ -39,7 +39,7 @@ void ignoredReturnValue() void memleak() { - char * pBuf = (char *)cv::fastMalloc(1000); // cppcheck-suppress cstyleCast + const char * pBuf = (char *)cv::fastMalloc(1000); // cppcheck-suppress cstyleCast std::cout << pBuf; // cppcheck-suppress memleak } diff --git a/test/testother.cpp b/test/testother.cpp index 3997d333a8e..b5dc0432eb3 100644 --- a/test/testother.cpp +++ b/test/testother.cpp @@ -3882,6 +3882,13 @@ class TestOther : public TestFixture { 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()); + + check("void f(int i) {\n" // #12185 + " void* p = &i;\n" + " std::cout << p << '\\n';\n" + "}\n"); + ASSERT_EQUALS("[test.cpp:2]: (style) Variable 'p' can be declared as pointer to const\n", + errout.str()); } void switchRedundantAssignmentTest() { @@ -8436,7 +8443,7 @@ class TestOther : public TestFixture { " ptr = otherPtr;\n" " free(otherPtr - xx - 1);\n" "}"); - ASSERT_EQUALS("", errout.str()); + ASSERT_EQUALS("[test.cpp:2]: (style) Variable 'ptr' can be declared as pointer to const\n", errout.str()); } void checkRedundantCopy() {