Coverity bounds issue when using enum in C++
16:21 13 Feb 2026

I use this pattern in multiple spots (here's a generic example):

enum FRUIT_ID 
{
    APPLE = 0,
    ORANGE,
    PEAR,
    NUM_FRUIT
};

and then I'll initialize an array:

Fruit fruit[NUM_FRUIT];

Later in my code, I'll do a check before indexing into the array as follows:

bool checkFruit(const FRUIT_ID id) const
{
    if (id >= 0 && id < NUM_FRUIT)
    {
        return fruit[id].bitten;
    }
    return false;
}

This ALWAYS raises a Coverity issue stating "Index id may be outside the bounds of fruit", but that can't be because I'm literally verifying by the check that it isn't. Is there a "correct" way to do this pattern I'm attempting here? Is this just a Coverity issue?

c++ coverity