Skip to content

Commit e553940

Browse files
authored
#12158: improve check: variableScope is not reported when there is el… (#5758)
variableScope is not reported when there is else if
1 parent c02d078 commit e553940

2 files changed

Lines changed: 32 additions & 10 deletions

File tree

lib/checkother.cpp

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -995,16 +995,7 @@ void CheckOther::checkVariableScope()
995995

996996
// parse else if blocks..
997997
} else if (Token::simpleMatch(tok, "else { if (") && Token::simpleMatch(tok->linkAt(3), ") {")) {
998-
const Token *endif = tok->linkAt(3)->linkAt(1);
999-
bool elseif = false;
1000-
if (Token::simpleMatch(endif, "} }"))
1001-
elseif = true;
1002-
else if (Token::simpleMatch(endif, "} else {") && Token::simpleMatch(endif->linkAt(2),"} }"))
1003-
elseif = true;
1004-
if (elseif && Token::findmatch(tok->next(), "%varid%", tok->linkAt(1), var->declarationId())) {
1005-
reduce = false;
1006-
break;
1007-
}
998+
tok = tok->next();
1008999
} else if (tok->varId() == var->declarationId() || tok->str() == "goto") {
10091000
reduce = false;
10101001
break;

test/testother.cpp

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,8 @@ class TestOther : public TestFixture {
101101
TEST_CASE(varScope33);
102102
TEST_CASE(varScope34);
103103
TEST_CASE(varScope35);
104+
TEST_CASE(varScope36); // #12158
105+
TEST_CASE(varScope37); // #12158
104106

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

1672+
void varScope36() {
1673+
// #12158
1674+
check("void f( uint32_t value ) {\n"
1675+
" uint32_t i = 0U;\n"
1676+
" if ( value > 100U ) { }\n"
1677+
" else if( value > 50U ) { }\n"
1678+
" else{\n"
1679+
" for( i = 0U; i < 5U; i++ ) {}\n"
1680+
" }\n"
1681+
"}\n", nullptr, false);
1682+
ASSERT_EQUALS("[test.cpp:2]: (style) The scope of the variable 'i' can be reduced.\n", errout.str());
1683+
}
1684+
1685+
void varScope37() {
1686+
// #12158
1687+
check("void f( uint32_t value ) {\n"
1688+
" uint32_t i = 0U;\n"
1689+
" if ( value > 100U ) { }\n"
1690+
" else {\n"
1691+
" if( value > 50U ) { }\n"
1692+
" else{\n"
1693+
" for( i = 0U; i < 5U; i++ ) {}\n"
1694+
" }\n"
1695+
" }\n"
1696+
"}\n", nullptr, false);
1697+
ASSERT_EQUALS("[test.cpp:2]: (style) The scope of the variable 'i' can be reduced.\n", errout.str());
1698+
}
1699+
1700+
16701701
#define checkOldStylePointerCast(code) checkOldStylePointerCast_(code, __FILE__, __LINE__)
16711702
void checkOldStylePointerCast_(const char code[], const char* file, int line) {
16721703
// Clear the error buffer..

0 commit comments

Comments
 (0)