From 87554bed7abea1e1245221eee2ab2ca4bac1c5de Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Sat, 22 May 2021 14:18:29 +0200 Subject: [PATCH] Redundant pointer op; Fixed false positives when macro is used --- lib/checkother.cpp | 6 +++--- test/testother.cpp | 14 ++++++++++---- 2 files changed, 13 insertions(+), 7 deletions(-) diff --git a/lib/checkother.cpp b/lib/checkother.cpp index 7e3f56a7d..49362ce4d 100644 --- a/lib/checkother.cpp +++ b/lib/checkother.cpp @@ -2674,10 +2674,10 @@ void CheckOther::checkRedundantPointerOp() return; for (const Token *tok = mTokenizer->tokens(); tok; tok = tok->next()) { - if (!tok->isUnaryOp("&") || !tok->astOperand1()->isUnaryOp("*")) - continue; + if (tok->isExpandedMacro() && tok->str() == "(") + tok = tok->link(); - if (tok->isExpandedMacro()) + if (!tok->isUnaryOp("&") || !tok->astOperand1()->isUnaryOp("*")) continue; // variable diff --git a/test/testother.cpp b/test/testother.cpp index e5c13ae29..d1d2c33b1 100644 --- a/test/testother.cpp +++ b/test/testother.cpp @@ -8179,12 +8179,18 @@ private: ASSERT_EQUALS("[test.cpp:2]: (style) Redundant pointer operation on 'ptr' - it's already a pointer.\n", errout.str()); // no warning for macros - check("#define MUTEX_LOCK(m) pthread_mutex_lock(&(m))\n" - "void f(struct mutex *mut) {\n" - " MUTEX_LOCK(*mut);\n" - "}\n", nullptr, false, true); + checkP("#define MUTEX_LOCK(m) pthread_mutex_lock(&(m))\n" + "void f(struct mutex *mut) {\n" + " MUTEX_LOCK(*mut);\n" + "}\n"); ASSERT_EQUALS("", errout.str()); + checkP("#define B(op) bar(op)\n" + "#define C(orf) B(&orf)\n" + "void foo(const int * pkey) {\n" + " C(*pkey);\n" + "}\n"); + ASSERT_EQUALS("", errout.str()); } void test_isSameExpression() { // see #5738