From f9d31a7dde3be0f417779ecaee33c6e32464cb73 Mon Sep 17 00:00:00 2001 From: chrchr-github Date: Sat, 20 Apr 2024 15:18:35 +0200 Subject: [PATCH] Fix #12626 FP returnByReference with string_view or template --- lib/checkclass.cpp | 5 +++++ test/testclass.cpp | 13 +++++++++++++ 2 files changed, 18 insertions(+) diff --git a/lib/checkclass.cpp b/lib/checkclass.cpp index 018b700104b..917489f0b2c 100644 --- a/lib/checkclass.cpp +++ b/lib/checkclass.cpp @@ -3330,6 +3330,11 @@ void CheckClass::checkReturnByReference() continue; if (func.isImplicitlyVirtual()) continue; + if (func.isOperator()) + continue; + if (const Library::Container* container = mSettings->library.detectContainer(func.retDef)) + if (container->view) + continue; if (const Variable* var = getSingleReturnVar(func.functionScope)) { if (!var->valueType()) continue; diff --git a/test/testclass.cpp b/test/testclass.cpp index 6f37f0c3c92..77a7432538b 100644 --- a/test/testclass.cpp +++ b/test/testclass.cpp @@ -8955,6 +8955,19 @@ class TestClass : public TestFixture { checkReturnByReference("struct S { S(); };\n" // #12620 "S::S() = delete;\n"); ASSERT_EQUALS("", errout_str()); // don't crash + + checkReturnByReference("struct S {\n" // #12626 + " std::string s;\n" + " operator std::string_view() const { return s; }\n" + " std::string_view get() const { return s; }\n" + "};\n" + "template\n" + "struct U {\n" + " T t;\n" + " operator const T& () const { return t; }\n" + "};\n" + "U u;\n"); + ASSERT_EQUALS("", errout_str()); } };