From 99ffb6d8f821623823546cf7841eb8fdf8fb3354 Mon Sep 17 00:00:00 2001 From: chrchr Date: Tue, 2 Aug 2022 17:52:05 +0200 Subject: [PATCH 1/3] Add test for #6541, avoid duplicate warning --- lib/checknullpointer.cpp | 3 ++- test/testnullpointer.cpp | 5 +++++ 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/lib/checknullpointer.cpp b/lib/checknullpointer.cpp index b10522b3ace..4fbe58785a0 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/testnullpointer.cpp b/test/testnullpointer.cpp index 239be7e6eea..45c13b65842 100644 --- a/test/testnullpointer.cpp +++ b/test/testnullpointer.cpp @@ -993,6 +993,11 @@ class TestNullPointer : public TestFixture { " 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() { From c6ce3550bed45e55c2f60f3f8291a2798a191756 Mon Sep 17 00:00:00 2001 From: chrchr Date: Tue, 2 Aug 2022 18:28:17 +0200 Subject: [PATCH 2/3] Add test for #5475 --- test/testincompletestatement.cpp | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/test/testincompletestatement.cpp b/test/testincompletestatement.cpp index 755ae798356..913c39c1627 100644 --- a/test/testincompletestatement.cpp +++ b/test/testincompletestatement.cpp @@ -667,6 +667,17 @@ class TestIncompleteStatement : public TestFixture { " *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" + " return 0;\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:6]: (warning, inconclusive) Found suspicious operator '+', result is not used.\n", + errout.str()); } void vardecl() { From 93ce6b66737eeef0de55c78b4945c7e21abf74a3 Mon Sep 17 00:00:00 2001 From: chrchr Date: Tue, 2 Aug 2022 18:55:40 +0200 Subject: [PATCH 3/3] Fix test --- test/testincompletestatement.cpp | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/test/testincompletestatement.cpp b/test/testincompletestatement.cpp index 913c39c1627..37a017cde9e 100644 --- a/test/testincompletestatement.cpp +++ b/test/testincompletestatement.cpp @@ -670,13 +670,12 @@ class TestIncompleteStatement : public TestFixture { check("void f() {\n" // #5475 " std::string(\"a\") + \"a\";\n" - " return 0;\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:6]: (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()); }