0
int* ptrF();
void f()
{
    int* p = ptrF();
    bool pOK = p && true;
    if (pOK)
        *p = 12; // Lint thinks p might be nullptr here
}

Lint 发出警告

C:\lint_test\nullptr_with_init.cpp(8): Issue 613: (Warning -- Possible use of null pointer 'p' in argument to operator 'unary *' [Reference: file C:\lint_test\nullptr_with_init.cpp: line 6])

有谁知道是否有一个设置可以让 Lint 更“聪明”并且如果 p == nullptr 看到 pOK 不能为真

这比更改代码或抑制这样的警告要好得多

        *p = 12; //lint !e613

编辑:

Pc Lint,如何使用 init() 抑制 err 613(Possible use of null ponter) 是一个完全不同的问题。那是关于如何抑制警告。这个是关于如何让 Lint 检查“复杂”的 if 语句(如果可能的话)

4

1 回答 1

0

我似乎在这种情况下添加从指针到 bool 的转换会有所帮助

bool pOK = (bool)p && true;
于 2017-08-17T18:01:34.970 回答