From 158f3d494e58872d390a489113490e37e0b66d30 Mon Sep 17 00:00:00 2001 From: chrchr-github <78114321+chrchr-github@users.noreply.github.com> Date: Mon, 24 Jan 2022 21:50:50 +0100 Subject: [PATCH] Fix #10143 false positive: redundantInitialization with std::shared_ptr (#3746) --- lib/checkother.cpp | 19 ++++++++++++------- test/testother.cpp | 7 +++++++ 2 files changed, 19 insertions(+), 7 deletions(-) diff --git a/lib/checkother.cpp b/lib/checkother.cpp index 981cdec77..6e66589e9 100644 --- a/lib/checkother.cpp +++ b/lib/checkother.cpp @@ -479,16 +479,21 @@ void CheckOther::checkRedundantAssignment() // todo: check static variables continue; - // If there is a custom assignment operator => this is inconclusive bool inconclusive = false; - if (mTokenizer->isCPP() && tok->astOperand1()->valueType() && tok->astOperand1()->valueType()->typeScope) { - const std::string op = "operator" + tok->str(); - for (const Function &f : tok->astOperand1()->valueType()->typeScope->functionList) { - if (f.name() == op) { - inconclusive = true; - break; + if (mTokenizer->isCPP() && tok->astOperand1()->valueType()) { + // If there is a custom assignment operator => this is inconclusive + if (tok->astOperand1()->valueType()->typeScope) { + const std::string op = "operator" + tok->str(); + for (const Function& f : tok->astOperand1()->valueType()->typeScope->functionList) { + if (f.name() == op) { + inconclusive = true; + break; + } } } + // assigning a smart pointer has side effects + if (tok->astOperand1()->valueType()->type == ValueType::SMART_POINTER) + break; } if (inconclusive && !mSettings->certainty.isEnabled(Certainty::inconclusive)) continue; diff --git a/test/testother.cpp b/test/testother.cpp index 110535000..df457a479 100644 --- a/test/testother.cpp +++ b/test/testother.cpp @@ -7875,6 +7875,13 @@ private: " e = dostuff();\n" "}"); ASSERT_EQUALS("", errout.str()); + + check("void f() {\n" // #10143 + " std::shared_ptr i = g();\n" + " h();\n" + " i = nullptr;\n" + "}\n"); + ASSERT_EQUALS("", errout.str()); } void redundantMemWrite() {