//定义了一些静态const变量

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

//一些开关案例
switch (Numdata)   //Numdata is either FirstData, SecondData
{
    case FirstData:
         //some code
    case SecondData:
         //some code
}

//现在 PC-lint 对此“Note 1960: Violates MISRA C++ 2008 Required Rule 5-0-12, Disallowed use of non-numeric value in a case label”的投诉

所以问题是为什么 PC-lint 不将静态常量成员视为数值?

明确键入强制转换大小写标签(这应该解决这个问题)是个好主意吗?

案例标签需要强制转换为什么类型?只是 uint8_t 会吗?

免除此 Lint 问题的其他方法?

最佳答案

尝试使用枚举,因为它们是“已知的”,例如使用以下代码,第二个函数没有问题。

#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;
}

关于c++ - 开关盒标签上的 PC-lint : Violates MISRA C++ 2008 Required Rule 5-0-12,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/22964744/

10-13 07:38