From 71f9a7269f01f41f999827a0963ea365694155f3 Mon Sep 17 00:00:00 2001 From: chrchr-github <78114321+chrchr-github@users.noreply.github.com> Date: Tue, 2 Aug 2022 21:43:18 +0200 Subject: [PATCH] Add test for #6541, #5475, avoid duplicate warning (#4335) * Add test for #6541, avoid duplicate warning * Add test for #5475 * Fix test --- lib/checknullpointer.cpp | 3 ++- test/testincompletestatement.cpp | 10 ++++++++++ test/testnullpointer.cpp | 5 +++++ 3 files changed, 17 insertions(+), 1 deletion(-) diff --git a/lib/checknullpointer.cpp b/lib/checknullpointer.cpp index b10522b3a..4fbe58785 100644 --- a/lib/checknullpointer.cpp +++ b/lib/checknullpointer.cpp @@ -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? diff --git a/test/testincompletestatement.cpp b/test/testincompletestatement.cpp index 755ae7983..37a017cde 100644 --- a/test/testincompletestatement.cpp +++ b/test/testincompletestatement.cpp @@ -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() { diff --git a/test/testnullpointer.cpp b/test/testnullpointer.cpp index 239be7e6e..45c13b658 100644 --- a/test/testnullpointer.cpp +++ b/test/testnullpointer.cpp @@ -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() {