0

I don't understand this warning for my little function:

int Fct_name (int nb1, int * nb2) 
{
    switch (Dest)
    { 
    Case 1 : 
        return Fct_1(nb1,nb2);
    Case 2 :
        return -1;
    }
}

If someone can help me?

Jonas
  • 6,915
  • 8
  • 35
  • 53
Guigui
  • 39
  • 1
  • 3
  • 7
    What happens if `dest` is not 1 or 2? – NathanOliver Jun 15 '17 at 12:43
  • 3
    You don't have a `default` case – Cory Kramer Jun 15 '17 at 12:43
  • I tried to put a default case but the warning is still there. Dest can only be 1 or 2 (it's an enum) – Guigui Jun 15 '17 at 12:45
  • The compiler is not smart enough to know that. You have to add a return; statement to every path yourself. – user1725145 Jun 15 '17 at 12:48
  • `return Dest == 1 ? Fct_1(nb1, nb2) : -1;` would be a lot simpler and wouldnt trigger this warning. Also if its an enum why are you using the numerical values for each case instead of the enum itself (thereby defeating the point of an enum...) ? – Borgleader Jun 15 '17 at 12:48
  • @Guigui No problem with this: http://coliru.stacked-crooked.com/a/68ba6bd24b08cfbe – NathanOliver Jun 15 '17 at 12:50
  • 2
    Take a look at [What happens if you static_cast invalid value to enum class?](https://stackoverflow.com/questions/18195312/what-happens-if-you-static-cast-invalid-value-to-enum-class) and [What happens if you static_cast invalid value to enum class?](https://stackoverflow.com/questions/33812998/is-it-allowed-for-an-enum-to-have-an-unlisted-value). Depending on the compiler you could write `DEST Dest = (DEST)3;` and it would still compile, and `Dest` could hold `3`. – t.niese Jun 15 '17 at 12:52
  • 4
    Since `Dest` is an enumerated type, the `case` statements should use the enumerators and not their values. – Pete Becker Jun 15 '17 at 12:56
  • Please edit your question to contain [mcve]. `Case` is misspeled and `Dest` is not defined anywhere. – Slava Jun 15 '17 at 13:04

6 Answers6

2

It's because, as the warning says, not all paths of your code return a value while the function has a distinct return type which tells the compiler "hey, I'm going to return something." but you don't actually do that if Dest is anything other than 1 or 2.


You commented:

Dest can only be 1 or 2 (it's an enum)

Yes okay but only you know that, your compiler doesn't, and it won't take your word for it. It can only see the static properties of your code, it can't predict how the runtime will go and thus it won't accept your code. For all it knows Dest can be changed by an external piece of code etc etc.


You should add some sort of default value:

int Fct_name (int nb1, int * nb2) 
{
   switch (Dest)
   { 
    case 1 : 
         return Fct_1(nb1,nb2);
    case 2 :
         return -1;
    }
    return 0;
}
Hatted Rooster
  • 35,759
  • 6
  • 62
  • 122
0

As said by @NathanOliver, the warning means that there might be a possible case where your function might not return any value. To be more precise if a user inputs a value which is not 1 or 2, then your function does not return any value.

You might be thinking that you are only going to input 1 or 2. But the compiler doesn't know this.

What you can do is -

  1. Ignore the warning - You can ignore the warning and go right ahead. But be careful that you only put 1 o 2 as the parameter in all cases.

Though I would not recommend ignoring the warning. In general, it is better to pay heed to the warnings. In the long run, it saves you from many bugs in case you are working on big projects.

  1. Add a default - This condition will not actually execute ever, but the compiler will now stop giving the warnings.

Here's the corrected code -

int Fct_name (int nb1, int * nb2) 
    {
        switch (Dest)
        { 
           case 1 : 
              return Fct_1(nb1,nb2);
           case 2 :
              return -1;
           default:
             return 0;
       }
   }

Or you could do this -

int Fct_name (int nb1, int * nb2) 
{
    switch (Dest)
    { 
       case 1 : 
          return Fct_1(nb1,nb2);
       case 2 :
          return -1;
    }
    return 0;
}
97amarnathk
  • 957
  • 2
  • 11
  • 30
0

If dest is an enum with only two values, it is unnecessary complicated - return Dest == 1 ? Fct_1(nb1,nb2) : -1; is enough. Otherwise, replace case 2: with default:.

Aganju
  • 6,295
  • 1
  • 12
  • 23
0

If Dest is really an enum as you said, the compiler should not issue a warning in your case. At least my compiler does not. So the following code compiles without warning/error:

enum {
  x = 1,
    y = 2
} Dest;

int Fct_name (int nb1, int * nb2)
{
    switch (Dest)
    {
            case 1 :
            return -5; //Fct_1(nb1,nb2);
            case 2 :
            return -1;
    }
}

int main() {

}

However, if Dest is an integer or if the enum defines other values than 1 and 2, then I get an error as well. This can then be fixed with a default-path:

enum {
  x = 1,
    y = 2,
    z = 3
} Dest;

int Fct_name (int nb1, int * nb2)
{
    switch (Dest)
    {
            case 1 :
            return -5; //Fct_1(nb1,nb2);
            case 2 :
            return -1;
        default:
            return 0;
    }
}
Stephan Lechner
  • 34,891
  • 4
  • 35
  • 58
0

If Dest can only be 1 or 2, you could rewrite like this

int Fct_name (int nb1, int * nb2) 
{
    if(Dest == 1) return Fct_1(nb1,nb2);
    return -1;
}
raullalves
  • 836
  • 8
  • 20
-1

Add a return XXX at the end of the function which will then ensure that the compiler will not have anyway of getting to the end of the function without a return of value occurring.

lostbard
  • 5,065
  • 1
  • 15
  • 17