Skip to content

Commit

Permalink
Fix FP unusedVariable with arrays (danmar#5319)
Browse files Browse the repository at this point in the history
  • Loading branch information
mptre authored Aug 12, 2023
1 parent b9cc138 commit 5c6962c
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
7 changes: 5 additions & 2 deletions lib/tokenize.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8843,8 +8843,11 @@ void Tokenizer::simplifyAttribute()

// check if after variable name
if (Token::Match(after, ";|=")) {
if (Token::Match(tok->previous(), "%type%"))
vartok = tok->previous();
Token *prev = tok->previous();
while (Token::simpleMatch(prev, "]"))
prev = prev->link()->previous();
if (Token::Match(prev, "%type%"))
vartok = prev;
}

// check if before variable name
Expand Down
13 changes: 13 additions & 0 deletions test/testunusedvar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5651,6 +5651,13 @@ class TestUnusedVar : public TestFixture {
" bool __attribute__((used)) test;\n"
"}");
ASSERT_EQUALS("", errout.str());

functionVariableUsage("int foo()\n"
"{\n"
" char a[1] __attribute__((unused));\n"
" char b[1][2] __attribute__((unused));\n"
"}");
ASSERT_EQUALS("", errout.str());
}

void localvarFunction() {
Expand Down Expand Up @@ -6199,6 +6206,12 @@ class TestUnusedVar : public TestFixture {
" [[maybe_unused]] [[anotherattribute]] const int* = 1;\n"
"}");
ASSERT_EQUALS("", errout.str());

functionVariableUsage("int main() {\n"
" [[maybe_unused]] char a[1];\n"
" [[maybe_unused]] char b[1][2];\n"
"}");
ASSERT_EQUALS("", errout.str());
}

void localvarthrow() { // ticket #3687
Expand Down

0 comments on commit 5c6962c

Please sign in to comment.