Fix #11742 Can't reduce scope as variable references itself (#5112)

This commit is contained in:
chrchr-github 2023-06-02 23:33:17 +02:00 committed by GitHub
parent 36af9b6450
commit 055489df5a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 18 additions and 1 deletions

View File

@ -1049,8 +1049,11 @@ bool CheckOther::checkInnerScope(const Token *tok, const Variable* var, bool& us
if (Token::Match(tok, "& %varid%", var->declarationId())) // Taking address of variable if (Token::Match(tok, "& %varid%", var->declarationId())) // Taking address of variable
return false; return false;
if (Token::Match(tok, "%varid% =", var->declarationId())) if (Token::Match(tok, "%varid% =", var->declarationId())) {
if (!bFirstAssignment && var->isInit() && Token::findmatch(tok->tokAt(2), "%varid%", Token::findsimplematch(tok->tokAt(3), ";"), var->declarationId()))
return false;
bFirstAssignment = true; bFirstAssignment = true;
}
if (!bFirstAssignment && Token::Match(tok, "* %varid%", var->declarationId())) // dereferencing means access to previous content if (!bFirstAssignment && Token::Match(tok, "* %varid%", var->declarationId())) // dereferencing means access to previous content
return false; return false;

View File

@ -105,6 +105,7 @@ private:
TEST_CASE(varScope31); // #11099 TEST_CASE(varScope31); // #11099
TEST_CASE(varScope32); // #11441 TEST_CASE(varScope32); // #11441
TEST_CASE(varScope33); TEST_CASE(varScope33);
TEST_CASE(varScope34);
TEST_CASE(oldStylePointerCast); TEST_CASE(oldStylePointerCast);
TEST_CASE(invalidPointerCast); TEST_CASE(invalidPointerCast);
@ -1614,6 +1615,19 @@ private:
errout.str()); errout.str());
} }
void varScope34() { // #11742
check("void f() {\n"
" bool b = false;\n"
" int i = 1;\n"
" for (int k = 0; k < 20; ++k) {\n"
" b = !b;\n"
" if (b)\n"
" i++;\n"
" }\n"
"}\n");
ASSERT_EQUALS("", errout.str());
}
#define checkOldStylePointerCast(code) checkOldStylePointerCast_(code, __FILE__, __LINE__) #define checkOldStylePointerCast(code) checkOldStylePointerCast_(code, __FILE__, __LINE__)
void checkOldStylePointerCast_(const char code[], const char* file, int line) { void checkOldStylePointerCast_(const char code[], const char* file, int line) {
// Clear the error buffer.. // Clear the error buffer..