3

In my project I changed many of our function's return value from bool to enum value
The problem is in the integration. The compiler doesn't warn me about wrong use of the functions.

I'm using g++ (with c++14) and with "-Wall -Wextra -Werror" flags

Lets see an example which produces the warning:

typedef enum {
    VALUE_1,
    VALUE_2,
    VALUE_3
} MY_ENUM;

bool bar() {
    return VALUE_3;
}

So in this example we return VALUE_3 and get error correctly: error: enum constant in boolean context [-Werror=int-in-bool-context]

But, if we change this example a bit, the compiler will not produce any errors:

typedef enum {
    VALUE_1,
    VALUE_2,
    VALUE_3
} MY_ENUM;

MY_ENUM foo() {
    return VALUE_3;
}

bool bar() {
    return foo();
}

Of course I understand that enum is type of int, and bool is a type of int so the compiler can process it. But at least I expect for some kind of warning?

Tried with clang too, same results with both examples.
The problem in my project is that I changed about ~150 function's return values, and there are about ~300 calling. So without any warnings I easily miss some...

Can I get it somehow to yell at me?

UdiM
  • 480
  • 3
  • 19
  • Why not use `enum class` ? – Sean Jan 11 '22 at 16:55
  • @Sean client constraints, header must be C. So the enum must be C style – UdiM Jan 11 '22 at 16:56
  • 1
    The client needs C-style header, but do you? You can use an `enum class`, clean up all of the compiler errors and then go back to the plain old `enum` after the compiler's found all of the usages for you. – user4581301 Jan 11 '22 at 17:29
  • 1
    Enumerators are of type `int` only in C. Separately, the warning explicitly mentions “constant”: it’s about uselessly using an enumerator (whose value is neither 0 nor 1) where `false` or `true` would work, not about the general boolean conversion (which is widely considered idiomatic). – Davis Herring Jan 11 '22 at 20:45

1 Answers1

1

Couple Ideas:

  1. Have a look here at some different flags:

https://gcc.gnu.org/onlinedocs/gcc/Warning-Options.html

Add in -Wextra. The site says not all warnings are explicitly added by -Wall and may need explicitly added. Note implicit conversion for enum is c only. Looks like you tagged c++.

  1. Also see

Warnings or errors for C++ implicit conversion of primitives

about primitive conversions.

  1. Enum class will not convert to int if you want to be really specific about using the enum value and therefore will give an error in bool context.

Hope this gets you somewhere.

notARobot
  • 61
  • 7
  • Tried `-Wextra`, didn't work. Didn't see any relevant answers in the second link – UdiM Jan 11 '22 at 17:15
  • 1
    -Wall should turn this on, but explicitly try "-Wint-in-bool-context" instead of -Wall. It is exactly what your title seems to describe. Otherwise try strongly typing your enums. Should not be able to assign a strongly typed enum to a bool or use it in any bool context. All you need to do is change enum example {}; to enum class example {}; – notARobot Jan 11 '22 at 17:46