[Bug c/28901] -Wunused-variable ignores unused const initialised variables

pmatilai at laiskiainen dot org gcc-bugzilla@gcc.gnu.org
Wed Feb 17 09:01:00 GMT 2016


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

Panu Matilainen <pmatilai at laiskiainen dot org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |pmatilai at laiskiainen dot org

--- Comment #26 from Panu Matilainen <pmatilai at laiskiainen dot org> ---
(In reply to Josh Triplett from comment #23)
> Please don't attach this to -Wextra; instead, please consider making it a
> -Wconst-unused-variable=2 or similar.  Otherwise, projects using static
> const values in header files would get a massive pile of additional
> warnings, typically all false positives.
>
> In a main file, this makes sense and likely has relatively few false
> positives; the most common false positive would involve defining something
> only used in ifdef'd code, and even then tagging it with
> __attribute__((unused)) (Linux's __maybe_unused) seems correct.
> 
> However, in a header file, any use of static const to define constants for a
> project will almost certainly get this warning, unless every single user of
> that header file (directly or indirectly) uses every constant (or unless the
> header itself does).  Silently throwing away the unused constants seems like
> the right thing to do there. 

Seconded, +100, amen. 

On main files warning on unused junk is certainly useful but static const is
commonly and deliberately used in headers (eg for arrays such as in comment
#18) and now produces unhelpful false positives. In case somebody really wants
to see the warnings from headers too, something like -Wconst-unused-variable=2
sounds fine but it should not be a default.

Just as a random data point: out of the three projects I've so far compiled
with gcc 6 (on fedora rawhide), two have false positives from static const
arrays in headers. The other one has them by the dozen.


More information about the Gcc-bugs mailing list