blob: b9c952873b9f7915ee89088f49685dd1a6c0d464 [file] [log] [blame]
Chandler Carrutha5b93322011-02-17 11:05:49 +00001// RUN: %clang_cc1 -fsyntax-only -Wliteral-conversion -verify %s
2
3void foo(int y);
4
5// Warn when a literal float or double is assigned or bound to an integer.
6void test0() {
7 // Float
8 int y0 = 1.2222F; // expected-warning {{implicit conversion turns literal floating-point number into integer}}
9 int y1 = (1.2222F); // expected-warning {{implicit conversion turns literal floating-point number into integer}}
10 int y2 = (((1.2222F))); // expected-warning {{implicit conversion turns literal floating-point number into integer}}
Chandler Carruthf65076e2011-04-10 08:36:24 +000011 int y3 = 12E1F; // expected-warning {{implicit conversion turns literal floating-point number into integer}} \
12 // expected-note {{this can be rewritten as an integer literal with the exact same value}}
13 int y4 = 1.2E1F; // expected-warning {{implicit conversion turns literal floating-point number into integer}} \
14 // expected-note {{this can be rewritten as an integer literal with the exact same value}}
Chandler Carrutha5b93322011-02-17 11:05:49 +000015 // Double
16 int y5 = 1.2222; // expected-warning {{implicit conversion turns literal floating-point number into integer}}
Chandler Carruthf65076e2011-04-10 08:36:24 +000017 int y6 = 12E1; // expected-warning {{implicit conversion turns literal floating-point number into integer}} \
18 // expected-note {{this can be rewritten as an integer literal with the exact same value}}
19 int y7 = 1.2E1; // expected-warning {{implicit conversion turns literal floating-point number into integer}} \
20 // expected-note {{this can be rewritten as an integer literal with the exact same value}}
21 int y8 = (1.2E1); // expected-warning {{implicit conversion turns literal floating-point number into integer}} \
22 // expected-note {{this can be rewritten as an integer literal with the exact same value}}
Chandler Carrutha5b93322011-02-17 11:05:49 +000023
24 // Test assignment to an existing variable.
25 y8 = 2.22F; // expected-warning {{implicit conversion turns literal floating-point number into integer}}
26
27 // Test direct initialization.
28 int y9(1.23F); // expected-warning {{implicit conversion turns literal floating-point number into integer}}
29
30 // Test passing a literal floating-point value to a function that takes an integer.
31 foo(1.2F); // expected-warning {{implicit conversion turns literal floating-point number into integer}}
32
33 // FIXME: -Wconversion-literal doesn't catch "-1.2F".
34 int y10 = -1.2F;
35
36 // -Wconversion-literal does NOT catch const values.
37 // (-Wconversion DOES catch them.)
38 static const float sales_tax_rate = .095F;
39 int z = sales_tax_rate;
40 foo(sales_tax_rate);
41
42 // Expressions, such as those that indicate rounding-down, should NOT produce warnings.
43 int x = 24 * 0.5;
44 int y = (24*60*60) * 0.25;
45 int pennies = 123.45 * 100;
46}