Add test for #6541, #5475, avoid duplicate warning (#4335)

* Add test for #6541, avoid duplicate warning

* Add test for #5475

* Fix test
This commit is contained in:
chrchr-github 2022-08-02 21:43:18 +02:00 committed by GitHub
parent bc409776e3
commit 71f9a7269f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 17 additions and 1 deletions

View File

@ -288,7 +288,8 @@ void CheckNullPointer::nullPointerByDeRefAndChec()
if (Token::Match(tok, "%num%|%char%|%str%"))
continue;
if (!isNullablePointer(tok, mSettings))
if (!isNullablePointer(tok, mSettings) ||
(tok->str() == "." && isNullablePointer(tok->astOperand2(), mSettings) && tok->astOperand2()->getValue(0))) // avoid duplicate warning
continue;
// Can pointer be NULL?

View File

@ -667,6 +667,16 @@ private:
" *new int;\n"
"}\n", /*inconclusive*/ true);
ASSERT_EQUALS("[test.cpp:2]: (warning, inconclusive) Found suspicious operator '*', result is not used.\n", errout.str());
check("void f() {\n" // #5475
" std::string(\"a\") + \"a\";\n"
"}\n"
"void f(std::string& a) {\n"
" a.erase(3) + \"suf\";\n"
"}\n", /*inconclusive*/ true);
ASSERT_EQUALS("[test.cpp:2]: (warning, inconclusive) Found suspicious operator '+', result is not used.\n"
"[test.cpp:5]: (warning, inconclusive) Found suspicious operator '+', result is not used.\n",
errout.str());
}
void vardecl() {

View File

@ -993,6 +993,11 @@ private:
" if (p);\n"
"}");
ASSERT_EQUALS("", errout.str());
check("struct S { struct T { char c; } *p; };\n" // #6541
"char f(S* s) { return s->p ? 'a' : s->p->c; }\n");
ASSERT_EQUALS("[test.cpp:2] -> [test.cpp:2]: (warning) Either the condition 's->p' is redundant or there is possible null pointer dereference: p.\n",
errout.str());
}
void nullpointer5() {