Bug 55077 - implement and enable by default -Wliteral-conversion
Summary: implement and enable by default -Wliteral-conversion
Status: NEW
Alias: None
Product: gcc
Classification: Unclassified
Component: c++ (show other bugs)
Version: unknown
: P3 enhancement
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords: diagnostic
: 63431 87405 (view as bug list)
Depends on:
Blocks: new-warning, new_warning
  Show dependency treegraph
 
Reported: 2012-10-25 23:51 UTC by Manuel López-Ibáñez
Modified: 2023-05-31 06:02 UTC (History)
6 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2013-11-10 00:00:00


Attachments
untested patch (1.03 KB, patch)
2014-10-01 15:11 UTC, Manuel López-Ibáñez
Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Manuel López-Ibáñez 2012-10-25 23:51:44 UTC
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.
Comment 1 Manuel López-Ibáñez 2012-10-27 19:48:25 UTC
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... :-(
Comment 2 jsm-csl@polyomino.org.uk 2012-10-28 18:02:07 UTC
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?
Comment 3 Andrew Pinski 2013-11-10 06:37:49 UTC
Confirmed.
Comment 4 Manuel López-Ibáñez 2014-10-01 15:09:15 UTC
*** Bug 63431 has been marked as a duplicate of this bug. ***
Comment 5 Manuel López-Ibáñez 2014-10-01 15:11:34 UTC
Created attachment 33637 [details]
untested patch

Untested patch. Bonus points if we show the value before and after conversion like clang does.
Comment 6 David Binderman 2015-11-23 18:07:32 UTC
(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.
Comment 7 Eric Gallager 2018-02-20 10:29:17 UTC
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.
Comment 8 Eric Gallager 2018-09-30 00:53:41 UTC
*** Bug 87405 has been marked as a duplicate of this bug. ***
Comment 9 David Binderman 2022-04-14 15:55:47 UTC
-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.
Comment 10 Eric Gallager 2023-05-31 02:16:32 UTC
(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.