xref: /llvm-project/clang-tools-extra/docs/clang-tidy/checks/bugprone/swapped-arguments.rst (revision cc4f86562127f29560abb54d0c11f277b3373a2a)
1.. title:: clang-tidy - bugprone-swapped-arguments
2
3bugprone-swapped-arguments
4==========================
5
6Finds potentially swapped arguments by examining implicit conversions.
7It analyzes the types of the arguments being passed to a function and compares
8them to the expected types of the corresponding parameters. If there is a
9mismatch or an implicit conversion that indicates a potential swap, a warning
10is 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 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
29It is important to note that for most argument swaps, the types need to match
30exactly. However, there are exceptions to this rule. Specifically, when the
31swapped argument is of integral type, an exact match is not always necessary.
32Implicit casts from other integral types are also accepted. Similarly, when
33dealing with floating-point arguments, implicit casts between different
34floating-point types are considered acceptable.
35
36To avoid confusion, swaps where both swapped arguments are of integral types or
37both are of floating-point types do not trigger the warning. In such cases, it's
38assumed that the developer intentionally used different integral or
39floating-point types and does not raise a warning. This approach prevents false
40positives and provides flexibility in handling situations where varying integral
41or floating-point types are intentionally utilized.
42