Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions lib/checkother.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion lib/token.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
24 changes: 24 additions & 0 deletions test/testother.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down