4

//some static const variables are defined

static const uint8_t FirstData = 1;
static const uint8_t SecondData = 2;

//some switch case

switch (Numdata)   //Numdata is either FirstData, SecondData
{
    case FirstData:
         //some code
    case SecondData:
         //some code
}

// Now PC-lint complaints for this "Note 1960: Violates MISRA C++ 2008 Required Rule 5-0-12, Disallowed use of non-numeric value in a case label"

So the question is why PC-lint does not consider static const members as Numeric value?

Is it a good idea to explicitly type cast case labels (which should resolve this) ?

what type do the case labels need to be type cast to? Will just uint8_t do?

Some other way to exempt this Lint issue ?

Andrew
  • 2,046
  • 1
  • 24
  • 37
  • Your example also violates 6-4-6 in that there is no `default` clause (although this may be because it's a cut down, minimal reproducible, example) – Andrew Jan 20 '22 at 09:21

2 Answers2

1

The rule says : "[Explicitly] signed char and unsigned char type shall only be used for the storage and use of numeric values." I assume this disallows use as labels, and the lint error message is badly worded. I see nothing wrong with it and would just disable the warning for that code, or, if necessary, for the whole file.

Wyzard
  • 33,849
  • 3
  • 67
  • 87
Peter - Reinstate Monica
  • 15,048
  • 4
  • 37
  • 62
1

Try using an enum, as they are "known", using the following code for example, the second function does not have the issue.

#include <cstdint>

namespace testa
{
    static const uint8_t FirstData = 1;
    static const uint8_t SecondData = 2;

    int32_t func_A(uint8_t Numdata)
    {
        int32_t ret = 0;
        switch (Numdata)   //Numdata is either FirstData, SecondData
        {
        case FirstData:
            ret = 1;
        case SecondData:
            ret = 2;
        }

        return ret;
    }
}

namespace testb
{
    enum data {
        FirstData,
        SecondData
    };

    int32_t func_B(data Numdata)
    {
        int32_t ret = 0;
        switch (Numdata)   //Numdata is either FirstData, SecondData
        {
        case FirstData:
            ret = 1;
        case SecondData:
            ret = 2;
        }

        return ret;
    }
}

int32_t main(int32_t, int8_t*[])
{
    int32_t z = 0;
    z += testa::func_A(testa::FirstData);
    z += testa::func_A(testa::SecondData);
    z += testb::func_B(testb::FirstData);
    z += testb::func_B(testb::SecondData);

    return z;
}
Simeon Pilgrim
  • 22,906
  • 3
  • 32
  • 45