This one causes tons of warnings in the libstdc++ testsuite (with -Wall in CXXFLAGS) and seems also bogus to me. struct ratio { static const int a = 3; }; const int ratio::a; int f() { ratio r; return r.a; }
More complete testcase: // PR c++/44412 // { dg-do compile } // { dg-options "-Wunused" } struct S { static const int a = 3; static int b; int c; }; const int S::a; int S::b = 4; int f1 () { S s; return s.a; } int f2 () { S s; return s.b; } void f3 () { S s; s.c = 6;// { dg-warning "set but not used" } } int f4 () { S s; s.c = 6; return s.c; } Guess we should mark the object through which a static data member is accessed, because that access already marks the object as TREE_USED, yet it is not a set of the object. Not sure where to do that though. Calling methods or static methods apparently is handled already, in both cases build_new_method_call -> build_this -> cp_build_unary_op ADDR_EXPR -> mark_lvalue_use -> mark_exp_read takes care of it: // PR c++/44412 // { dg-do compile } // { dg-options "-Wunused" } struct S { int foo (); static int bar (); }; int S::foo () { return 5; } int S::bar () { return 6; } int f1 () { S s; return s.foo (); } int f2 () { S s; return s.bar (); }
Created attachment 20838 [details] gcc46-pr44412.patch Untested patch.
Subject: Bug 44412 Author: jakub Date: Fri Jun 4 18:45:07 2010 New Revision: 160290 URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=160290 Log: PR c++/44412 * typeck.c (build_class_member_access_expr): Call mark_exp_read on object for static data members. * g++.dg/warn/Wunused-var-10.C: New test. * g++.dg/warn/Wunused-var-11.C: New test. Added: trunk/gcc/testsuite/g++.dg/warn/Wunused-var-10.C trunk/gcc/testsuite/g++.dg/warn/Wunused-var-11.C Modified: trunk/gcc/cp/ChangeLog trunk/gcc/cp/typeck.c trunk/gcc/testsuite/ChangeLog
Fixed.