From dec520c50756cbfc0d1bc2a072c245345fe556ff Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Tue, 4 Jun 2013 21:18:20 +0200 Subject: [PATCH] Tokenizer::simplifyEnum: Fixed false positives --- lib/tokenize.cpp | 7 ++++--- test/testsimplifytokens.cpp | 12 ++++++++++++ 2 files changed, 16 insertions(+), 3 deletions(-) diff --git a/lib/tokenize.cpp b/lib/tokenize.cpp index 50c149a3f..988e3f01f 100644 --- a/lib/tokenize.cpp +++ b/lib/tokenize.cpp @@ -7437,9 +7437,10 @@ void Tokenizer::simplifyEnum() if (Token::simpleMatch(tok2->previous(), ") {") || Token::simpleMatch(tok2->tokAt(-2), ") const {")) { std::set 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"); } diff --git a/test/testsimplifytokens.cpp b/test/testsimplifytokens.cpp index 7f306e9e9..090ec5931 100644 --- a/test/testsimplifytokens.cpp +++ b/test/testsimplifytokens.cpp @@ -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