Allow passing std::string_view by value (#3817)

This commit is contained in:
Julien Marrec 2022-02-10 21:01:12 +01:00 committed by GitHub
parent cd7532df21
commit 2535bf984b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 11 additions and 1 deletions

View File

@ -1300,7 +1300,7 @@ void CheckOther::checkPassByReference()
bool inconclusive = false; 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. // Ensure that it is a large object.
if (!var->type()->classScope) if (!var->type()->classScope)
inconclusive = true; inconclusive = true;

View File

@ -1650,6 +1650,16 @@ private:
check("void f(const std::string &str) {}"); check("void f(const std::string &str) {}");
ASSERT_EQUALS("", errout.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<int> v) {}"); check("void f(const std::vector<int> v) {}");
ASSERT_EQUALS("[test.cpp:1]: (performance) Function parameter 'v' should be passed by const reference.\n", errout.str()); ASSERT_EQUALS("[test.cpp:1]: (performance) Function parameter 'v' should be passed by const reference.\n", errout.str());