rspec/rules/S5268/cfamily/rule.adoc

41 lines
913 B
Plaintext
Raw Normal View History

== Why is this an issue?
2021-04-28 16:49:39 +02:00
Contrary to what you might believe, using the addition operator does not append an integral constant to a string. Adding a ``++char++`` or an integral to a string pointer does not append it to the string. What it does instead is incrementing the string pointer by a value defined by this ``++char++`` or integral.
=== Noncompliant code example
2021-04-28 16:49:39 +02:00
2022-02-04 17:28:24 +01:00
[source,cpp]
2021-04-28 16:49:39 +02:00
----
int a;
std::string str1 = "foo" + a; // Noncompliant
char[] b = "foo";
char* str2 = b + 'b'; // Noncompliant{code}
----
=== Compliant solution
2021-04-28 16:49:39 +02:00
2022-02-04 17:28:24 +01:00
[source,cpp]
2021-04-28 16:49:39 +02:00
----
int a;
std::string str1 = "foo" + std::to_string(a); // Compliant
std::string s = "foo";
std::string str2 = s + 'b'; // Compliant{code}
----
ifdef::env-github,rspecator-view[]
'''
== Implementation Specification
(visible only on this page)
=== Message
adding "XXX" to a string pointer does not append to the string
endif::env-github,rspecator-view[]