Bug 87406 - Implement -Wconstant-conversion and -Wbitfield-constant-conversion
Summary: Implement -Wconstant-conversion and -Wbitfield-constant-conversion
Status: NEW
Alias: None
Product: gcc
Classification: Unclassified
Component: c++ (show other bugs)
Version: 9.0
: P3 enhancement
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords: diagnostic
Depends on:
Blocks: new-warning, new_warning
  Show dependency treegraph
 
Reported: 2018-09-24 10:39 UTC by Martin Liška
Modified: 2021-03-19 22:04 UTC (History)
3 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2018-09-24 00:00:00


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Martin Liška 2018-09-24 10:39:36 UTC
It's also from LLVM, one bug that was seen by that:
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=87396

Example of error message:
implicit conversion from 'int' to 'char' changes value from 132 to -124
Comment 1 Jonathan Wakely 2018-09-24 11:51:49 UTC
We do warn about this, but only with -Wconversion which is not in -Wall or -Wextra

c.cc:1:24: warning: conversion to ‘char’ from ‘int’ may alter its value [-Wconversion]
 char f(int i) { return i; }
                        ^
Comment 2 Manuel López-Ibáñez 2018-09-24 19:43:21 UTC
(In reply to Jonathan Wakely from comment #1)
> We do warn about this, but only with -Wconversion which is not in -Wall or
> -Wextra
> 
> c.cc:1:24: warning: conversion to ‘char’ from ‘int’ may alter its value
> [-Wconversion]
>  char f(int i) { return i; }


This is not the "constant" case. A testcase would be:

char foo(void) {
    return 132;
} 

<source>: In function 'char square()':
<source>:3:12: warning: conversion from 'int' to 'char' changes value from '132' to ''\37777777604'' [-Wconversion]
     return 132;
            ^~~

I think the constant case should be safe for -Wall. It is a just a matter of adding more specific flags.
Comment 3 Eric Gallager 2018-10-03 02:29:53 UTC
Is clang's -Wbitfield-constant-conversion anything like the -Wbitfield-conversion flag requested in bug 39170?
Comment 4 Martin Liška 2018-10-03 09:11:57 UTC
(In reply to Eric Gallager from comment #3)
> Is clang's -Wbitfield-constant-conversion anything like the
> -Wbitfield-conversion flag requested in bug 39170?

It's similar, but if I see correctly the LLVM warning is only about constants:
https://godbolt.org/z/srLT8m