Skip to content

Commit 9ac99f0

Browse files
Fix #14119 FN constVariablePointer for variable declared in for loop / partial fix for #14148 (#7807)
Co-authored-by: chrchr-github <[email protected]>
1 parent cd9d2de commit 9ac99f0

File tree

3 files changed

+28
-4
lines changed

3 files changed

+28
-4
lines changed

lib/checkother.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1910,7 +1910,7 @@ void CheckOther::checkConstPointer()
19101910
if (lhs && lhs->variable() && lhs->variable()->isReference() && lhs->variable()->nameToken() == lhs && !lhs->variable()->isConst())
19111911
takingRef = true;
19121912
if (lhs && lhs->valueType() && lhs->valueType()->pointer && (lhs->valueType()->constness & 1) == 0 &&
1913-
parent->valueType() && parent->valueType()->pointer)
1913+
parent->valueType() && parent->valueType()->pointer && lhs->variable() != var)
19141914
nonConstPtrAssignment = true;
19151915
if (!takingRef && !nonConstPtrAssignment)
19161916
continue;
@@ -1927,9 +1927,9 @@ void CheckOther::checkConstPointer()
19271927
}
19281928
} else {
19291929
int argn = -1;
1930-
if (Token::Match(parent, "%oror%|%comp%|&&|?|!|-|<<"))
1930+
if (Token::Match(parent, "%oror%|%comp%|&&|?|!|-|<<|;"))
19311931
continue;
1932-
if (hasIncDecPlus && !parent->astParent())
1932+
if (hasIncDecPlus && (!parent->astParent() || parent->astParent()->str() == ";"))
19331933
continue;
19341934
if (Token::simpleMatch(parent, "(") && Token::Match(parent->astOperand1(), "if|while"))
19351935
continue;

lib/token.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2680,7 +2680,7 @@ void Token::Impl::setCppcheckAttribute(CppcheckAttributesType type, MathLib::big
26802680

26812681
bool Token::Impl::getCppcheckAttribute(CppcheckAttributesType type, MathLib::bigint &value) const
26822682
{
2683-
CppcheckAttributes *attr = mCppcheckAttributes;
2683+
const CppcheckAttributes *attr = mCppcheckAttributes;
26842684
while (attr && attr->type != type)
26852685
attr = attr->next;
26862686
if (attr)

test/testother.cpp

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4511,6 +4511,30 @@ class TestOther : public TestFixture {
45114511
"}\n");
45124512
ASSERT_EQUALS("[test.cpp:4:8]: (style) Variable 'tok1' can be declared as pointer to const [constVariablePointer]\n",
45134513
errout_str());
4514+
4515+
check("struct S { S* next; };\n" // #14119
4516+
"void f(S* s) {\n"
4517+
" for (S* p = s->next; p != nullptr; p = p->next) {}\n"
4518+
"}\n");
4519+
ASSERT_EQUALS("[test.cpp:3:13]: (style) Variable 'p' can be declared as pointer to const [constVariablePointer]\n",
4520+
errout_str());
4521+
4522+
check("void f(int* p) {\n"
4523+
" for (int* q = p; q;)\n"
4524+
" break;\n"
4525+
"}\n");
4526+
ASSERT_EQUALS("[test.cpp:2:15]: (style) Variable 'q' can be declared as pointer to const [constVariablePointer]\n",
4527+
errout_str());
4528+
4529+
check("void g(const int*);\n" // #14148
4530+
"void f() {\n"
4531+
" int a[] = {1, 2, 3};\n"
4532+
" for (int* p = a; *p != 3; p++) {\n"
4533+
" g(p);\n"
4534+
" }\n"
4535+
"}\n");
4536+
ASSERT_EQUALS("[test.cpp:4:15]: (style) Variable 'p' can be declared as pointer to const [constVariablePointer]\n",
4537+
errout_str());
45144538
}
45154539

45164540
void constArray() {

0 commit comments

Comments
 (0)