1 // RUN: %check_clang_tidy %s bugprone-misplaced-widening-cast %t -- -config="{CheckOptions: [{key: bugprone-misplaced-widening-cast.CheckImplicitCasts, value: 1}]}" --
2 
func(long arg)3 void func(long arg) {}
4 
assign(int a,int b)5 void assign(int a, int b) {
6   long l;
7 
8   l = a * b;
9   // CHECK-MESSAGES: :[[@LINE-1]]:7: warning: either cast from 'int' to 'long' is ineffective, or there is loss of precision before the conversion [bugprone-misplaced-widening-cast]
10   l = (long)(a * b);
11   // CHECK-MESSAGES: :[[@LINE-1]]:7: warning: either cast from 'int' to 'long'
12   l = (long)a * b;
13 
14   l = a << 8;
15   // CHECK-MESSAGES: :[[@LINE-1]]:7: warning: either cast from 'int' to 'long'
16   l = (long)(a << 8);
17   // CHECK-MESSAGES: :[[@LINE-1]]:7: warning: either cast from 'int' to 'long'
18   l = (long)b << 8;
19 
20   l = static_cast<long>(a * b);
21   // CHECK-MESSAGES: :[[@LINE-1]]:7: warning: either cast from 'int' to 'long'
22 }
23 
compare(int a,int b,long c)24 void compare(int a, int b, long c) {
25   bool l;
26 
27   l = a * b == c;
28   // CHECK-MESSAGES: :[[@LINE-1]]:7: warning: either cast from 'int' to 'long'
29   l = c == a * b;
30   // CHECK-MESSAGES: :[[@LINE-1]]:12: warning: either cast from 'int' to 'long'
31   l = (long)(a * b) == c;
32   // CHECK-MESSAGES: :[[@LINE-1]]:7: warning: either cast from 'int' to 'long'
33   l = c == (long)(a * b);
34   // CHECK-MESSAGES: :[[@LINE-1]]:12: warning: either cast from 'int' to 'long'
35   l = (long)a * b == c;
36   l = c == (long)a * b;
37 }
38 
init(unsigned int n)39 void init(unsigned int n) {
40   long l1 = n << 8;
41   // CHECK-MESSAGES: :[[@LINE-1]]:13: warning: either cast from 'unsigned int' to 'long'
42   long l2 = (long)(n << 8);
43   // CHECK-MESSAGES: :[[@LINE-1]]:13: warning: either cast from 'unsigned int' to 'long'
44   long l3 = (long)n << 8;
45 }
46 
call(unsigned int n)47 void call(unsigned int n) {
48   func(n << 8);
49   // CHECK-MESSAGES: :[[@LINE-1]]:8: warning: either cast from 'unsigned int' to 'long'
50   func((long)(n << 8));
51   // CHECK-MESSAGES: :[[@LINE-1]]:8: warning: either cast from 'unsigned int' to 'long'
52   func((long)n << 8);
53 }
54 
ret(int a)55 long ret(int a) {
56   if (a < 0) {
57     return a * 1000;
58     // CHECK-MESSAGES: :[[@LINE-1]]:12: warning: either cast from 'int' to 'long'
59   } else if (a > 0) {
60     return (long)(a * 1000);
61     // CHECK-MESSAGES: :[[@LINE-1]]:12: warning: either cast from 'int' to 'long'
62   } else {
63     return (long)a * 1000;
64   }
65 }
66 
dontwarn1(unsigned char a,int i,unsigned char * p)67 void dontwarn1(unsigned char a, int i, unsigned char *p) {
68   long l;
69   // The result is a 9 bit value, there is no truncation in the implicit cast.
70   l = (long)(a + 15);
71   // The result is a 12 bit value, there is no truncation in the implicit cast.
72   l = (long)(a << 4);
73   // The result is a 3 bit value, there is no truncation in the implicit cast.
74   l = (long)((i % 5) + 1);
75   // The result is a 16 bit value, there is no truncation in the implicit cast.
76   l = (long)(((*p) << 8) + *(p + 1));
77 }
78 
79 template <class T> struct DontWarn2 {
assignDontWarn280   void assign(T a, T b) {
81     long l;
82     l = (long)(a * b);
83   }
84 };
85 DontWarn2<int> DW2;
86 
87 // Cast is not suspicious when casting macro.
88 #define A  (X<<2)
macro1(int X)89 long macro1(int X) {
90   return (long)A;
91 }
92 
93 // Don't warn about cast in macro.
94 #define B(X,Y)   (long)(X*Y)
macro2(int x,int y)95 long macro2(int x, int y) {
96   return B(x,y);
97 }
98 
floatingpoint(float a,float b)99 void floatingpoint(float a, float b) {
100   double d = (double)(a * b); // Currently we don't warn for this.
101 }
102