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
This commit is contained in:
chrchr-github 2023-06-07 20:45:48 +02:00 committed by GitHub
parent 12fe652321
commit ec4267a2bd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 10 additions and 3 deletions

View File

@ -8586,7 +8586,6 @@ initializer list (7) string& replace (const_iterator i1, const_iterator i2, init
<use-retval/> <use-retval/>
<leak-ignore/> <leak-ignore/>
<noreturn>false</noreturn> <noreturn>false</noreturn>
<const/>
<returnValue type="void *"/> <returnValue type="void *"/>
</function> </function>
<function name="std::auto_ptr::reset,std::shared_ptr::reset,std::unique_ptr::reset,std::weak_ptr::reset"> <function name="std::auto_ptr::reset,std::shared_ptr::reset,std::unique_ptr::reset,std::weak_ptr::reset">

View File

@ -2531,7 +2531,7 @@ bool isVariableChanged(const Token *tok, int indirect, const Settings *settings,
return false; 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; return false;
const Function * fun = ftok->function(); const Function * fun = ftok->function();

View File

@ -4724,6 +4724,14 @@ void smartPtr_get()
*p = 1; *p = 1;
} }
void smartPtr_get2(std::vector<std::unique_ptr<int>>& v)
{
for (auto& u : v) {
int* p = u.get();
*p = 0;
}
}
void smartPtr_reset() void smartPtr_reset()
{ {
std::unique_ptr<int> p(new int()); std::unique_ptr<int> p(new int());

View File

@ -6532,7 +6532,7 @@ private:
errout.str()); errout.str());
checkConst("struct S {\n" checkConst("struct S {\n"
" bool f() { return p.get() != nullptr; }\n" " bool f() { return p != nullptr; }\n"
" std::shared_ptr<int> p;\n" " std::shared_ptr<int> p;\n"
"};\n"); "};\n");
ASSERT_EQUALS("[test.cpp:2]: (style, inconclusive) Technically the member function 'S::f' can be const.\n", ASSERT_EQUALS("[test.cpp:2]: (style, inconclusive) Technically the member function 'S::f' can be const.\n",