| 1 |
.. title:: clang-tidy - bugprone-swapped-arguments |
| 2 |
|
| 3 |
bugprone-swapped-arguments |
| 4 |
========================== |
| 5 |
|
| 6 |
Finds potentially swapped arguments by examining implicit conversions. |
| 7 |
It analyzes the types of the arguments being passed to a function and compares |
| 8 |
them to the expected types of the corresponding parameters. If there is a |
| 9 |
mismatch or an implicit conversion that indicates a potential swap, a warning |
| 10 |
is raised. |
| 11 |
|
| 12 |
.. code-block:: c++ |
| 13 |
|
| 14 |
void printNumbers(int a, float b); |
| 15 |
|
| 16 |
int main() { |
| 17 |
// Swapped arguments: float passed as int, int as float) |
| 18 |
printNumbers(10.0f, 5); |
| 19 |
return 0; |
| 20 |
} |
| 21 |
|
| 22 |
Covers a wide range of implicit conversions, including: |
| 23 |
- User-defined conversions |
| 24 |
- Conversions from floating-point types to boolean or integral types |
| 25 |
- Conversions from integral types to boolean or floating-point types |
| 26 |
- Conversions from boolean to integer types or floating-point types |
| 27 |
- Conversions from (member) pointers to boolean |
| 28 |
|
| 29 |
It is important to note that for most argument swaps, the types need to match |
| 30 |
exactly. However, there are exceptions to this rule. Specifically, when the |
| 31 |
swapped argument is of integral type, an exact match is not always necessary. |
| 32 |
Implicit casts from other integral types are also accepted. Similarly, when |
| 33 |
dealing with floating-point arguments, implicit casts between different |
| 34 |
floating-point types are considered acceptable. |
| 35 |
|
| 36 |
To avoid confusion, swaps where both swapped arguments are of integral types or |
| 37 |
both are of floating-point types do not trigger the warning. In such cases, it's |
| 38 |
assumed that the developer intentionally used different integral or |
| 39 |
floating-point types and does not raise a warning. This approach prevents false |
| 40 |
positives and provides flexibility in handling situations where varying integral |
| 41 |
or floating-point types are intentionally utilized. |