Fixed false negative #4663

This commit is contained in:
PKEuS 2013-03-18 08:17:53 -07:00
parent 72d37f4c2c
commit d9f7042992
2 changed files with 18 additions and 2 deletions

View File

@ -2104,10 +2104,17 @@ void CheckOther::checkVariableScope()
bool used = false; // Don't warn about unused variables
for (; tok != var->scope()->classEnd; tok = tok->next()) {
if (tok->str() == "{" && tok->strAt(-1) != "=") {
if (used || !checkInnerScope(tok, var, used)) {
if (used) {
bool used2 = false;
if (!checkInnerScope(tok, var, used2) || used2) {
reduce = false;
break;
}
} else if (!checkInnerScope(tok, var, used)) {
reduce = false;
break;
}
tok = tok->link();
} else if (tok->varId() == var->varId() || tok->str() == "goto") {
reduce = false;
@ -2127,7 +2134,6 @@ bool CheckOther::checkInnerScope(const Token *tok, const Variable* var, bool& us
bool noContinue = true;
const Token* forHeadEnd = 0;
const Token* end = tok->link();
if (scope->type == Scope::eUnconditional && (tok->strAt(-1) == ")" || tok->previous()->isName())) // Might be an unknown macro like BOOST_FOREACH
loopVariable = true;

View File

@ -915,6 +915,16 @@ private:
" }\n"
"}");
ASSERT_EQUALS("[test.cpp:2]: (style) The scope of the variable 'x' can be reduced.\n", errout.str());
varScope("void f() {\n"
" int x;\n"
" if (a) {\n"
" x = stuff(x);\n"
" morestuff(x);\n"
" }\n"
" if (b) {}\n"
"}");
ASSERT_EQUALS("[test.cpp:2]: (style) The scope of the variable 'x' can be reduced.\n", errout.str());
}
void checkOldStylePointerCast(const char code[]) {