Implemented unused variable checking for standard types (#2851)
This commit is contained in:
parent
a6c54563ae
commit
bb940e4722
|
@ -612,7 +612,7 @@ void CheckUnusedVar::checkFunctionVariableUsage_iterateScopes(const Scope* const
|
||||||
type = Variables::pointerPointer;
|
type = Variables::pointerPointer;
|
||||||
else if (i->isPointer())
|
else if (i->isPointer())
|
||||||
type = Variables::pointer;
|
type = Variables::pointer;
|
||||||
else if (_tokenizer->isC() || i->typeEndToken()->isStandardType() || isRecordTypeWithoutSideEffects(*i) || Token::simpleMatch(i->nameToken()->tokAt(-3), "std :: string"))
|
else if (_tokenizer->isC() || i->typeEndToken()->isStandardType() || isRecordTypeWithoutSideEffects(*i) || Token::simpleMatch(i->typeStartToken(), "std ::"))
|
||||||
type = Variables::standard;
|
type = Variables::standard;
|
||||||
if (type == Variables::none || isPartOfClassStructUnion(i->typeStartToken()))
|
if (type == Variables::none || isPartOfClassStructUnion(i->typeStartToken()))
|
||||||
continue;
|
continue;
|
||||||
|
@ -675,22 +675,21 @@ void CheckUnusedVar::checkFunctionVariableUsage_iterateScopes(const Scope* const
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (Token::Match(tok, "%type% const| *|&| const| *| const| %var% [;=[(]")) { // Declaration: Skip
|
if (Token::Match(tok->previous(), "[;{}]")) {
|
||||||
// Before a declaration there should be ;{}
|
for (const Token* tok2 = tok->next(); tok2; tok2 = tok2->next()) {
|
||||||
const Token *prev = tok;
|
if (tok2->varId()) {
|
||||||
while (prev && prev->isName() && prev->str() != "return" && prev->str() != "throw")
|
const Variable* var = _tokenizer->getSymbolDatabase()->getVariableFromVarId(tok2->varId());
|
||||||
prev = prev->previous();
|
if (var && var->nameToken() == tok2) { // Declaration: Skip
|
||||||
if (Token::Match(prev, "[;{}]")) {
|
tok = tok2->next();
|
||||||
|
if (tok && Token::Match(tok, "( %var% )")) // Simple initialization through copy ctor
|
||||||
tok = tok->next();
|
tok = tok->next();
|
||||||
while (Token::Match(tok, "const|*|&"))
|
else if (tok && Token::Match(tok, "= %var% ;")) // Simple initialization
|
||||||
tok = tok->next();
|
tok = tok->next();
|
||||||
tok = Token::findmatch(tok, "[;=[(]");
|
else if (var->typeEndToken()->str() == ">") // Be careful with types like std::vector
|
||||||
if (tok && Token::Match(tok, "( %var% )")) // Simple initialization through copy ctor
|
tok = tok->previous();
|
||||||
tok = tok->next();
|
break;
|
||||||
else if (tok && Token::Match(tok, "= %var% ;")) // Simple initialization
|
}
|
||||||
tok = tok->next();
|
} else if (Token::Match(tok2, "[;({=]"))
|
||||||
if (!tok)
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -115,6 +115,8 @@ private:
|
||||||
|
|
||||||
TEST_CASE(localvarthrow); // ticket #3687
|
TEST_CASE(localvarthrow); // ticket #3687
|
||||||
|
|
||||||
|
TEST_CASE(localVarStd);
|
||||||
|
|
||||||
// Don't give false positives for variables in structs/unions
|
// Don't give false positives for variables in structs/unions
|
||||||
TEST_CASE(localvarStruct1);
|
TEST_CASE(localvarStruct1);
|
||||||
TEST_CASE(localvarStruct2);
|
TEST_CASE(localvarStruct2);
|
||||||
|
@ -3129,6 +3131,33 @@ private:
|
||||||
ASSERT_EQUALS("", errout.str());
|
ASSERT_EQUALS("", errout.str());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void localVarStd() {
|
||||||
|
functionVariableUsage("void f() {\n"
|
||||||
|
" std::string x = foo();\n"
|
||||||
|
"}");
|
||||||
|
ASSERT_EQUALS("[test.cpp:2]: (style) Variable 'x' is assigned a value that is never used\n", errout.str());
|
||||||
|
|
||||||
|
functionVariableUsage("void f() {\n"
|
||||||
|
" std::vector<int> x;\n"
|
||||||
|
"}");
|
||||||
|
ASSERT_EQUALS("[test.cpp:2]: (style) Unused variable: x\n", errout.str());
|
||||||
|
|
||||||
|
functionVariableUsage("void f() {\n"
|
||||||
|
" std::vector<int> x(100);\n"
|
||||||
|
"}");
|
||||||
|
TODO_ASSERT_EQUALS("[test.cpp:2]: (style) Variable 'x' is assigned a value that is never used\n", "", errout.str());
|
||||||
|
|
||||||
|
functionVariableUsage("void f() {\n"
|
||||||
|
" std::vector<MyClass> x;\n"
|
||||||
|
"}");
|
||||||
|
ASSERT_EQUALS("[test.cpp:2]: (style) Unused variable: x\n", errout.str());
|
||||||
|
|
||||||
|
functionVariableUsage("void f() {\n"
|
||||||
|
" std::vector<MyClass> x(100);\n" // Might have a side-effect
|
||||||
|
"}");
|
||||||
|
ASSERT_EQUALS("", errout.str());
|
||||||
|
}
|
||||||
|
|
||||||
// ticket #3104 - false positive when variable is read with "if (NOT var)"
|
// ticket #3104 - false positive when variable is read with "if (NOT var)"
|
||||||
void localvarIfNOT() {
|
void localvarIfNOT() {
|
||||||
functionVariableUsage("void f() {\n"
|
functionVariableUsage("void f() {\n"
|
||||||
|
|
Loading…
Reference in New Issue