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) 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() {