Fix unusedScopedObject FPs (#4364)

This commit is contained in:
chrchr-github 2022-08-17 09:11:23 +02:00 committed by GitHub
parent 98b9f2cbf1
commit 1a95515e47
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 23 additions and 1 deletions

View File

@ -2062,7 +2062,18 @@ void CheckOther::checkMisusedScopedObject()
&& Token::Match(tok->linkAt(2), ")|} ; !!}")
&& (!tok->next()->function() || // is not a function on this scope
tok->next()->function()->isConstructor()) // or is function in this scope and it's a ctor
&& (!tok->tokAt(2)->astOperand2() || isConstStatement(tok->tokAt(2)->astOperand2(), mTokenizer->isCPP()))) {
&& !Token::simpleMatch(tok->tokAt(2)->astParent(), ";")) { // for loop condition
if (const Token* arg = tok->tokAt(2)->astOperand2()) {
if (!isConstStatement(arg, mTokenizer->isCPP()))
continue;
if (tok->strAt(2) == "(") {
if (arg->varId()) // TODO: check if this is a declaration
continue;
const Token* rml = nextAfterAstRightmostLeaf(arg);
if (rml && rml->previous() && rml->previous()->varId())
continue;
}
}
tok = tok->next();
misusedScopeObjectError(tok, tok->str());
tok = tok->next();

View File

@ -5049,6 +5049,17 @@ private:
"[test.cpp:7]: (style) Instance of 'int' object is destroyed immediately.\n"
"[test.cpp:8]: (style) Instance of 'int' object is destroyed immediately.\n",
errout.str());
check("void f(int j) {\n"
" for (; bool(j); ) {}\n"
"}\n", "test.cpp");
ASSERT_EQUALS("", errout.str());
check("void g() {\n"
" float (f);\n"
" float (*p);\n"
"}\n", "test.cpp");
ASSERT_EQUALS("", errout.str());
}
void trac2084() {