blob: 9e7cd8134d4620a3178b3a3ba732abf4126aa047 [file] [log] [blame]
Gabor Horvath5273f612016-04-06 12:04:51 +00001// RUN: %check_clang_tidy %s misc-misplaced-widening-cast %t -- -config="{CheckOptions: [{key: misc-misplaced-widening-cast.CheckImplicitCasts, value: 1}]}" --
2
3void func(long arg) {}
Daniel Marjamakiad329372016-02-09 14:08:49 +00004
5void assign(int a, int b) {
6 long l;
7
8 l = a * b;
Daniel Marjamakiad329372016-02-09 14:08:49 +00009 // CHECK-MESSAGES: :[[@LINE-1]]:7: warning: either cast from 'int' to 'long' is ineffective, or there is loss of precision before the conversion [misc-misplaced-widening-cast]
Gabor Horvath5273f612016-04-06 12:04:51 +000010 l = (long)(a * b);
11 // CHECK-MESSAGES: :[[@LINE-1]]:7: warning: either cast from 'int' to 'long'
Daniel Marjamakiad329372016-02-09 14:08:49 +000012 l = (long)a * b;
13
Gabor Horvath5273f612016-04-06 12:04:51 +000014 l = a << 8;
15 // CHECK-MESSAGES: :[[@LINE-1]]:7: warning: either cast from 'int' to 'long'
Daniel Marjamakiad329372016-02-09 14:08:49 +000016 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);
Gabor Horvath5273f612016-04-06 12:04:51 +000021 // CHECK-MESSAGES: :[[@LINE-1]]:7: warning: either cast from 'int' to 'long'
22}
23
24void 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;
Daniel Marjamakiad329372016-02-09 14:08:49 +000037}
38
39void init(unsigned int n) {
Gabor Horvath5273f612016-04-06 12:04:51 +000040 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
47void 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);
Daniel Marjamakiad329372016-02-09 14:08:49 +000053}
54
55long ret(int a) {
Gabor Horvath5273f612016-04-06 12:04:51 +000056 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 }
Daniel Marjamakiad329372016-02-09 14:08:49 +000065}
66
67void 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.
Gabor Horvath5273f612016-04-06 12:04:51 +000074 l = (long)((i % 5) + 1);
Daniel Marjamakiad329372016-02-09 14:08:49 +000075 // The result is a 16 bit value, there is no truncation in the implicit cast.
Gabor Horvath5273f612016-04-06 12:04:51 +000076 l = (long)(((*p) << 8) + *(p + 1));
Daniel Marjamakiad329372016-02-09 14:08:49 +000077}
78
79template <class T> struct DontWarn2 {
80 void assign(T a, T b) {
81 long l;
82 l = (long)(a * b);
83 }
84};
85DontWarn2<int> DW2;
86
87// Cast is not suspicious when casting macro.
88#define A (X<<2)
89long 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)
95long macro2(int x, int y) {
96 return B(x,y);
97}
98
99void floatingpoint(float a, float b) {
100 double d = (double)(a * b); // Currently we don't warn for this.
101}