Skip to content

Commit 753501c

Browse files
committed
Fix #14119 FN constVariablePointer for variable declared in for loop
1 parent 633b4e4 commit 753501c

2 files changed

Lines changed: 16 additions & 2 deletions

File tree

lib/checkother.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1893,7 +1893,7 @@ void CheckOther::checkConstPointer()
18931893
if (lhs && lhs->variable() && lhs->variable()->isReference() && lhs->variable()->nameToken() == lhs && !lhs->variable()->isConst())
18941894
takingRef = true;
18951895
if (lhs && lhs->valueType() && lhs->valueType()->pointer && (lhs->valueType()->constness & 1) == 0 &&
1896-
parent->valueType() && parent->valueType()->pointer)
1896+
parent->valueType() && parent->valueType()->pointer && lhs->variable() != var)
18971897
nonConstPtrAssignment = true;
18981898
if (!takingRef && !nonConstPtrAssignment)
18991899
continue;
@@ -1910,7 +1910,7 @@ void CheckOther::checkConstPointer()
19101910
}
19111911
} else {
19121912
int argn = -1;
1913-
if (Token::Match(parent, "%oror%|%comp%|&&|?|!|-|<<"))
1913+
if (Token::Match(parent, "%oror%|%comp%|&&|?|!|-|<<|;"))
19141914
continue;
19151915
if (hasIncDecPlus && !parent->astParent())
19161916
continue;

test/testother.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4497,6 +4497,20 @@ class TestOther : public TestFixture {
44974497
ASSERT_EQUALS("[test.cpp:2:18]: (style) Parameter 's' can be declared as pointer to const [constParameterPointer]\n"
44984498
"[test.cpp:5:18]: (style) Parameter 's' can be declared as pointer to const [constParameterPointer]\n",
44994499
errout_str());
4500+
4501+
check("struct S { S* next; };\n" // #14119
4502+
"void f(S* s) {\n"
4503+
" for (S* p = s->next; p != nullptr; p = p->next) {}\n"
4504+
"}\n");
4505+
ASSERT_EQUALS("[test.cpp:3:13]: (style) Variable 'p' can be declared as pointer to const [constVariablePointer]\n",
4506+
errout_str());
4507+
4508+
check("void f(int* p) {\n"
4509+
" for (int* q = p; q;)\n"
4510+
" break;\n"
4511+
"}\n");
4512+
ASSERT_EQUALS("[test.cpp:2:15]: (style) Variable 'q' can be declared as pointer to const [constVariablePointer]\n",
4513+
errout_str());
45004514
}
45014515

45024516
void constArray() {

0 commit comments

Comments
 (0)