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
11 changes: 1 addition & 10 deletions lib/checkother.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -995,16 +995,7 @@ void CheckOther::checkVariableScope()

// parse else if blocks..
} else if (Token::simpleMatch(tok, "else { if (") && Token::simpleMatch(tok->linkAt(3), ") {")) {
const Token *endif = tok->linkAt(3)->linkAt(1);
bool elseif = false;
if (Token::simpleMatch(endif, "} }"))
elseif = true;
else if (Token::simpleMatch(endif, "} else {") && Token::simpleMatch(endif->linkAt(2),"} }"))
elseif = true;
if (elseif && Token::findmatch(tok->next(), "%varid%", tok->linkAt(1), var->declarationId())) {
reduce = false;
break;
}
tok = tok->next();
} else if (tok->varId() == var->declarationId() || tok->str() == "goto") {
reduce = false;
break;
Expand Down
31 changes: 31 additions & 0 deletions test/testother.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,8 @@ class TestOther : public TestFixture {
TEST_CASE(varScope33);
TEST_CASE(varScope34);
TEST_CASE(varScope35);
TEST_CASE(varScope36); // #12158
TEST_CASE(varScope37); // #12158

TEST_CASE(oldStylePointerCast);
TEST_CASE(invalidPointerCast);
Expand Down Expand Up @@ -1667,6 +1669,35 @@ class TestOther : public TestFixture {
ASSERT_EQUALS("[test.cpp:4]: (style) The scope of the variable 'buf' can be reduced.\n", errout.str());
}

void varScope36() {
// #12158
check("void f( uint32_t value ) {\n"
" uint32_t i = 0U;\n"
" if ( value > 100U ) { }\n"
" else if( value > 50U ) { }\n"
" else{\n"
" for( i = 0U; i < 5U; i++ ) {}\n"
" }\n"
"}\n", nullptr, false);
ASSERT_EQUALS("[test.cpp:2]: (style) The scope of the variable 'i' can be reduced.\n", errout.str());
}

void varScope37() {
// #12158
check("void f( uint32_t value ) {\n"
" uint32_t i = 0U;\n"
" if ( value > 100U ) { }\n"
" else {\n"
" if( value > 50U ) { }\n"
" else{\n"
" for( i = 0U; i < 5U; i++ ) {}\n"
" }\n"
" }\n"
"}\n", nullptr, false);
ASSERT_EQUALS("[test.cpp:2]: (style) The scope of the variable 'i' can be reduced.\n", 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