public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
* RE: False positive warning: missing return statement and switch statements
@ 2005-11-15 22:27 Steven L. Zook
  2005-11-15 22:40 ` Frans Englich
  0 siblings, 1 reply; 7+ messages in thread
From: Steven L. Zook @ 2005-11-15 22:27 UTC (permalink / raw)
  To: Frans Englich, gcc-help

What happens when someone calls with:

toInt( (Number)3 );

C/C++ lets you cast any numeric to an enum.

-----Original Message-----
From: gcc-help-owner@gcc.gnu.org [mailto:gcc-help-owner@gcc.gnu.org] On
Behalf Of Frans Englich
Sent: Tuesday, November 15, 2005 2:21 PM
To: gcc-help@gcc.gnu.org
Subject: False positive warning: missing return statement and switch
statements


Hello,

For the code below, when I invoke "g++ -Wall file.cpp" with GCC version
3.3.4, I get:

file.cpp: In function `int toInt(Number)':
file.cpp:20: warning: control reaches end of non-void function

------------------------------------------------------------------------
-
enum Number
{
        Zero,
        One,
        Two
};

int toInt(const Number num)
{
        switch(num)
        {
                case Zero:
                        return 0;
                case One:
                        return 1;
                case Two:
                        return 2;
        }
}

int main()
{
        Number num = Zero;
        return toInt(num);
}
------------------------------------------------------------------------
-

I don't understand why the warning is issued. Isn't it fair to assume
that the argument(const Number num) is correct since it is strongly
typed, by being an enumerator?


Cheers,

		Frans

^ permalink raw reply	[flat|nested] 7+ messages in thread
* RE: False positive warning: missing return statement and switch statements
@ 2005-11-15 23:26 Steven L. Zook
  2005-11-15 23:31 ` Frans Englich
  0 siblings, 1 reply; 7+ messages in thread
From: Steven L. Zook @ 2005-11-15 23:26 UTC (permalink / raw)
  To: Frans Englich; +Cc: MSX to GCC

int toInt(const Number num)
{
        switch(num)
        {
                case Zero:
                        return 0;
                case One:
                        return 1;
                case Two:
                default:
                        return 2;
        }
} 

This approach at least returns something without causing much code
expansion. If you want to trust your code, this approach will silence
the warnings without costing much at run-time.

I guess that the compiler writers have a very difficult choice in these
kind of warnings between under and over warning. Their choice may not
work well for you. 

-----Original Message-----
From: Frans Englich [mailto:frans.englich@telia.com] 
Sent: Tuesday, November 15, 2005 3:01 PM
To: John Love-Jensen
Cc: Steven L. Zook; MSX to GCC
Subject: Re: False positive warning: missing return statement and switch
statements

On Tuesday 15 November 2005 22:47, John Love-Jensen wrote:
> Hi Frans,
>
> > Obviously, I am looking at this the wrong way -- please explain.
>
> Improve your code safety using C++ capabilities:
>
> int toInt(const Number num)
> {
>   switch(num)
>   {
>   case Zero:
>     return 0;
>   case One:
>     return 1;
>   case Two:
>     return 2;
>   default:
>     throw std::range_error("toInt(): enum Number out of range");
>   }
> }
>
> Then the warning goes away, and your code is better.

Yupp, that's what I currently do, in my case it unfortunately becomes a
significant amount of code for dummy tests(because I know I'm behaving).


Cheers,

		Frans

^ permalink raw reply	[flat|nested] 7+ messages in thread
* False positive warning: missing return statement and switch statements
@ 2005-11-15 22:10 Frans Englich
  0 siblings, 0 replies; 7+ messages in thread
From: Frans Englich @ 2005-11-15 22:10 UTC (permalink / raw)
  To: gcc-help


Hello,

For the code below, when I invoke "g++ -Wall file.cpp" with GCC version 3.3.4, 
I get:

file.cpp: In function `int toInt(Number)':
file.cpp:20: warning: control reaches end of non-void function

-------------------------------------------------------------------------
enum Number
{
        Zero,
        One,
        Two
};

int toInt(const Number num)
{
        switch(num)
        {
                case Zero:
                        return 0;
                case One:
                        return 1;
                case Two:
                        return 2;
        }
}

int main()
{
        Number num = Zero;
        return toInt(num);
}
-------------------------------------------------------------------------

I don't understand why the warning is issued. Isn't it fair to assume that the 
argument(const Number num) is correct since it is strongly typed, by being an 
enumerator?


Cheers,

		Frans

^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2005-11-15 23:31 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2005-11-15 22:27 False positive warning: missing return statement and switch statements Steven L. Zook
2005-11-15 22:40 ` Frans Englich
2005-11-15 22:47   ` John Love-Jensen
2005-11-15 22:49     ` Frans Englich
  -- strict thread matches above, loose matches on Subject: below --
2005-11-15 23:26 Steven L. Zook
2005-11-15 23:31 ` Frans Englich
2005-11-15 22:10 Frans Englich

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).