From 060a27ceab292122f15d68cf94ff66290a648c8e Mon Sep 17 00:00:00 2001 From: chrchr-github <78114321+chrchr-github@users.noreply.github.com> Date: Thu, 31 Oct 2024 10:16:57 +0100 Subject: [PATCH 1/2] Update testother.cpp --- test/testother.cpp | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/test/testother.cpp b/test/testother.cpp index 68c0937620c..75421ea18a3 100644 --- a/test/testother.cpp +++ b/test/testother.cpp @@ -4313,6 +4313,18 @@ class TestOther : public TestFixture { "}\n"); ASSERT_EQUALS("[test.cpp:1]: (style) Parameter 'b' can be declared as pointer to const\n", errout_str()); + + check("struct S { int a; };\n" // #13286 + "void f(struct S* s) {\n" + " if ((--s)->a >= 0) {}\n" + "}\n" + "void g(struct S* s) {\n" + " --s;\n" + " if (s->a >= 0) {}\n" + "}\n"); + ASSERT_EQUALS("[test.cpp:2]: (style) Parameter 's' can be declared as pointer to const\n" + "[test.cpp:5]: (style) Parameter 's' can be declared as pointer to const\n", + errout_str()); } void constArray() { From 35098290454c7fa6ff23cb9e3aaa530dd9fdf73a Mon Sep 17 00:00:00 2001 From: chrchr-github <78114321+chrchr-github@users.noreply.github.com> Date: Thu, 31 Oct 2024 10:18:36 +0100 Subject: [PATCH 2/2] Update checkother.cpp --- lib/checkother.cpp | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/lib/checkother.cpp b/lib/checkother.cpp index 2678756d18c..be08b4dd5fc 100644 --- a/lib/checkother.cpp +++ b/lib/checkother.cpp @@ -1688,7 +1688,7 @@ void CheckOther::checkConstPointer() else if (Token::simpleMatch(parent, "[") && parent->astOperand1() == tok && tok != nameTok) deref = DEREF; else if (Token::Match(parent, "%op%") && Token::simpleMatch(parent->astParent(), ".")) - deref = DEREF; + deref = MEMBER; else if (Token::simpleMatch(parent, ".")) deref = MEMBER; else if (astIsRangeBasedForDecl(tok)) @@ -1707,12 +1707,12 @@ void CheckOther::checkConstPointer() continue; } } - if (Token::Match(gparent, "%cop%") && !gparent->isUnaryOp("&") && !gparent->isUnaryOp("*")) - continue; if (hasIncDecPlus) { parent = gparent; gparent = gparent ? gparent->astParent() : nullptr; } + if (Token::Match(gparent, "%cop%") && !gparent->isUnaryOp("&") && !gparent->isUnaryOp("*")) + continue; int argn = -1; if (Token::simpleMatch(gparent, "return")) { const Function* function = gparent->scope()->function; @@ -1744,6 +1744,8 @@ void CheckOther::checkConstPointer() int argn = -1; if (Token::Match(parent, "%oror%|%comp%|&&|?|!|-|<<")) continue; + if (hasIncDecPlus && !parent->astParent()) + continue; if (Token::simpleMatch(parent, "(") && Token::Match(parent->astOperand1(), "if|while")) continue; if (Token::simpleMatch(parent, "=") && parent->astOperand1() == tok)