42 lines · plain
1.. title:: clang-tidy - bugprone-swapped-arguments2 3bugprone-swapped-arguments4==========================5 6Finds potentially swapped arguments by examining implicit conversions.7It analyzes the types of the arguments being passed to a function and compares8them to the expected types of the corresponding parameters. If there is a9mismatch or an implicit conversion that indicates a potential swap, a warning10is 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 22Covers a wide range of implicit conversions, including:23- User-defined conversions24- Conversions from floating-point types to boolean or integral types25- Conversions from integral types to boolean or floating-point types26- Conversions from boolean to integer types or floating-point types27- Conversions from (member) pointers to boolean28 29It is important to note that for most argument swaps, the types need to match30exactly. However, there are exceptions to this rule. Specifically, when the31swapped argument is of integral type, an exact match is not always necessary.32Implicit casts from other integral types are also accepted. Similarly, when33dealing with floating-point arguments, implicit casts between different34floating-point types are considered acceptable.35 36To avoid confusion, swaps where both swapped arguments are of integral types or37both are of floating-point types do not trigger the warning. In such cases,38it's assumed that the developer intentionally used different integral or39floating-point types and does not raise a warning. This approach prevents false40positives and provides flexibility in handling situations where varying41integral or floating-point types are intentionally utilized.42