| 1 |
.. title:: clang-tidy - bugprone-string-integer-assignment |
| 2 |
|
| 3 |
bugprone-string-integer-assignment |
| 4 |
================================== |
| 5 |
|
| 6 |
The check finds assignments of an integer to ``std::basic_string<CharT>`` |
| 7 |
(``std::string``, ``std::wstring``, etc.). The source of the problem is the |
| 8 |
following assignment operator of ``std::basic_string<CharT>``: |
| 9 |
|
| 10 |
.. code-block:: c++ |
| 11 |
|
| 12 |
basic_string& operator=( CharT ch ); |
| 13 |
|
| 14 |
Numeric types can be implicitly casted to character types. |
| 15 |
|
| 16 |
.. code-block:: c++ |
| 17 |
|
| 18 |
std::string s; |
| 19 |
int x = 5965; |
| 20 |
s = 6; |
| 21 |
s = x; |
| 22 |
|
| 23 |
Use the appropriate conversion functions or character literals. |
| 24 |
|
| 25 |
.. code-block:: c++ |
| 26 |
|
| 27 |
std::string s; |
| 28 |
int x = 5965; |
| 29 |
s = '6'; |
| 30 |
s = std::to_string(x); |
| 31 |
|
| 32 |
In order to suppress false positives, use an explicit cast. |
| 33 |
|
| 34 |
.. code-block:: c++ |
| 35 |
|
| 36 |
std::string s; |
| 37 |
s = static_cast<char>(6); |