diff --git a/lib/checkother.cpp b/lib/checkother.cpp index 79697a2e6d7..ea9c881876b 100644 --- a/lib/checkother.cpp +++ b/lib/checkother.cpp @@ -2017,8 +2017,8 @@ void CheckOtherImpl::checkConstPointer() pointers.emplace(var); const Token* parent = tok->astParent(); enum Deref : std::uint8_t { NONE, DEREF, MEMBER } deref = NONE; - bool hasIncDecPlus = false; - if (parent && (parent->isUnaryOp("*") || (((hasIncDecPlus = parent->isIncDecOp()) || (hasIncDecPlus = (parent->str() == "+"))) && + bool hasIncDecPlusMinus = false; + if (parent && (parent->isUnaryOp("*") || (((hasIncDecPlusMinus = parent->isIncDecOp()) || (hasIncDecPlusMinus = Token::Match(parent, "[+-]"))) && parent->astParent() && parent->astParent()->isUnaryOp("*")))) deref = DEREF; else if (Token::simpleMatch(parent, "[") && parent->astOperand1() == tok && tok != nameTok) @@ -2054,7 +2054,7 @@ void CheckOtherImpl::checkConstPointer() } } } - if (hasIncDecPlus) { + if (hasIncDecPlusMinus) { parent = gparent; gparent = gparent ? gparent->astParent() : nullptr; } @@ -2089,9 +2089,11 @@ void CheckOtherImpl::checkConstPointer() } } else { int argn = -1; - if (Token::Match(parent, "%oror%|%comp%|&&|?|!|-|<<|;")) + if (Token::Match(parent, "%oror%|%comp%|&&|?|!|<<|;")) continue; - if (hasIncDecPlus && (!parent->astParent() || parent->astParent()->str() == ";")) + if (Token::simpleMatch(parent, "-") && parent->valueType() && parent->valueType()->pointer == 0) + continue; + if (hasIncDecPlusMinus && (!parent->astParent() || parent->astParent()->str() == ";")) continue; if (Token::simpleMatch(parent, "(") && Token::Match(parent->astOperand1(), "if|while")) continue; diff --git a/test/testother.cpp b/test/testother.cpp index d6025e9a640..27659086416 100644 --- a/test/testother.cpp +++ b/test/testother.cpp @@ -5087,6 +5087,15 @@ class TestOther : public TestFixture { "void g(__attribute__((unused)) int *p) {}\n" "void h(int *p) {}\n"); ASSERT_EQUALS("[test.cpp:3:13]: (style) Parameter 'p' can be declared as pointer to const [constParameterPointer]\n", errout_str()); + + check("int* f(int* s) {\n" // #15219 + " int* q = s - 1;\n" + " return q;\n" + "}\n" + "int g(int* p) {\n" + " return *(p - 1);\n" + "}\n"); + ASSERT_EQUALS("[test.cpp:5:12]: (style) Parameter 'p' can be declared as pointer to const [constParameterPointer]\n", errout_str()); } void constArray() {