Limit variableScope check. Do not check C code if all local variables are declared at function level.
This commit is contained in:
parent
093ff58f5f
commit
485153c930
|
@ -879,6 +879,22 @@ void CheckOther::checkVariableScope()
|
|||
|
||||
const SymbolDatabase *symbolDatabase = mTokenizer->getSymbolDatabase();
|
||||
|
||||
// In C it is common practice to declare local variables at the
|
||||
// start of functions.
|
||||
if (mTokenizer->isC()) {
|
||||
// Try to autodetect what coding style is used. If a local variable is
|
||||
// declared in an inner scope then we will warn about all variables.
|
||||
bool limitScope = false;
|
||||
for (const Variable* var : symbolDatabase->variableList()) {
|
||||
if (var && var->isLocal() && var->nameToken()->scope()->type != Scope::ScopeType::eFunction) {
|
||||
limitScope = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (!limitScope)
|
||||
return;
|
||||
}
|
||||
|
||||
for (const Variable* var : symbolDatabase->variableList()) {
|
||||
if (!var || !var->isLocal() || (!var->isPointer() && !var->isReference() && !var->typeStartToken()->isStandardType()))
|
||||
continue;
|
||||
|
|
|
@ -1245,6 +1245,16 @@ private:
|
|||
" if (currtime > t) {}\n"
|
||||
" }\n"
|
||||
"}", "test.c");
|
||||
ASSERT_EQUALS("", errout.str());
|
||||
|
||||
check("void f() {\n"
|
||||
" time_t currtime;\n"
|
||||
" if (a) {\n"
|
||||
" int x = 123;\n"
|
||||
" currtime = time(&dummy);\n"
|
||||
" if (currtime > t) {}\n"
|
||||
" }\n"
|
||||
"}", "test.c");
|
||||
ASSERT_EQUALS("[test.c:2]: (style) The scope of the variable 'currtime' can be reduced.\n", errout.str());
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue