From ec4267a2bd633578c281f45be95c81b8ce254f8f Mon Sep 17 00:00:00 2001 From: chrchr-github <78114321+chrchr-github@users.noreply.github.com> Date: Wed, 7 Jun 2023 20:45:48 +0200 Subject: [PATCH] Don't suggest const for smartptr::get() (#5126) * Don't suggest const for smartptr::get() * Fix test * Fix merge * get() doesn't change a smartpointer * Parentheses --- cfg/std.cfg | 1 - lib/astutils.cpp | 2 +- test/cfg/std.cpp | 8 ++++++++ test/testclass.cpp | 2 +- 4 files changed, 10 insertions(+), 3 deletions(-) diff --git a/cfg/std.cfg b/cfg/std.cfg index 311f9bae3..ede343a4b 100644 --- a/cfg/std.cfg +++ b/cfg/std.cfg @@ -8586,7 +8586,6 @@ initializer list (7) string& replace (const_iterator i1, const_iterator i2, init false - diff --git a/lib/astutils.cpp b/lib/astutils.cpp index cf5f228e1..20305a7d3 100644 --- a/lib/astutils.cpp +++ b/lib/astutils.cpp @@ -2531,7 +2531,7 @@ bool isVariableChanged(const Token *tok, int indirect, const Settings *settings, return false; } } - if (settings && settings->library.isFunctionConst(ftok)) + if ((settings && settings->library.isFunctionConst(ftok)) || (astIsSmartPointer(tok) && ftok->str() == "get")) // TODO: replace with action/yield? return false; const Function * fun = ftok->function(); diff --git a/test/cfg/std.cpp b/test/cfg/std.cpp index 1caf7ebf3..cf26f73cb 100644 --- a/test/cfg/std.cpp +++ b/test/cfg/std.cpp @@ -4724,6 +4724,14 @@ void smartPtr_get() *p = 1; } +void smartPtr_get2(std::vector>& v) +{ + for (auto& u : v) { + int* p = u.get(); + *p = 0; + } +} + void smartPtr_reset() { std::unique_ptr p(new int()); diff --git a/test/testclass.cpp b/test/testclass.cpp index 4119f8e2f..93d85a623 100644 --- a/test/testclass.cpp +++ b/test/testclass.cpp @@ -6532,7 +6532,7 @@ private: errout.str()); checkConst("struct S {\n" - " bool f() { return p.get() != nullptr; }\n" + " bool f() { return p != nullptr; }\n" " std::shared_ptr p;\n" "};\n"); ASSERT_EQUALS("[test.cpp:2]: (style, inconclusive) Technically the member function 'S::f' can be const.\n",