Coder Coder - 5 months ago 42
C Question

Finding unused enum members in C

What is the fastest way to find unused enum members?

Commenting values out one by one won't work because I have almost 700 members and want to trim off a few unused ones.


I am not aware of any compiler warning, but you could possibly try with splint static analyzer tool. According to its documentation (emphasis mine):

Splint detects constants, functions, parameters, variables, types, enumerator members, and structure or union fields that are declared but never used.

As I checked, it works as intented. Here is example code:

#include <stdio.h>

enum Month { JAN, FEB, MAR };

int main()
    enum Month m1 = JAN;
    printf("%d\n", m1);

By running the splint command, you will obtain following messages:

main.c:3:19: Enum member FEB not used
   A member of an enum type is never used. (Use -enummemuse to inhibit warning)
main.c:3:24: Enum member MAR not used