xref: /minix3/external/bsd/llvm/dist/clang/test/SemaCXX/warn-literal-conversion.cpp (revision f4a2713ac843a11c696ec80c0a5e3e5d80b4d338)
1*f4a2713aSLionel Sambuc // RUN: %clang_cc1 -fsyntax-only -Wliteral-conversion -verify %s
2*f4a2713aSLionel Sambuc 
3*f4a2713aSLionel Sambuc void foo(int y);
4*f4a2713aSLionel Sambuc 
5*f4a2713aSLionel Sambuc // Warn when a literal float or double is assigned or bound to an integer.
test0()6*f4a2713aSLionel Sambuc void test0() {
7*f4a2713aSLionel Sambuc   // Float
8*f4a2713aSLionel Sambuc   int y0 = 1.2222F; // expected-warning {{implicit conversion from 'float' to 'int' changes value from 1.2222 to 1}}
9*f4a2713aSLionel Sambuc   int y1 = (1.2222F); // expected-warning {{implicit conversion from 'float' to 'int' changes value from 1.2222 to 1}}
10*f4a2713aSLionel Sambuc   int y2 = (((1.2222F))); // expected-warning {{implicit conversion from 'float' to 'int' changes value from 1.2222 to 1}}
11*f4a2713aSLionel Sambuc   int y3 = 12E-1F; // expected-warning {{implicit conversion from 'float' to 'int' changes value from 1.2 to 1}}
12*f4a2713aSLionel Sambuc   int y4 = 1.23E1F; // expected-warning {{implicit conversion from 'float' to 'int' changes value from 12.3 to 12}}
13*f4a2713aSLionel Sambuc   // Double
14*f4a2713aSLionel Sambuc   int y5 = 1.2222; // expected-warning {{implicit conversion from 'double' to 'int' changes value from 1.2222 to 1}}
15*f4a2713aSLionel Sambuc   int y6 = 12E-1; // expected-warning {{implicit conversion from 'double' to 'int' changes value from 1.2 to 1}}
16*f4a2713aSLionel Sambuc   int y7 = 1.23E1; // expected-warning {{implicit conversion from 'double' to 'int' changes value from 12.3 to 12}}
17*f4a2713aSLionel Sambuc   int y8 = (1.23E1); // expected-warning {{implicit conversion from 'double' to 'int' changes value from 12.3 to 12}}
18*f4a2713aSLionel Sambuc 
19*f4a2713aSLionel Sambuc   // Test assignment to an existing variable.
20*f4a2713aSLionel Sambuc   y8 = 2.22F; // expected-warning {{implicit conversion from 'float' to 'int' changes value from 2.22 to 2}}
21*f4a2713aSLionel Sambuc 
22*f4a2713aSLionel Sambuc   // Test direct initialization.
23*f4a2713aSLionel Sambuc   int y9(1.23F); // expected-warning {{implicit conversion from 'float' to 'int' changes value from 1.23 to 1}}
24*f4a2713aSLionel Sambuc 
25*f4a2713aSLionel Sambuc   // Test passing a literal floating-point value to a function that takes an integer.
26*f4a2713aSLionel Sambuc   foo(1.2F); // expected-warning {{implicit conversion from 'float' to 'int' changes value from 1.2 to 1}}
27*f4a2713aSLionel Sambuc 
28*f4a2713aSLionel Sambuc   int y10 = -1.2F;  // expected-warning {{implicit conversion from 'float' to 'int' changes value from 1.2 to 1}}
29*f4a2713aSLionel Sambuc 
30*f4a2713aSLionel Sambuc   // -Wliteral-conversion does NOT catch const values.
31*f4a2713aSLionel Sambuc   // (-Wconversion DOES catch them.)
32*f4a2713aSLionel Sambuc   static const float sales_tax_rate = .095F;
33*f4a2713aSLionel Sambuc   int z = sales_tax_rate;
34*f4a2713aSLionel Sambuc   foo(sales_tax_rate);
35*f4a2713aSLionel Sambuc 
36*f4a2713aSLionel Sambuc   // Expressions, such as those that indicate rounding-down, should NOT produce warnings.
37*f4a2713aSLionel Sambuc   int x = 24 * 0.5;
38*f4a2713aSLionel Sambuc   int y = (24*60*60) * 0.25;
39*f4a2713aSLionel Sambuc   int pennies = 123.45 * 100;
40*f4a2713aSLionel Sambuc }
41