Fixed #5145 (False Positive: Variable 'A' hides enumerator with same name)

This commit is contained in:
Daniel Marjamki 2013-11-03 09:34:48 +01:00
parent 5f0cfa0ee4
commit bcc8384a95
2 changed files with 9 additions and 3 deletions

View File

@ -7801,9 +7801,8 @@ void Tokenizer::simplifyEnum()
tok3 = tok3->link(); // skip inner scopes
else if (tok3->isName() && enumValues.find(tok3->str()) != enumValues.end()) {
const Token *prev = tok3->previous();
if ((prev->isName() && !Token::Match(prev, "return|case|throw")) ||
prev->str() == "*" ||
Token::Match(prev, "& %type% =")) {
if ((prev->isName() && !Token::Match(prev,"return|case|throw")) ||
Token::Match(prev, "&|* %type% =")) {
// variable declaration?
shadowVars.insert(tok3->str());
if (inScope && _settings->isEnabled("style")) {

View File

@ -371,6 +371,7 @@ private:
TEST_CASE(enum36); // ticket #4378
TEST_CASE(enum37); // ticket #4280 (shadow variable)
TEST_CASE(enum38); // ticket #4463 (when throwing enum id, don't warn about shadow variable)
TEST_CASE(enum39); // ticket #5145 (fp variable hides enum)
TEST_CASE(enumscope1); // ticket #3949
TEST_CASE(duplicateDefinition); // ticket #3565
@ -7452,6 +7453,12 @@ private:
ASSERT_EQUALS("", errout.str());
}
void enum39() { // #5145 - fp variable hides enum
const char code[] = "enum { A }; void f() { int a = 1 * A; }";
checkSimplifyEnum(code);
ASSERT_EQUALS("", errout.str());
}
void enumscope1() { // #3949 - don't simplify enum from one function in another function
const char code[] = "void foo() { enum { A = 0, B = 1 }; }\n"
"void bar() { int a = A; }";