https://gcc.gnu.org/bugzilla/show_bug.cgi?id=94575

            Bug ID: 94575
           Summary: Bogus warning: Used variable is “not” used
           Product: gcc
           Version: 10.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: bisqwit at iki dot fi
  Target Milestone: ---

Incorrect warning (-std=c++14 -Wall )
Tested and occurs on GCC 5.4.1, 6.5.0, 7.5.0, 8.4.0, 9.3.0, and 10.0.1 (master
revision 596c90d3559:023579257f5:906b3eb9df6c577d3f6e9c3ea5c9d7e4d1e90536))

tmp.cc: In function ‘int main()’:
tmp.cc:9:22: warning: variable ‘table’ set but not used
[-Wunused-but-set-variable]
    9 |     static const int table[1] = {123456};
      |                      ^~~~~

Table is in fact used; program prints 123456.

#include <cstdio>
template<typename T>
static void Use(T& plot)
{
    plot(1);
}
int main()
{
    static const int table[1] = {123456};
    Use([&](auto x)
    {
        unsigned var = table[x];
        unsigned ui = var;
        std::printf("%u\n", ui);
    });
}

This a non-exhaustive list of changes that will make the warning go away:

— Changing the lambda auto parameter into a static type such as int
— Changing Use() into a lambda function in main()
— Removing the store into temporary variable “ui”

Reply via email to