1 // RUN: %check_clang_tidy -check-suffixes=ALL,C -std=c99 %s bugprone-implicit-widening-of-multiplication-result %t -- -- -target x86_64-unknown-unknown -x c
2 // RUN: %check_clang_tidy -check-suffixes=ALL,CXX %s bugprone-implicit-widening-of-multiplication-result %t -- -- -target x86_64-unknown-unknown -x c++
3 
4 // RUN: %check_clang_tidy -check-suffixes=ALL,C -std=c99 %s bugprone-implicit-widening-of-multiplication-result %t -- \
5 // RUN:     -config='{CheckOptions: [ \
6 // RUN:         {key: bugprone-implicit-widening-of-multiplication-result.UseCXXStaticCastsInCppSources, value: false} \
7 // RUN:     ]}' -- -target x86_64-unknown-unknown -x c
8 // RUN: %check_clang_tidy -check-suffixes=ALL,C %s bugprone-implicit-widening-of-multiplication-result %t -- \
9 // RUN:     -config='{CheckOptions: [ \
10 // RUN:         {key: bugprone-implicit-widening-of-multiplication-result.UseCXXStaticCastsInCppSources, value: false} \
11 // RUN:     ]}' -- -target x86_64-unknown-unknown -x c++
12 
t0(int a,int b)13 long t0(int a, int b) {
14   return a * b;
15   // CHECK-NOTES-ALL: :[[@LINE-1]]:10: warning: performing an implicit widening conversion to type 'long' of a multiplication performed in type 'int'
16   // CHECK-NOTES-ALL: :[[@LINE-2]]:10: note: make conversion explicit to silence this warning
17   // CHECK-NOTES-C:                    (long)( )
18   // CHECK-NOTES-CXX:                  static_cast<long>( )
19   // CHECK-NOTES-ALL: :[[@LINE-5]]:10: note: perform multiplication in a wider type
20   // CHECK-NOTES-C:                    (long)
21   // CHECK-NOTES-CXX:                  static_cast<long>()
22 }
t1(int a,int b)23 unsigned long t1(int a, int b) {
24   return a * b;
25   // CHECK-NOTES-ALL: :[[@LINE-1]]:10: warning: performing an implicit widening conversion to type 'unsigned long' of a multiplication performed in type 'int'
26   // CHECK-NOTES-ALL: :[[@LINE-2]]:10: note: make conversion explicit to silence this warning
27   // CHECK-NOTES-C:                    (unsigned long)( )
28   // CHECK-NOTES-CXX:                  static_cast<unsigned long>( )
29   // CHECK-NOTES-ALL: :[[@LINE-5]]:10: note: perform multiplication in a wider type
30   // CHECK-NOTES-C:                    (long)
31   // CHECK-NOTES-CXX:                  static_cast<long>()
32 }
33 
t2(unsigned int a,int b)34 long t2(unsigned int a, int b) {
35   return a * b;
36   // CHECK-NOTES-ALL: :[[@LINE-1]]:10: warning: performing an implicit widening conversion to type 'long' of a multiplication performed in type 'unsigned int'
37   // CHECK-NOTES-ALL: :[[@LINE-2]]:10: note: make conversion explicit to silence this warning
38   // CHECK-NOTES-C:                    (long)( )
39   // CHECK-NOTES-CXX:                  static_cast<long>( )
40   // CHECK-NOTES-ALL: :[[@LINE-5]]:10: note: perform multiplication in a wider type
41   // CHECK-NOTES-C:                    (unsigned long)
42   // CHECK-NOTES-CXX:                  static_cast<unsigned long>()
43 }
t3(unsigned int a,int b)44 unsigned long t3(unsigned int a, int b) {
45   return a * b;
46   // CHECK-NOTES-ALL: :[[@LINE-1]]:10: warning: performing an implicit widening conversion to type 'unsigned long' of a multiplication performed in type 'unsigned int'
47   // CHECK-NOTES-ALL: :[[@LINE-2]]:10: note: make conversion explicit to silence this warning
48   // CHECK-NOTES-C:                    (unsigned long)( )
49   // CHECK-NOTES-CXX:                  static_cast<unsigned long>( )
50   // CHECK-NOTES-ALL: :[[@LINE-5]]:10: note: perform multiplication in a wider type
51   // CHECK-NOTES-C:                    (unsigned long)
52   // CHECK-NOTES-CXX:                  static_cast<unsigned long>()
53 }
54 
t4(int a,unsigned int b)55 long t4(int a, unsigned int b) {
56   return a * b;
57   // CHECK-NOTES-ALL: :[[@LINE-1]]:10: warning: performing an implicit widening conversion to type 'long' of a multiplication performed in type 'unsigned int'
58   // CHECK-NOTES-ALL: :[[@LINE-2]]:10: note: make conversion explicit to silence this warning
59   // CHECK-NOTES-ALL: :[[@LINE-3]]:10: note: perform multiplication in a wider type
60 }
t5(int a,unsigned int b)61 unsigned long t5(int a, unsigned int b) {
62   return a * b;
63   // CHECK-NOTES-ALL: :[[@LINE-1]]:10: warning: performing an implicit widening conversion to type 'unsigned long' of a multiplication performed in type 'unsigned int'
64   // CHECK-NOTES-ALL: :[[@LINE-2]]:10: note: make conversion explicit to silence this warning
65   // CHECK-NOTES-ALL: :[[@LINE-3]]:10: note: perform multiplication in a wider type
66 }
67 
t6(unsigned int a,unsigned int b)68 long t6(unsigned int a, unsigned int b) {
69   return a * b;
70   // CHECK-NOTES-ALL: :[[@LINE-1]]:10: warning: performing an implicit widening conversion to type 'long' of a multiplication performed in type 'unsigned int'
71   // CHECK-NOTES-ALL: :[[@LINE-2]]:10: note: make conversion explicit to silence this warning
72   // CHECK-NOTES-ALL: :[[@LINE-3]]:10: note: perform multiplication in a wider type
73 }
t7(unsigned int a,unsigned int b)74 unsigned long t7(unsigned int a, unsigned int b) {
75   return a * b;
76   // CHECK-NOTES-ALL: :[[@LINE-1]]:10: warning: performing an implicit widening conversion to type 'unsigned long' of a multiplication performed in type 'unsigned int'
77   // CHECK-NOTES-ALL: :[[@LINE-2]]:10: note: make conversion explicit to silence this warning
78   // CHECK-NOTES-ALL: :[[@LINE-3]]:10: note: perform multiplication in a wider type
79 }
80 
t8(int a,int b)81 long t8(int a, int b) {
82   return (a * b);
83   // CHECK-NOTES-ALL: :[[@LINE-1]]:11: warning: performing an implicit widening conversion to type 'long' of a multiplication performed in type 'int'
84   // CHECK-NOTES-ALL: :[[@LINE-2]]:11: note: make conversion explicit to silence this warning
85   // CHECK-NOTES-ALL: :[[@LINE-3]]:11: note: perform multiplication in a wider type
86 }
t9(int a,int b)87 long t9(int a, int b) {
88   return (a)*b;
89   // CHECK-NOTES-ALL: :[[@LINE-1]]:10: warning: performing an implicit widening conversion to type 'long' of a multiplication performed in type 'int'
90   // CHECK-NOTES-ALL: :[[@LINE-2]]:10: note: make conversion explicit to silence this warning
91   // CHECK-NOTES-ALL: :[[@LINE-3]]:10: note: perform multiplication in a wider type
92 }
n10(int a,int b)93 long n10(int a, int b) {
94   return (long)(a * b);
95 }
n11(int a,int b)96 long n11(int a, int b) {
97   return (unsigned long)(a * b);
98 }
99 
n12(long a,int b)100 long n12(long a, int b) {
101   return a * b;
102 }
n13(int a,long b)103 long n13(int a, long b) {
104   return a * b;
105 }
106 
n14(int a,int b,int c)107 long n14(int a, int b, int c) {
108   return a + b * c;
109 }
n15(int a,int b,int c)110 long n15(int a, int b, int c) {
111   return a * b + c;
112 }
113 
114 #ifdef __cplusplus
115 template <typename T1, typename T2>
template_test(T1 a,T1 b)116 T2 template_test(T1 a, T1 b) {
117   return a * b;
118 }
template_test_instantiation(int a,int b)119 long template_test_instantiation(int a, int b) {
120   return template_test<int, long>(a, b);
121 }
122 #endif
123