In a quite large code base, I found the following construct (rewritten snippet) in a cpp file
int main()
{
bool b;
//... some code ...;
b = "False"
}
This is completely legal code, but clearly not intentional. It is easily fixed, but it has been present since 2014 without anyone noticing, so it is obviously not easy to spot.
Is it possible to make gcc warn about this?
To suppress this warning use the unused attribute (see Variable Attributes). This warning is also enabled by -Wunused , which is enabled by -Wall . Warn whenever a static function is declared but not defined or a non-inline static function is unused.
By default, Fuchsia C/C++ code compiles with the flag -Wconversion , which prohibits implicit type conversions that may alter a value.
gcc -Wall enables all compiler's warning messages. This option should always be used, in order to generate better code.
Compiler warnings are messages produced by a compiler regarding program code fragments to be considered by the developer, as they may contain errors. Unlike compilation errors, warnings don't interrupt the compilation process.
Neither g++ 7 nor clang++ 5 warn with -Wall -Wextra -Wpedantic
.
Clang has a warning called -Wstring-conversion
that will catch the mistake, but gcc doesn't have it. I briefly looked through the gcc warning documentation page and didn't find anything that would catch the mistake.
live wandbox example
I've filed a report for a feature suggestion on the gcc bug tracker: #80151.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With