From clang: test.cc:4:23: warning: implicit conversion from 'double' to 'int' changes value from 3.1415926 to 3 [-Wliteral-conversion] static const int c = 3.1415926; ~ ^~~~~~~~~ This should be fairly easy, since we warn already with -Wconversion: 1) Change the warning flag. 2) Show the values before/after.
Problem: We do not want to warn for expressions. From the clang testcases: // Expressions, such as those that indicate rounding-down, should NOT produce warnings. int x = 24 * 0.5; int y = (24*60*60) * 0.25; int pennies = 123.45 * 100; But gcc folds those before conversion_warning, so we cannot know they are expressions... :-(
On Sat, 27 Oct 2012, manu at gcc dot gnu.org wrote: > // Expressions, such as those that indicate rounding-down, should NOT produce > warnings. > int x = 24 * 0.5; > int y = (24*60*60) * 0.25; > int pennies = 123.45 * 100; The last of those seems pretty suspicious (123.45 isn't an exact floating-point value, but the user probably wants 12345 independent of whether the floating-point value is above or below the exact decimal value). Are you *sure* you don't want a warning in such a case?
Confirmed.
*** Bug 63431 has been marked as a duplicate of this bug. ***
Created attachment 33637 [details] untested patch Untested patch. Bonus points if we show the value before and after conversion like clang does.
(In reply to Manuel López-Ibáñez from comment #5) > Created attachment 33637 [details] > untested patch > > Untested patch. Bonus points if we show the value before and after > conversion like clang does. I tried out the patch by building a test compiler and then building the current Linux kernel. Trivial problem with doubled tokens (&& &&), but the main problem is that the patch doesn't fix the problem I described of double and float literals into integral types. Also, plenty of false positives for integer into smaller integral types, for example integer into short or integer into char. For example, the patch warns for this reasonable code: charVariable = 0x80; As is, I would not use the patch, but maybe with some further work, it might be suitable for future use.
Combining the 2 testcases from this bug, I get the following output now: $ $ cat 55077.c && /usr/local/bin/gcc -c -Wall -Wextra -pedantic -Wconversion -Wdouble-promotion 55077.c static const int c = 3.1415926; int x = 24 * 0.5; int y = (24*60*60) * 0.25; int pennies = 123.45 * 100; 55077.c:1:22: warning: conversion from 'double' to 'int' changes value from '3.1415926000000001e+0' to '3' [-Wfloat-conversion] static const int c = 3.1415926; ^~~~~~~~~ 55077.c:1:18: warning: 'c' defined but not used [-Wunused-const-variable=] static const int c = 3.1415926; ^ $ So it shows the values before/after now; still might want to split off a separate -Wliteral-conversion flag though.
*** Bug 87405 has been marked as a duplicate of this bug. ***
-Wfloat-conversion does the deed: any chance of getting it someplace useful like -Wall or -Wextra anytime soon ? I will put it into my local compiler.
(In reply to David Binderman from comment #9) > -Wfloat-conversion does the deed: any chance of getting it someplace useful > like -Wall or -Wextra anytime soon ? > > I will put it into my local compiler. I think the point here is that the proposed -Wliteral-conversion warns for a smaller number of cases than -Wfloat-conversion does, and thus would be safer to enable more widely than -Wfloat-conversion is.