Add cfg for std::scoped_lock, handle template arguments in checkMisusedScopedObject() (#4615)

This commit is contained in:
chrchr-github 2022-12-07 09:10:48 +01:00 committed by GitHub
parent 7d3ce62ee9
commit d5d7446433
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 16 additions and 4 deletions

View File

@ -8672,6 +8672,7 @@ initializer list (7) string& replace (const_iterator i1, const_iterator i2, init
<suppress>std::basic_ofstream</suppress>
<checkFiniteLifetime>std::insert_iterator</checkFiniteLifetime>
<checkFiniteLifetime>std::lock_guard</checkFiniteLifetime>
<checkFiniteLifetime>std::scoped_lock</checkFiniteLifetime>
<checkFiniteLifetime>std::unique_lock</checkFiniteLifetime>
<checkFiniteLifetime>std::shared_lock</checkFiniteLifetime>
<check>std::pair</check>

View File

@ -2105,11 +2105,14 @@ void CheckOther::checkMisusedScopedObject()
if (ctorTok && (((ctorTok->type() || ctorTok->isStandardType() || (ctorTok->function() && ctorTok->function()->isConstructor())) // TODO: The rhs of || should be removed; It is a workaround for a symboldatabase bug
&& (!ctorTok->function() || ctorTok->function()->isConstructor()) // // is not a function on this scope or is function in this scope and it's a ctor
&& ctorTok->str() != "void") || isLibraryConstructor(tok->next(), typeStr))) {
if (const Token* arg = ctorTok->next()->astOperand2()) {
const Token* parTok = ctorTok->next();
if (Token::simpleMatch(parTok, "<") && parTok->link())
parTok = parTok->link()->next();
if (const Token* arg = parTok->astOperand2()) {
if (!isConstStatement(arg, mTokenizer->isCPP()))
continue;
if (ctorTok->strAt(1) == "(") {
if (arg->varId()) // TODO: check if this is a declaration
if (parTok->str() == "(") {
if (arg->varId() && !(arg->variable() && arg->variable()->nameToken() != arg))
continue;
const Token* rml = nextAfterAstRightmostLeaf(arg);
if (rml && rml->previous() && rml->previous()->varId())

View File

@ -5140,9 +5140,17 @@ private:
" void f() {\n"
" std::lock_guard<std::mutex>(m);\n"
" }\n"
" void g() {\n"
" std::scoped_lock<std::mutex>(m);\n"
" }\n"
" void h() {\n"
" std::scoped_lock(m);\n"
" }\n"
" std::mutex m;\n"
"}\n", "test.cpp");
ASSERT_EQUALS("[test.cpp:3]: (style) Instance of 'std::lock_guard' object is destroyed immediately.\n",
ASSERT_EQUALS("[test.cpp:3]: (style) Instance of 'std::lock_guard' object is destroyed immediately.\n"
"[test.cpp:6]: (style) Instance of 'std::scoped_lock' object is destroyed immediately.\n"
"[test.cpp:9]: (style) Instance of 'std::scoped_lock' object is destroyed immediately.\n",
errout.str());
}