diff --git a/lib/checkother.cpp b/lib/checkother.cpp index 440fde971f8..d07abbb6852 100644 --- a/lib/checkother.cpp +++ b/lib/checkother.cpp @@ -1910,7 +1910,7 @@ void CheckOther::checkConstPointer() if (lhs && lhs->variable() && lhs->variable()->isReference() && lhs->variable()->nameToken() == lhs && !lhs->variable()->isConst()) takingRef = true; if (lhs && lhs->valueType() && lhs->valueType()->pointer && (lhs->valueType()->constness & 1) == 0 && - parent->valueType() && parent->valueType()->pointer) + parent->valueType() && parent->valueType()->pointer && lhs->variable() != var) nonConstPtrAssignment = true; if (!takingRef && !nonConstPtrAssignment) continue; @@ -1927,9 +1927,9 @@ void CheckOther::checkConstPointer() } } else { int argn = -1; - if (Token::Match(parent, "%oror%|%comp%|&&|?|!|-|<<")) + if (Token::Match(parent, "%oror%|%comp%|&&|?|!|-|<<|;")) continue; - if (hasIncDecPlus && !parent->astParent()) + if (hasIncDecPlus && (!parent->astParent() || parent->astParent()->str() == ";")) continue; if (Token::simpleMatch(parent, "(") && Token::Match(parent->astOperand1(), "if|while")) continue; diff --git a/lib/token.cpp b/lib/token.cpp index 35b5a9141b8..94fd40a4633 100644 --- a/lib/token.cpp +++ b/lib/token.cpp @@ -2680,7 +2680,7 @@ void Token::Impl::setCppcheckAttribute(CppcheckAttributesType type, MathLib::big bool Token::Impl::getCppcheckAttribute(CppcheckAttributesType type, MathLib::bigint &value) const { - CppcheckAttributes *attr = mCppcheckAttributes; + const CppcheckAttributes *attr = mCppcheckAttributes; while (attr && attr->type != type) attr = attr->next; if (attr) diff --git a/test/testother.cpp b/test/testother.cpp index 9d0c9ccc12c..c9be6eeb319 100644 --- a/test/testother.cpp +++ b/test/testother.cpp @@ -4511,6 +4511,30 @@ class TestOther : public TestFixture { "}\n"); ASSERT_EQUALS("[test.cpp:4:8]: (style) Variable 'tok1' can be declared as pointer to const [constVariablePointer]\n", errout_str()); + + check("struct S { S* next; };\n" // #14119 + "void f(S* s) {\n" + " for (S* p = s->next; p != nullptr; p = p->next) {}\n" + "}\n"); + ASSERT_EQUALS("[test.cpp:3:13]: (style) Variable 'p' can be declared as pointer to const [constVariablePointer]\n", + errout_str()); + + check("void f(int* p) {\n" + " for (int* q = p; q;)\n" + " break;\n" + "}\n"); + ASSERT_EQUALS("[test.cpp:2:15]: (style) Variable 'q' can be declared as pointer to const [constVariablePointer]\n", + errout_str()); + + check("void g(const int*);\n" // #14148 + "void f() {\n" + " int a[] = {1, 2, 3};\n" + " for (int* p = a; *p != 3; p++) {\n" + " g(p);\n" + " }\n" + "}\n"); + ASSERT_EQUALS("[test.cpp:4:15]: (style) Variable 'p' can be declared as pointer to const [constVariablePointer]\n", + errout_str()); } void constArray() {