Skip to content

Commit dec520c

Browse files
committed
Tokenizer::simplifyEnum: Fixed false positives
1 parent 8b5792a commit dec520c

2 files changed

Lines changed: 16 additions & 3 deletions

File tree

lib/tokenize.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7437,9 +7437,10 @@ void Tokenizer::simplifyEnum()
74377437
if (Token::simpleMatch(tok2->previous(), ") {") || Token::simpleMatch(tok2->tokAt(-2), ") const {")) {
74387438
std::set<std::string> shadowArg;
74397439
for (const Token* arg = tok2; arg && arg->str() != "("; arg = arg->previous()) {
7440-
if (Token::Match(arg, "%type% [,)]") && enumValues.find(arg->str()) != enumValues.end()) {
7440+
if (Token::Match(arg->previous(), "%type%|*|& %type% [,)]") &&
7441+
enumValues.find(arg->str()) != enumValues.end()) {
74417442
shadowArg.insert(arg->str());
7442-
if (_settings->isEnabled("style")) {
7443+
if (inScope && _settings->isEnabled("style")) {
74437444
const EnumValue enumValue = enumValues.find(arg->str())->second;
74447445
duplicateEnumError(arg, enumValue.name, "Function argument");
74457446
}
@@ -7465,7 +7466,7 @@ void Tokenizer::simplifyEnum()
74657466
Token::Match(prev, "& %type% =")) {
74667467
// variable declaration?
74677468
shadowVars.insert(tok3->str());
7468-
if (_settings->isEnabled("style")) {
7469+
if (inScope && _settings->isEnabled("style")) {
74697470
const EnumValue enumValue = enumValues.find(tok3->str())->second;
74707471
duplicateEnumError(tok3, enumValue.name, "Variable");
74717472
}

test/testsimplifytokens.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7235,6 +7235,18 @@ class TestSimplifyTokens : public TestFixture {
72357235
ASSERT_EQUALS("[test.cpp:3] -> [test.cpp:1]: (style) Variable 'x' hides enumerator with same name\n"
72367236
"[test.cpp:6] -> [test.cpp:1]: (style) Function argument 'x' hides enumerator with same name\n",
72377237
errout.str());
7238+
7239+
// avoid false positive: in other scope
7240+
const char code2[] = "class C1 { enum en { x = 0 }; };\n"
7241+
"class C2 { bool x; };\n";
7242+
checkSimplifyEnum(code2);
7243+
ASSERT_EQUALS("", errout.str());
7244+
7245+
// avoid false positive: inner if-scope
7246+
const char code3[] = "enum en { x = 0 };\n"
7247+
"void f() { if (aa) ; else if (bb==x) df; }\n";
7248+
checkSimplifyEnum(code3);
7249+
ASSERT_EQUALS("", errout.str());
72387250
}
72397251

72407252
void enum23() { // ticket #2804

0 commit comments

Comments
 (0)