varScope: Fix false negatives for references

This commit is contained in:
Daniel Marjamäki 2018-06-22 22:51:03 +02:00
parent e111902682
commit d0614b9b36
2 changed files with 20 additions and 1 deletions

View File

@ -1097,7 +1097,7 @@ void CheckOther::checkVariableScope()
const SymbolDatabase *symbolDatabase = mTokenizer->getSymbolDatabase();
for (const Variable* var : symbolDatabase->variableList()) {
if (!var || !var->isLocal() || (!var->isPointer() && var->valueType()->type <= ValueType::Type::RECORD))
if (!var || !var->isLocal() || (!var->isPointer() && !var->isReference() && var->valueType()->type <= ValueType::Type::RECORD))
continue;
if (var->isConst())

View File

@ -82,6 +82,7 @@ private:
TEST_CASE(varScope21); // Ticket #5382
TEST_CASE(varScope22); // Ticket #5684
TEST_CASE(varScope23); // Ticket #6154
TEST_CASE(varScope24); // pointer / reference
TEST_CASE(oldStylePointerCast);
TEST_CASE(invalidPointerCast);
@ -1140,6 +1141,24 @@ private:
ASSERT_EQUALS("", errout.str());
}
void varScope24() {
check("void f(Foo x) {\n"
" Foo &r = x;\n"
" if (cond) {\n"
" r.dostuff();\n"
" }\n"
"}");
ASSERT_EQUALS("[test.cpp:2]: (style) The scope of the variable 'r' can be reduced.\n", errout.str());
check("void f(Foo x) {\n"
" Foo foo = x;\n"
" if (cond) {\n"
" foo.dostuff();\n"
" }\n"
"}");
ASSERT_EQUALS("", errout.str());
}
void checkOldStylePointerCast(const char code[]) {
// Clear the error buffer..
errout.str("");