diff --git a/lib/checkother.cpp b/lib/checkother.cpp index e221accc782..6c2040c72ee 100644 --- a/lib/checkother.cpp +++ b/lib/checkother.cpp @@ -2007,8 +2007,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) @@ -2044,7 +2044,7 @@ void CheckOtherImpl::checkConstPointer() } } } - if (hasIncDecPlus) { + if (hasIncDecPlusMinus) { parent = gparent; gparent = gparent ? gparent->astParent() : nullptr; } @@ -2079,9 +2079,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 085b39cfffb..dbd1eb521fd 100644 --- a/test/testother.cpp +++ b/test/testother.cpp @@ -5096,6 +5096,15 @@ class TestOther : public TestFixture { ASSERT_EQUALS("[test.cpp:3:8]: (style) Parameter 'p' can be declared as pointer to const [constParameterPointer]\n" "[test.cpp:3:8]: (style) Parameter 'q' 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() {