Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion lib/checkother.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
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;
}

if (!bFirstAssignment && Token::Match(tok, "* %varid%", var->declarationId())) // dereferencing means access to previous content
return false;
Expand Down
14 changes: 14 additions & 0 deletions test/testother.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ class TestOther : public TestFixture {
TEST_CASE(varScope31); // #11099
TEST_CASE(varScope32); // #11441
TEST_CASE(varScope33);
TEST_CASE(varScope34);

TEST_CASE(oldStylePointerCast);
TEST_CASE(invalidPointerCast);
Expand Down Expand Up @@ -1614,6 +1615,19 @@ class TestOther : public TestFixture {
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__)
void checkOldStylePointerCast_(const char code[], const char* file, int line) {
// Clear the error buffer..
Expand Down