Create rule S7121 Calls to c_str() should not implicitly recreate strings or string_views CPP-3435
This commit is contained in:
parent
ee3e232e09
commit
289e7cf5e9
23
rules/S7121/cfamily/metadata.json
Normal file
23
rules/S7121/cfamily/metadata.json
Normal file
@ -0,0 +1,23 @@
|
||||
{
|
||||
"title": "Calls to c_str() should not implicitly recreate strings or string_views",
|
||||
"type": "CODE_SMELL",
|
||||
"status": "ready",
|
||||
"remediation": {
|
||||
"func": "Constant\/Issue",
|
||||
"constantCost": "5min"
|
||||
},
|
||||
"tags": [
|
||||
],
|
||||
"defaultSeverity": "Major",
|
||||
"ruleSpecification": "RSPEC-7121",
|
||||
"sqKey": "S7121",
|
||||
"scope": "All",
|
||||
"defaultQualityProfiles": ["Sonar way"],
|
||||
"quickfix": "covered",
|
||||
"code": {
|
||||
"impacts": {
|
||||
"MAINTAINABILITY": "MEDIUM"
|
||||
},
|
||||
"attribute": "EFFICIENT"
|
||||
}
|
||||
}
|
57
rules/S7121/cfamily/rule.adoc
Normal file
57
rules/S7121/cfamily/rule.adoc
Normal file
@ -0,0 +1,57 @@
|
||||
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 get introduced when the original object could be used directly.
|
||||
|
||||
[source,cpp,diff-id=1,diff-type=noncompliant]
|
||||
----
|
||||
void takeString(std::string const& dest);
|
||||
|
||||
void takeStringView(std::string_view dest);
|
||||
|
||||
void func(std::string const& src) {
|
||||
takeString(src.c_str()); // Noncompliant: redundant copy
|
||||
takeStringView(src.c_str()); // Noncompliant: redundant crossing of the content
|
||||
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 require searching the content of a string 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]
|
||||
----
|
||||
void takeString(std::string const& dest);
|
||||
|
||||
void takeStringView(std::string_view dest);
|
||||
|
||||
void func(std::string const& src) {
|
||||
takeString(src); // Compliant
|
||||
takeStringView(src); // Compliant
|
||||
takeStringView(src); // Compliant
|
||||
}
|
||||
----
|
||||
|
||||
=== Exception
|
||||
|
||||
This rule does not raise when explicitly creating a string or a string_view from `c_str()`.
|
||||
|
||||
[source,cpp]
|
||||
----
|
||||
void takeString(std::string const& dest);
|
||||
|
||||
void func(std::string const& src) {
|
||||
takeString(std::string(src.c_str())); // Compliant by exception
|
||||
}
|
||||
----
|
||||
|
||||
This operation could be done on purpose in the rare case where `src` is a string containing embedded null-terminators, in order to only keep the content up to the first null-terminator in `dest`.
|
||||
|
||||
== Resources
|
||||
|
||||
=== Related rules
|
||||
|
||||
* S6231 - "std::string_view" and "std::span" parameters should be directly constructed from sequences
|
2
rules/S7121/metadata.json
Normal file
2
rules/S7121/metadata.json
Normal file
@ -0,0 +1,2 @@
|
||||
{
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user