diff --git a/lib/tokenize.cpp b/lib/tokenize.cpp index 86b167057..50c149a3f 100644 --- a/lib/tokenize.cpp +++ b/lib/tokenize.cpp @@ -7439,6 +7439,10 @@ void Tokenizer::simplifyEnum() for (const Token* arg = tok2; arg && arg->str() != "("; arg = arg->previous()) { if (Token::Match(arg, "%type% [,)]") && enumValues.find(arg->str()) != enumValues.end()) { shadowArg.insert(arg->str()); + if (_settings->isEnabled("style")) { + const EnumValue enumValue = enumValues.find(arg->str())->second; + duplicateEnumError(arg, enumValue.name, "Function argument"); + } } } if (!shadowArg.empty()) { @@ -7455,8 +7459,17 @@ void Tokenizer::simplifyEnum() if (tok3->str() == "{") tok3 = tok3->link(); // skip inner scopes else if (tok3->isName() && enumValues.find(tok3->str()) != enumValues.end()) { - if (Token::Match(tok3->previous(), "*|%type%") || Token::Match(tok3->previous(), "& %type% =")) // variable declaration? + const Token *prev = tok3->previous(); + if ((prev->isName() && !Token::Match(prev, "return|case")) || + prev->str() == "*" || + Token::Match(prev, "& %type% =")) { + // variable declaration? shadowVars.insert(tok3->str()); + if (_settings->isEnabled("style")) { + const EnumValue enumValue = enumValues.find(tok3->str())->second; + duplicateEnumError(tok3, enumValue.name, "Variable"); + } + } } } if (!shadowVars.empty()) { diff --git a/test/testsimplifytokens.cpp b/test/testsimplifytokens.cpp index b0436bdab..7f306e9e9 100644 --- a/test/testsimplifytokens.cpp +++ b/test/testsimplifytokens.cpp @@ -7232,7 +7232,9 @@ private: " x+=1;\n" "}\n"; checkSimplifyEnum(code); - ASSERT_EQUALS("[test.cpp:3] -> [test.cpp:1]: (style) Variable 'x' hides enumerator with same name\n", errout.str()); + 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()); } void enum23() { // ticket #2804