FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

Fix #14119 FN constVariablePointer for variable declared in for loop / partial fix for #14148 by chrchr-github · Pull Request #7807 · cppcheck-opensource/cppcheck · GitHub

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

Filter by extension

Filter by extension .cpp  (3) All 1 file type selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
6 changes: 3 additions & 3 deletions lib/checkother.cpp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
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;
Comment thread
chrchr-github marked this conversation as resolved.
while (attr && attr->type != type)
attr = attr->next;
if (attr)
Expand Down
24 changes: 24 additions & 0 deletions test/testother.cpp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
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

Back | FazBrowse Home | New Git URL