From 5c6962c273f64f8d27fd672e7f02e088bf68c352 Mon Sep 17 00:00:00 2001 From: Anton Lindqvist Date: Sat, 12 Aug 2023 16:55:52 +0200 Subject: [PATCH] Fix FP unusedVariable with arrays (#5319) --- lib/tokenize.cpp | 7 +++++-- test/testunusedvar.cpp | 13 +++++++++++++ 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/lib/tokenize.cpp b/lib/tokenize.cpp index 1138285abf7..ec63858ff22 100644 --- a/lib/tokenize.cpp +++ b/lib/tokenize.cpp @@ -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 diff --git a/test/testunusedvar.cpp b/test/testunusedvar.cpp index 527fb5c1916..94904a6d684 100644 --- a/test/testunusedvar.cpp +++ b/test/testunusedvar.cpp @@ -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() { @@ -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