diff --git a/rules/S7121/cfamily/rule.adoc b/rules/S7121/cfamily/rule.adoc index 541aad37cbe..7ff64e7df8c 100644 --- a/rules/S7121/cfamily/rule.adoc +++ b/rules/S7121/cfamily/rule.adoc @@ -3,7 +3,7 @@ Unnecessary calls to `c_str()` or `data()` reduce readability and performance. == Why is this an issue? In the process of refactoring code from C to C++, it can easily happen that redundant calls to `c_str()` -or `data()` on standard strings or string_views get introduced when the original string could be used directly. +or `data()` on standard strings get introduced when the original string could be used directly. [source,cpp,diff-id=1,diff-type=noncompliant] ---- @@ -11,15 +11,15 @@ void takeString(std::string const& dest); void takeStringView(std::string_view dest); -void func(std::string const& src, std::string_view srcView) { +void func(std::string const& src) { takeString(src.c_str()); // Noncompliant: redundant copy - takeStringView(src.c_str()); // Noncompliant: redundant copy - takeStringView(srcView.data()); // Noncompliant: redundant crossing of the content + takeStringView(src.c_str()); // Noncompliant: redundant crossing of the conten + takeStringView(src.data()); // Noncompliant: redundant crossing of the content } ---- Not only is this unnecessary step a readability issue, but it will force the creation of useless intermediary strings, -or force to cross the content of a string_view to search for the null-terminator, reducing the performance. +and force to cross the content of a string to search for the null-terminator, reducing the performance. The call to `c_str()` or `data()` should be removed. [source,cpp,diff-id=1,diff-type=compliant]