Tokenizer::simplifyEnum: Fixed false positives

This commit is contained in:
Daniel Marjamäki 2013-06-04 21:18:20 +02:00
parent 8b5792a0f8
commit dec520c507
2 changed files with 16 additions and 3 deletions

View File

@ -7437,9 +7437,10 @@ void Tokenizer::simplifyEnum()
if (Token::simpleMatch(tok2->previous(), ") {") || Token::simpleMatch(tok2->tokAt(-2), ") const {")) {
std::set<std::string> shadowArg;
for (const Token* arg = tok2; arg && arg->str() != "("; arg = arg->previous()) {
if (Token::Match(arg, "%type% [,)]") && enumValues.find(arg->str()) != enumValues.end()) {
if (Token::Match(arg->previous(), "%type%|*|& %type% [,)]") &&
enumValues.find(arg->str()) != enumValues.end()) {
shadowArg.insert(arg->str());
if (_settings->isEnabled("style")) {
if (inScope && _settings->isEnabled("style")) {
const EnumValue enumValue = enumValues.find(arg->str())->second;
duplicateEnumError(arg, enumValue.name, "Function argument");
}
@ -7465,7 +7466,7 @@ void Tokenizer::simplifyEnum()
Token::Match(prev, "& %type% =")) {
// variable declaration?
shadowVars.insert(tok3->str());
if (_settings->isEnabled("style")) {
if (inScope && _settings->isEnabled("style")) {
const EnumValue enumValue = enumValues.find(tok3->str())->second;
duplicateEnumError(tok3, enumValue.name, "Variable");
}

View File

@ -7235,6 +7235,18 @@ private:
ASSERT_EQUALS("[test.cpp:3] -> [test.cpp:1]: (style) Variable 'x' hides enumerator with same name\n"
"[test.cpp:6] -> [test.cpp:1]: (style) Function argument 'x' hides enumerator with same name\n",
errout.str());
// avoid false positive: in other scope
const char code2[] = "class C1 { enum en { x = 0 }; };\n"
"class C2 { bool x; };\n";
checkSimplifyEnum(code2);
ASSERT_EQUALS("", errout.str());
// avoid false positive: inner if-scope
const char code3[] = "enum en { x = 0 };\n"
"void f() { if (aa) ; else if (bb==x) df; }\n";
checkSimplifyEnum(code3);
ASSERT_EQUALS("", errout.str());
}
void enum23() { // ticket #2804