Skip to content

Commit b933258

Browse files
authored
Fix #14125: false positive: unusedVariable with trailing [[maybe_unused]] attribute (#8029)
1 parent 670e4a0 commit b933258

File tree

3 files changed

+40
-10
lines changed

3 files changed

+40
-10
lines changed

lib/tokenize.cpp

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9607,17 +9607,27 @@ void Tokenizer::simplifyCPPAttribute()
96079607
if (!head)
96089608
syntaxError(tok);
96099609

9610-
while (Token::Match(head->next(), "%name%|*|&|&&|const|static|inline|volatile"))
9611-
head = head->next();
9612-
if (Token::Match(head, "%name%") && !Token::Match(head, "auto ["))
9613-
head->isAttributeMaybeUnused(true);
9614-
else if (Token::Match(tok->previous(), "%name%") && Token::Match(tok->link(), "] [;={]")) {
9615-
tok->previous()->isAttributeMaybeUnused(true);
9610+
if (Token::simpleMatch(head, ";")) {
9611+
Token *backTok = tok;
9612+
while (Token::Match(backTok, "]|[|)")) {
9613+
if (Token::Match(backTok, "]|)"))
9614+
backTok = backTok->link();
9615+
backTok = backTok->previous();
9616+
}
9617+
if (Token::Match(backTok, "%name%")) {
9618+
backTok->isAttributeMaybeUnused(true);
9619+
}
96169620
} else {
9617-
if (Token::simpleMatch(head->next(), "[")) {
9621+
while (Token::Match(head->next(), "%name%|::|*|&|&&"))
9622+
head = head->next();
9623+
if (Token::Match(head, "%name%") && !Token::Match(head, "auto ["))
9624+
head->isAttributeMaybeUnused(true);
9625+
else if (Token::Match(tok->previous(), "%name%") && Token::Match(tok->link(), "] [;={]")) {
9626+
tok->previous()->isAttributeMaybeUnused(true);
9627+
} else if (Token::simpleMatch(head->next(), "[")) {
96189628
head = head->next();
96199629
const Token *end = head->link();
9620-
for (head = head->next(); end && head != end; head = head->next()) {
9630+
for (head = head->next(); head != end; head = head->next()) {
96219631
if (Token::Match(head, "%name%")) {
96229632
head->isAttributeMaybeUnused(true);
96239633
}

test/testtokenize.cpp

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -278,7 +278,8 @@ class TestTokenizer : public TestFixture {
278278
TEST_CASE(functionAttributeListAfter);
279279
TEST_CASE(functionAttributeListAfter2);
280280
TEST_CASE(cppMaybeUnusedBefore);
281-
TEST_CASE(cppMaybeUnusedAfter);
281+
TEST_CASE(cppMaybeUnusedAfter1);
282+
TEST_CASE(cppMaybeUnusedAfter2);
282283
TEST_CASE(cppMaybeUnusedStructuredBinding);
283284

284285
TEST_CASE(splitTemplateRightAngleBrackets);
@@ -4260,7 +4261,7 @@ class TestTokenizer : public TestFixture {
42604261
ASSERT(var && var->isAttributeMaybeUnused());
42614262
}
42624263

4263-
void cppMaybeUnusedAfter() {
4264+
void cppMaybeUnusedAfter1() {
42644265
const char code[] = "int var [[maybe_unused]] {};";
42654266
const char expected[] = "int var { } ;";
42664267

@@ -4273,6 +4274,19 @@ class TestTokenizer : public TestFixture {
42734274
ASSERT(var && var->isAttributeMaybeUnused());
42744275
}
42754276

4277+
void cppMaybeUnusedAfter2() {
4278+
const char code[] = "std::string var [[maybe_unused]];";
4279+
const char expected[] = "std :: string var ;";
4280+
4281+
SimpleTokenizer tokenizer(settings0, *this);
4282+
ASSERT(tokenizer.tokenize(code));
4283+
4284+
ASSERT_EQUALS(expected, tokenizer.tokens()->stringifyList(nullptr, false));
4285+
4286+
const Token *var = Token::findsimplematch(tokenizer.tokens(), "var");
4287+
ASSERT(var && var->isAttributeMaybeUnused());
4288+
}
4289+
42764290
void cppMaybeUnusedStructuredBinding() {
42774291
const char code[] = "[[maybe_unused]] auto [var1, var2] = f();";
42784292
const char expected[] = "auto [ var1 , var2 ] = f ( ) ;";

test/testunusedvar.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6534,6 +6534,12 @@ class TestUnusedVar : public TestFixture {
65346534
" [[maybe_unused]] char b[1][2];\n"
65356535
"}");
65366536
ASSERT_EQUALS("", errout_str());
6537+
6538+
functionVariableUsage("int main() {\n"
6539+
" std::string a [[maybe_unused]];\n"
6540+
" f();\n"
6541+
"}");
6542+
ASSERT_EQUALS("", errout_str());
65376543
}
65386544

65396545
void localvarrvalue() { // ticket #13977

0 commit comments

Comments
 (0)