Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a warning for string literal to bool conversion in gcc

Tags:

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?

like image 919
tov Avatar asked Mar 22 '17 13:03

tov


People also ask

How do I stop a GCC warning?

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.

Which flag would you pass to your C++ compiler so it warns you about implicit conversions?

By default, Fuchsia C/C++ code compiles with the flag -Wconversion , which prohibits implicit type conversions that may alter a value.

Which GCC flag is used to enable all compiler warnings?

gcc -Wall enables all compiler's warning messages. This option should always be used, in order to generate better code.

What are compiler warnings?

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.


1 Answers

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.

enter image description here

live wandbox example


I've filed a report for a feature suggestion on the gcc bug tracker: #80151.

like image 165
Vittorio Romeo Avatar answered Sep 23 '22 11:09

Vittorio Romeo