From 7a61735159d1d28a8239cbd7da7e75e4044dce4b Mon Sep 17 00:00:00 2001 From: chrchr-github <78114321+chrchr-github@users.noreply.github.com> Date: Thu, 11 Jul 2024 12:38:20 +0200 Subject: [PATCH 1/3] Update testother.cpp --- test/testother.cpp | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/test/testother.cpp b/test/testother.cpp index da58be85e81..77b3572a71e 100644 --- a/test/testother.cpp +++ b/test/testother.cpp @@ -9046,6 +9046,20 @@ class TestOther : public TestFixture { TODO_ASSERT_EQUALS("", "[test.cpp:16] -> [test.cpp:18]: (style) The comparison 'c == m->get()' is always true because 'c' and 'm->get()' represent the same value.\n", errout_str()); + + check("struct S {\n" // #12927 + " const std::string & f() const { return str; }\n" + " std::string str;\n" + "};\n" + "void f(const S* s) {\n" + " const std::string v{ s->f() };\n" + " if (v.empty()) {}\n" + "}\n" + "void g(const S* s) {\n" + " const std::string w(s->f());\n" + " if (w.empty()) {}\n" + "}\n"); + ASSERT_EQUALS("[test.cpp:6]: (performance, inconclusive) Use const reference for 's' to avoid unnecessary data copying.\n", errout_str()); } void checkNegativeShift() { From 4173e7c5bea8df974303d5ab93b3ad8738dca94f Mon Sep 17 00:00:00 2001 From: chrchr-github <78114321+chrchr-github@users.noreply.github.com> Date: Thu, 11 Jul 2024 12:40:11 +0200 Subject: [PATCH 2/3] Update checkother.cpp --- lib/checkother.cpp | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/lib/checkother.cpp b/lib/checkother.cpp index 40fd10a97d4..50a5f9b9405 100644 --- a/lib/checkother.cpp +++ b/lib/checkother.cpp @@ -2920,9 +2920,11 @@ void CheckOther::checkRedundantCopy() const Token* startTok = var->nameToken(); if (startTok->strAt(1) == "=") // %type% %name% = ... ; ; - else if (Token::Match(startTok->next(), "(|{") && var->isClass() && var->typeScope()) { + else if (Token::Match(startTok->next(), "(|{") && var->isClass()) { + if (!var->typeScope() && !(var->valueType() && var->valueType()->container)) + continue; // Object is instantiated. Warn if constructor takes arguments by value. - if (constructorTakesReference(var->typeScope())) + if (var->typeScope() && constructorTakesReference(var->typeScope())) continue; } else if (Token::simpleMatch(startTok->next(), ";") && startTok->next()->isSplittedVarDeclEq()) { startTok = startTok->tokAt(2); @@ -2934,7 +2936,7 @@ void CheckOther::checkRedundantCopy() continue; if (!Token::Match(tok->previous(), "%name% (")) continue; - if (!Token::Match(tok->link(), ") )| ;")) // bailout for usage like "const A a = getA()+3" + if (!Token::Match(tok->link(), ") )|}| ;")) // bailout for usage like "const A a = getA()+3" continue; const Token* dot = tok->astOperand1(); From 3180933921233c1cb80d45c615495ca7ff1ec9c9 Mon Sep 17 00:00:00 2001 From: chrchr-github <78114321+chrchr-github@users.noreply.github.com> Date: Thu, 11 Jul 2024 12:47:10 +0200 Subject: [PATCH 3/3] Update testother.cpp --- test/testother.cpp | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/test/testother.cpp b/test/testother.cpp index 77b3572a71e..260a5aa34a3 100644 --- a/test/testother.cpp +++ b/test/testother.cpp @@ -9059,7 +9059,9 @@ class TestOther : public TestFixture { " const std::string w(s->f());\n" " if (w.empty()) {}\n" "}\n"); - ASSERT_EQUALS("[test.cpp:6]: (performance, inconclusive) Use const reference for 's' to avoid unnecessary data copying.\n", errout_str()); + ASSERT_EQUALS("[test.cpp:6]: (performance, inconclusive) Use const reference for 'v' to avoid unnecessary data copying.\n" + "[test.cpp:10]: (performance, inconclusive) Use const reference for 'w' to avoid unnecessary data copying.\n", + errout_str()); } void checkNegativeShift() {