This is the mail archive of the gcc-help@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

RE: False positive warning: missing return statement and switch statements


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


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]