| 1 |
// RUN: %check_clang_tidy %s bugprone-misplaced-widening-cast %t -- -config="{CheckOptions: {bugprone-misplaced-widening-cast.CheckImplicitCasts: false}}" -- |
| 2 |
|
| 3 |
void func(long arg) {} |
| 4 |
|
| 5 |
void assign(int a, int b) { |
| 6 |
long l; |
| 7 |
|
| 8 |
l = a * b; |
| 9 |
l = (long)(a * b); |
| 10 |
// 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] |
| 11 |
l = (long)a * b; |
| 12 |
|
| 13 |
l = a << 8; |
| 14 |
l = (long)(a << 8); |
| 15 |
// CHECK-MESSAGES: :[[@LINE-1]]:7: warning: either cast from 'int' to 'long' |
| 16 |
l = (long)b << 8; |
| 17 |
|
| 18 |
l = static_cast<long>(a * b); |
| 19 |
// CHECK-MESSAGES: :[[@LINE-1]]:7: warning: either cast from 'int' to 'long' |
| 20 |
} |
| 21 |
|
| 22 |
void compare(int a, int b, long c) { |
| 23 |
bool l; |
| 24 |
|
| 25 |
l = a * b == c; |
| 26 |
l = c == a * b; |
| 27 |
l = (long)(a * b) == c; |
| 28 |
// CHECK-MESSAGES: :[[@LINE-1]]:7: warning: either cast from 'int' to 'long' |
| 29 |
l = c == (long)(a * b); |
| 30 |
// CHECK-MESSAGES: :[[@LINE-1]]:12: warning: either cast from 'int' to 'long' |
| 31 |
l = (long)a * b == c; |
| 32 |
l = c == (long)a * b; |
| 33 |
} |
| 34 |
|
| 35 |
void init(unsigned int n) { |
| 36 |
long l1 = n << 8; |
| 37 |
long l2 = (long)(n << 8); |
| 38 |
// CHECK-MESSAGES: :[[@LINE-1]]:13: warning: either cast from 'unsigned int' to 'long' |
| 39 |
long l3 = (long)n << 8; |
| 40 |
} |
| 41 |
|
| 42 |
void call(unsigned int n) { |
| 43 |
func(n << 8); |
| 44 |
func((long)(n << 8)); |
| 45 |
// CHECK-MESSAGES: :[[@LINE-1]]:8: warning: either cast from 'unsigned int' to 'long' |
| 46 |
func((long)n << 8); |
| 47 |
} |
| 48 |
|
| 49 |
long ret(int a) { |
| 50 |
if (a < 0) { |
| 51 |
return a * 1000; |
| 52 |
} else if (a > 0) { |
| 53 |
return (long)(a * 1000); |
| 54 |
// CHECK-MESSAGES: :[[@LINE-1]]:12: warning: either cast from 'int' to 'long' |
| 55 |
} else { |
| 56 |
return (long)a * 1000; |
| 57 |
} |
| 58 |
} |
| 59 |
|
| 60 |
// Shall not generate an assert. https://bugs.llvm.org/show_bug.cgi?id=33660 |
| 61 |
template <class> class A { |
| 62 |
enum Type {}; |
| 63 |
static char *m_fn1() { char p = (Type)(&p - m_fn1()); } |
| 64 |
}; |
| 65 |
|
| 66 |
enum DaysEnum { |
| 67 |
MON, |
| 68 |
TUE, |
| 69 |
WED, |
| 70 |
THR, |
| 71 |
FRI, |
| 72 |
SAT, |
| 73 |
SUN |
| 74 |
}; |
| 75 |
|
| 76 |
// Do not warn for int to enum casts. |
| 77 |
void nextDay(DaysEnum day) { |
| 78 |
if (day < SUN) |
| 79 |
day = (DaysEnum)(day + 1); |
| 80 |
if (day < SUN) |
| 81 |
day = static_cast<DaysEnum>(day + 1); |
| 82 |
} |