Skip to content

Commit 87554be

Browse files
committed
Redundant pointer op; Fixed false positives when macro is used
1 parent 9a9f14b commit 87554be

2 files changed

Lines changed: 13 additions & 7 deletions

File tree

lib/checkother.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2674,10 +2674,10 @@ void CheckOther::checkRedundantPointerOp()
26742674
return;
26752675

26762676
for (const Token *tok = mTokenizer->tokens(); tok; tok = tok->next()) {
2677-
if (!tok->isUnaryOp("&") || !tok->astOperand1()->isUnaryOp("*"))
2678-
continue;
2677+
if (tok->isExpandedMacro() && tok->str() == "(")
2678+
tok = tok->link();
26792679

2680-
if (tok->isExpandedMacro())
2680+
if (!tok->isUnaryOp("&") || !tok->astOperand1()->isUnaryOp("*"))
26812681
continue;
26822682

26832683
// variable

test/testother.cpp

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8179,12 +8179,18 @@ class TestOther : public TestFixture {
81798179
ASSERT_EQUALS("[test.cpp:2]: (style) Redundant pointer operation on 'ptr' - it's already a pointer.\n", errout.str());
81808180

81818181
// no warning for macros
8182-
check("#define MUTEX_LOCK(m) pthread_mutex_lock(&(m))\n"
8183-
"void f(struct mutex *mut) {\n"
8184-
" MUTEX_LOCK(*mut);\n"
8185-
"}\n", nullptr, false, true);
8182+
checkP("#define MUTEX_LOCK(m) pthread_mutex_lock(&(m))\n"
8183+
"void f(struct mutex *mut) {\n"
8184+
" MUTEX_LOCK(*mut);\n"
8185+
"}\n");
81868186
ASSERT_EQUALS("", errout.str());
81878187

8188+
checkP("#define B(op) bar(op)\n"
8189+
"#define C(orf) B(&orf)\n"
8190+
"void foo(const int * pkey) {\n"
8191+
" C(*pkey);\n"
8192+
"}\n");
8193+
ASSERT_EQUALS("", errout.str());
81888194
}
81898195

81908196
void test_isSameExpression() { // see #5738

0 commit comments

Comments
 (0)