Pc Lint,如何使用 init() 为 class 抑制错误 613(可能使用空指针)

Pc Lint, how to suppress err 613(Possible use of null ponter) for class with init()

尽量简化情况。所以我有一个 class:

class C
{
    int * field;
public:
    C() : field(nullptr) {}
    void init(int* f) { field = f; }

    int getI1() { return *field; }
    int getI2() { return *field; }
};

生成 2 个 Lint 警告 613(可能使用空指针 'C::i'...)

我知道在调用 getI1() 或 getI2() 时 "field" 不会为空。不幸的是我无法在构造函数中初始化它。所以我想抑制 Lint 警告。我可以这样做

class C
{
    int * field;
public:
    C() : field(nullptr) {}
    void init(int* f) { field = f; }

    int getI1() { return *field; } //lint !e613
    int getI2() { return *field; } //lint !e613
};

但在我的真实情况下:

1) 这样的class有很多,每个class有很多 使用此指针的函数。

2) 我的管理人员不允许我添加太多 lint 代码中的注释。

所以我的问题是:有没有人知道可以让我告诉 Lint "I know the code is not best, just stop checking this particular member variable for null" 的命令行选项?

可能类似于 -sem 参数?

So my question: does anyone know a command-line option that will let me tell Lint "I know the code is not best, just stop checking this particular member variable for null"?

这是错误的处理方式(即使我知道这样的命令行参数)。

PC-Lint 会正确警告您

int getI1() { return *i; } //lint !e613
int getI2() { return *i; } //lint !e613

可能会无意中取消引用 nullptr

只是试图抑制1 衰减不是一个好主意,因为 init() 函数的调用不是强制性的。

摆脱它的正确方法是添加一个显式检查,如

int getI1() { 
    if(i) {
        return *i; 
    }
    throw std::runtime_error("i wasn't initialized properly.");
}

1) There are rather many such classes and each class has many functions that use this pointer.

除了浏览它们并重构那些糟糕的代码之外别无他法。

2) My managements doesn't allow me to add too many lint comments in the code.

这是个好政策。他们花钱安装SCA工具是有原因的,希望代码得到改进。
如果这与您有空的时间冲突,请让他们制定一项任务,让您完成该任务。


如果您只想专注于 PC-Lint 报告的其他(更重要的内容),请使用 grep 或类似工具过滤掉您不想看到的 ATM 内容。但是不要建立命令行参数来完全抑制它们。那些东西会永远被遗忘,以后再也不会被触及或接近。


1 抑制 SCA 工具(如 PC-Lint)给出的任何错误或警告会破坏它的全部目的,除非您绝对确定该工具会给您 误报 。否则,您的公司可以简单地节省购买许可证的费用,并坚持不良的编码习惯。

似乎在构造器有运行之后,你有一个不可用的实例(调用getT1()或getT2()会崩溃)。那不是我喜欢的东西。

最好有一个构造函数 C(int* f)。问题消失了。在这种情况下,警告是完全合理的,并警告您有错误代码,因此应该修复错误代码。

我知道代码不好,应该修复等等。不幸的是,我现在不能这样做(因为一个人需要付出巨大的努力和高风险的改变),但这些错误压倒了其他问题,有时甚至是更严重的问题

我发现要在一行中抑制此警告,您可以这样做:

-esym(613, C::field)