C & C++

C & C++

Made by DeepSource

Buggy or lossy integer to string assignment CXX-W2060

Bug risk
Major

Numeric types can be implicitly casted to character types. This allows for the below overload to be used when assigning a numerical value to string.\

To fix this issue, use the appropriate conversion functions or character literals to avoid implicit conversion of numeric types.

basic_string& operator=( CharT ch );

This can lead to buggy logic.

std::string s;
int x = 5965;
s = 6;
s = x;

Bad Practice

std::string s;
int x = 5965;
s = 6;
s = x;

Recommended

std::string s;
int x = 5965;
s = '6';
s = std::to_string(x);
s = static_cast<char>(6);