Skip to content

-Wunused-const-variable is too aggressive, shouldn't be part of "-Wall" anyway #19970

@Quuxplusone

Description

@Quuxplusone
Bugzilla Link 19596
Version 3.4
OS All
CC @CaseyCarter,@davidstone,@belkadan,@HighCommander4

Extended Description

#pragma once

#define ENUM_CONST(a) a,
#define ENUM_COUNT(a) +1
#define ENUM_NAME(a) #a,

#define TYPE_SAFE_ENUM(NAME, TYPE, VALUES) \
    enum class NAME : TYPE { VALUES(ENUM_CONST) }; \
    static const TYPE EnumSize_##NAME = 0 VALUES(ENUM_COUNT); \
    inline const char* EnumName(NAME a) { const char* names[] = { VALUES(ENUM_NAME) }; return names[static_cast<TYPE>(a)]; }

#include <string>

#define TABLE_ROLE_VALUES(F) \
    F(Slave) \
    F(Master)

TYPE_SAFE_ENUM(TableRole, int, TABLE_ROLE_VALUES);

TableRole g_foo = TableRole::Slave;
TableRole GetFoo() { return g_foo; }
std::string GetFooName() { return EnumName(g_foo); }

The above test case gives a spurious warning about the "unused const variable" EnumSize_TableRole, even though there's nothing the programmer can do about that.
I would like to see this warning either
(1) moved out of -Wall to preserve GCC-compatibility (I think the appropriate place for it is -Weverything), and/or
(2) toned down so that it doesn't warn about variables generated by macro expansions.

$ clang++ -std=c++11 -c test.cc -Wall
test.cc:7:1: warning: unused variable 'EnumSize_TableRole' [-Wunused-const-variable]
TYPE_SAFE_ENUM(TableRole, int, TABLE_ROLE_VALUES);
^
./type_safe_enum.h:7:117: note: expanded from macro 'TYPE_SAFE_ENUM'
#define TYPE_SAFE_ENUM(NAME, TYPE, VALUES) enum class NAME : TYPE { VALUES(ENUM_CONST) }; static const TYPE EnumSize_##NAME = 0 VALUES...
^
:159:1: note: expanded from here
EnumSize_TableRole
^
1 warning generated.

$ clang++ --version
Apple LLVM version 5.1 (clang-503.0.38) (based on LLVM 3.4svn)
Target: x86_64-apple-darwin12.5.0
Thread model: posix

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugzillaIssues migrated from bugzillaclang:diagnosticsNew/improved warning or error message in Clang, but not in clang-tidy or static analyzerduplicateResolved as duplicate

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions