From ae791e265e8e5b6b264874aabaf9932a980fdc0d Mon Sep 17 00:00:00 2001 From: Julien Marrec Date: Thu, 10 Feb 2022 14:36:37 +0100 Subject: [PATCH 1/2] Add a failing test for passing std::string_view by value (as it should) --- test/testother.cpp | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/test/testother.cpp b/test/testother.cpp index 57b48885154..ed31a7ce164 100644 --- a/test/testother.cpp +++ b/test/testother.cpp @@ -1605,6 +1605,16 @@ class TestOther : public TestFixture { check("void f(const std::string &str) {}"); ASSERT_EQUALS("", errout.str()); + // The idiomatic way of passing a std::string_view is by value + check("void f(const std::string_view str) {}"); + ASSERT_EQUALS("", errout.str()); + + check("void f(std::string_view str) {}"); + ASSERT_EQUALS("", errout.str()); + + check("void f(const std::string_view &str) {}"); + ASSERT_EQUALS("", errout.str()); + check("void f(const std::vector v) {}"); ASSERT_EQUALS("[test.cpp:1]: (performance) Function parameter 'v' should be passed by const reference.\n", errout.str()); From ac2c55777463c533231c51098fa49312a02c4dd6 Mon Sep 17 00:00:00 2001 From: Julien Marrec Date: Thu, 10 Feb 2022 14:37:35 +0100 Subject: [PATCH 2/2] Allow passing std::string_view by value (as it should) --- lib/checkother.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/checkother.cpp b/lib/checkother.cpp index bbe613fa22f..fd0434a5385 100644 --- a/lib/checkother.cpp +++ b/lib/checkother.cpp @@ -1301,7 +1301,7 @@ void CheckOther::checkPassByReference() bool inconclusive = false; - if (var->valueType() && var->valueType()->type == ValueType::Type::CONTAINER) {} else if (var->type() && !var->type()->isEnumType()) { // Check if type is a struct or class. + if (var->valueType() && var->valueType()->type == ValueType::Type::CONTAINER && !var->valueType()->container->view) {} else if (var->type() && !var->type()->isEnumType()) { // Check if type is a struct or class. // Ensure that it is a large object. if (!var->type()->classScope) inconclusive = true;