From 2535bf984bb10a2eb93a5f3e46fff77ec74011b5 Mon Sep 17 00:00:00 2001 From: Julien Marrec Date: Thu, 10 Feb 2022 21:01:12 +0100 Subject: [PATCH] Allow passing std::string_view by value (#3817) --- lib/checkother.cpp | 2 +- test/testother.cpp | 10 ++++++++++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/lib/checkother.cpp b/lib/checkother.cpp index 723b5b4bd..a15f4e543 100644 --- a/lib/checkother.cpp +++ b/lib/checkother.cpp @@ -1300,7 +1300,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; diff --git a/test/testother.cpp b/test/testother.cpp index b79404433..7ffd07728 100644 --- a/test/testother.cpp +++ b/test/testother.cpp @@ -1650,6 +1650,16 @@ private: 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());