From 242afc389d2e4dbbfb9f3f2974a84df92eb31ca2 Mon Sep 17 00:00:00 2001 From: chrchr-github <78114321+chrchr-github@users.noreply.github.com> Date: Sat, 25 Jun 2022 21:42:55 +0200 Subject: [PATCH] Fix #11148 Regression: Parameter \'ptr\' can be declared as pointer to const (#4233) --- lib/checkother.cpp | 4 +++- test/testother.cpp | 7 +++++++ 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/lib/checkother.cpp b/lib/checkother.cpp index fc3cc333b..b63e5820f 100644 --- a/lib/checkother.cpp +++ b/lib/checkother.cpp @@ -1556,6 +1556,8 @@ void CheckOther::checkConstPointer() deref = true; else if (Token::simpleMatch(parent, "[") && parent->astOperand1() == tok && tok != nameTok) deref = true; + else if (Token::Match(parent, "%op%") && Token::simpleMatch(parent->astParent(), ".")) + deref = true; else if (astIsRangeBasedForDecl(tok)) continue; if (deref) { @@ -1577,7 +1579,7 @@ void CheckOther::checkConstPointer() } else if (Token::simpleMatch(gparent, "[") && gparent->astOperand2() == parent) continue; } else { - if (Token::Match(parent, "%oror%|%comp%|&&|?|!")) + if (Token::Match(parent, "%oror%|%comp%|&&|?|!|-")) continue; else if (Token::simpleMatch(parent, "(") && Token::Match(parent->astOperand1(), "if|while")) continue; diff --git a/test/testother.cpp b/test/testother.cpp index 406371886..5f81695cd 100644 --- a/test/testother.cpp +++ b/test/testother.cpp @@ -3250,6 +3250,13 @@ private: " **b = 0;\n" "}\n"); ASSERT_EQUALS("", errout.str()); + + check("ptrdiff_t f(int *p0, int *p1) {\n" // #11148 + " return p0 - p1;\n" + "}\n"); + ASSERT_EQUALS("[test.cpp:1]: (style) Parameter 'p0' can be declared as pointer to const\n" + "[test.cpp:1]: (style) Parameter 'p1' can be declared as pointer to const\n", + errout.str()); } void switchRedundantAssignmentTest() {