xref: /llvm-project/clang-tools-extra/test/clang-tidy/checkers/bugprone/swapped-arguments.cpp (revision cc4f86562127f29560abb54d0c11f277b3373a2a)
1 // RUN: %check_clang_tidy %s bugprone-swapped-arguments %t
2 
3 void F(int, double);
4 
5 int SomeFunction();
6 
7 template <typename T, typename U>
G(T a,U b)8 void G(T a, U b) {
9   F(a, b); // no-warning
10   F(2.0, 4);
11 // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: argument with implicit conversion from 'double' to 'int' followed by argument converted from 'int' to 'double', potentially swapped arguments.
12 // CHECK-FIXES: F(4, 2.0)
13 }
14 
15 void funShortFloat(short, float);
16 void funFloatFloat(float, float);
17 void funBoolShort(bool, short);
18 void funBoolFloat(bool, float);
19 
foo()20 void foo() {
21   F(1.0, 3);
22 // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: argument with implicit conversion from 'double' to 'int' followed by argument converted from 'int' to 'double', potentially swapped arguments.
23 // CHECK-FIXES: F(3, 1.0)
24 
25 #define M(x, y) x##y()
26 
27   double b = 1.0;
28   F(b, M(Some, Function));
29 // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: argument with implicit conversion from 'double' to 'int' followed by argument converted from 'int' to 'double', potentially swapped arguments.
30 // CHECK-FIXES: F(M(Some, Function), b);
31 
32 #define N F(b, SomeFunction())
33 
34   N;
35 // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: argument with implicit conversion from 'double' to 'int' followed by argument converted from 'int' to 'double', potentially swapped arguments.
36 // In macro, don't emit fixits.
37 // CHECK-FIXES: #define N F(b, SomeFunction())
38 
39   G(b, 3);
40   G(3, 1.0);
41   G(0, 0);
42 
43   F(1.0, 1.0);    // no-warning
44   F(3, 1.0);      // no-warning
45   F(true, false); // no-warning
46   F(0, 'c');      // no-warning
47 
48 #define APPLY(f, x, y) f(x, y)
49   APPLY(F, 1.0, 3);
50 // CHECK-MESSAGES: :[[@LINE-1]]:9: warning: argument with implicit conversion from 'double' to 'int' followed by argument converted from 'int' to 'double', potentially swapped arguments.
51 // CHECK-FIXES: APPLY(F, 3, 1.0);
52 
53 #define PARAMS 1.0, 3
54 #define CALL(P) F(P)
55   CALL(PARAMS);
56 // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: argument with implicit conversion from 'double' to 'int' followed by argument converted from 'int' to 'double', potentially swapped arguments.
57 // In macro, don't emit fixits.
58 
59   funShortFloat(5.0, 1U);
60   // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: argument with implicit conversion from 'double' to 'short' followed by argument converted from 'unsigned int' to 'float', potentially swapped arguments.
61   // CHECK-FIXES: funShortFloat(1U, 5.0);
62   funShortFloat(5.0, 1);
63   // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: argument with implicit conversion from 'double' to 'short' followed by argument converted from 'int' to 'float', potentially swapped arguments.
64   // CHECK-FIXES: funShortFloat(1, 5.0);
65   funShortFloat(5.0f, 1);
66   // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: argument with implicit conversion from 'float' to 'short' followed by argument converted from 'int' to 'float', potentially swapped arguments.
67   // CHECK-FIXES: funShortFloat(1, 5.0f);
68 
69   funFloatFloat(1.0f, 2.0);
70 
71   funBoolShort(1U, true);
72   // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: argument with implicit conversion from 'unsigned int' to 'bool' followed by argument converted from 'bool' to 'short', potentially swapped arguments.
73   // CHECK-FIXES: funBoolShort(true, 1U);
74 
75   funBoolFloat(1.0, true);
76   // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: argument with implicit conversion from 'double' to 'bool' followed by argument converted from 'bool' to 'float', potentially swapped arguments.
77   // CHECK-FIXES: funBoolFloat(true, 1.0);
78 }
79