Skip to content

Commit 7a91746

Browse files
Fix #14714, #14748 FN constParameterPointer (array member, dereference in ternary) (#8553)
Co-authored-by: chrchr-github <noreply@github.com>
1 parent e277aba commit 7a91746

2 files changed

Lines changed: 23 additions & 1 deletion

File tree

lib/checkother.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1941,7 +1941,9 @@ void CheckOther::checkConstPointer()
19411941
continue;
19421942
if (deref != NONE) {
19431943
const Token* gparent = parent->astParent();
1944-
while (Token::simpleMatch(gparent, "[") && parent != gparent->astOperand2() && parent->str() == gparent->str())
1944+
while (Token::simpleMatch(gparent, "[") && parent != gparent->astOperand2())
1945+
gparent = gparent->astParent();
1946+
while (Token::Match(gparent, "[?:]"))
19451947
gparent = gparent->astParent();
19461948
if (deref == MEMBER) {
19471949
if (!gparent)

test/testother.cpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4844,6 +4844,26 @@ class TestOther : public TestFixture {
48444844
" return s->x ? 1 : 0;\n"
48454845
"}\n");
48464846
ASSERT_EQUALS("[test.cpp:2:10]: (style) Parameter 's' can be declared as pointer to const [constParameterPointer]\n", errout_str());
4847+
4848+
check("struct S { int a[1][1]; };\n" // #14714
4849+
"int f(S* s) {\n"
4850+
" return s->a[0][0] ? 1 : 0;\n"
4851+
"}\n");
4852+
ASSERT_EQUALS("[test.cpp:2:10]: (style) Parameter 's' can be declared as pointer to const [constParameterPointer]\n", errout_str());
4853+
4854+
check("int f(int *p, int *q) {\n" // #14748
4855+
" return p ? *p : *q;\n"
4856+
"}\n"
4857+
"void g(int *p, int *q) {\n"
4858+
" int& r = p ? *p : *q;\n"
4859+
" r = 0;\n"
4860+
"}\n"
4861+
"void h(int *p, int *q) {\n"
4862+
" i(p ? *p : *q);\n"
4863+
"}\n");
4864+
ASSERT_EQUALS("[test.cpp:1:12]: (style) Parameter 'p' can be declared as pointer to const [constParameterPointer]\n"
4865+
"[test.cpp:1:20]: (style) Parameter 'q' can be declared as pointer to const [constParameterPointer]\n",
4866+
errout_str());
48474867
}
48484868

48494869
void constArray() {

0 commit comments

Comments
 (0)