https://gcc.gnu.org/bugzilla/show_bug.cgi?id=99353
Bug ID: 99353 Summary: warn_unused attribute does not seem to work for static variables Product: gcc Version: 9.2.0 Status: UNCONFIRMED Severity: normal Priority: P3 Component: c++ Assignee: unassigned at gcc dot gnu.org Reporter: robert-gcc at debian dot org Target Milestone: --- According to https://gcc.gnu.org/onlinedocs/gcc-10.1.0/gcc/C_002b_002b-Attributes.html#C_002b_002b-Attributes, the 'warn_unused' attribute 'informs the compiler that variables of this type should be warned about if they appear to be unused, just like variables of fundamental types'. However this does not seem to work in case of static variables - please see the program pasted below, that does not use any of declared variables, but "g++ -Wall -Wextra -O2 -std=c++17" diagnoses mostly local unused variables, as marked in the comments in the program. See https://wandbox.org/permlink/fztgVWxYX8iJ5uYS for a demo. I've tried a few different versions of g++ there, including the latest one, but with the same result. --- Program contents: --- struct __attribute__ ((warn_unused)) A { }; struct __attribute__ ((warn_unused)) B { B() {}; }; struct __attribute__ ((warn_unused)) C { ~C() {}; }; static int i1 = 0; // warning: 'i1' defined but not used static const int i2 = 0; // NO WARNING static A a1; // warning: 'a1' defined but not used static const A a2; // NO WARNING static B b1; // NO WARNING static const B b2; // NO WARNING static C c1; // NO WARNING static const C c2; // NO WARNING int main() { A a3; // warning: unused variable 'a3' const A a4; // warning: unused variable 'a4' B b3; // warning: unused variable 'b3 const B b4; // warning: unused variable 'b4' C c3; // warning: unused variable 'c3' const C c4; // warning: unused variable 'c4' static A a5; // warning: unused variable 'a5', warning: 'a5' defined but not used static const A a6; // warning: unused variable 'a6' static B b5; // warning: unused variable 'b5' static const B b6; // warning: unused variable 'b6' static C c5; // NO WARNING static const C c6; // NO WARNING return 0; }