| 1 |
.. title:: clang-tidy - bugprone-string-constructor |
| 2 |
|
| 3 |
bugprone-string-constructor |
| 4 |
=========================== |
| 5 |
|
| 6 |
Finds string constructors that are suspicious and probably errors. |
| 7 |
|
| 8 |
A common mistake is to swap parameters to the 'fill' string-constructor. |
| 9 |
|
| 10 |
Examples: |
| 11 |
|
| 12 |
.. code-block:: c++ |
| 13 |
|
| 14 |
std::string str('x', 50); // should be str(50, 'x') |
| 15 |
|
| 16 |
Calling the string-literal constructor with a length bigger than the literal is |
| 17 |
suspicious and adds extra random characters to the string. |
| 18 |
|
| 19 |
Examples: |
| 20 |
|
| 21 |
.. code-block:: c++ |
| 22 |
|
| 23 |
std::string("test", 200); // Will include random characters after "test". |
| 24 |
std::string_view("test", 200); |
| 25 |
|
| 26 |
Creating an empty string from constructors with parameters is considered |
| 27 |
suspicious. The programmer should use the empty constructor instead. |
| 28 |
|
| 29 |
Examples: |
| 30 |
|
| 31 |
.. code-block:: c++ |
| 32 |
|
| 33 |
std::string("test", 0); // Creation of an empty string. |
| 34 |
std::string_view("test", 0); |
| 35 |
|
| 36 |
Options |
| 37 |
------- |
| 38 |
|
| 39 |
.. option:: WarnOnLargeLength |
| 40 |
|
| 41 |
When `true`, the check will warn on a string with a length greater than |
| 42 |
:option:`LargeLengthThreshold`. Default is `true`. |
| 43 |
|
| 44 |
.. option:: LargeLengthThreshold |
| 45 |
|
| 46 |
An integer specifying the large length threshold. Default is `0x800000`. |
| 47 |
|
| 48 |
.. option:: StringNames |
| 49 |
|
| 50 |
Default is `::std::basic_string;::std::basic_string_view`. |
| 51 |
|
| 52 |
Semicolon-delimited list of class names to apply this check to. |
| 53 |
By default `::std::basic_string` applies to ``std::string`` and |
| 54 |
``std::wstring``. Set to e.g. `::std::basic_string;llvm::StringRef;QString` |
| 55 |
to perform this check on custom classes. |