Icon View Thread

The following is the text of the current message along with any replies.
Messages 1 to 2 of 2 total
Thread Compiler warning, why?
Fri, Aug 21 2015 5:01 AMPermanent Link

Michael Dreher

I have this enumaration type

type
 TTriState = (
   TS_FALSE,
   TS_TRUE,
   TS_UNDEFINED
 );       

and a function using a parameter of this type:

function TriStateToErrColor(ts : TTriState) : TColor;
begin
 case ts of
   TS_FALSE     : Result := ERROR_COLOR_FALSE;
   TS_TRUE      : Result := ERROR_COLOR_TRUE;
   TS_UNDEFINED : Result := ERROR_COLOR_UNDEFINED;
 end;
end;   // <- this is line (71,1)   

Although there is not code path in the function where the return value will be undefined, the compiler
is warning "[Warning] GlobalFunctoins.wbs (71,1): The result may not have been assigned a value".

To get rid of the warning, I'll be forced to code a dummy statement like

 case ts of
   TS_FALSE     : Result := ERROR_COLOR_FALSE;
   TS_TRUE      : Result := ERROR_COLOR_TRUE;
   TS_UNDEFINED : Result := ERROR_COLOR_UNDEFINED;     
 else  Result := ERROR_COLOR_UNDEFINED; // <<<-----
 end;

that is never executed. I know the Delphi compilers do the same, they warn in this case.
It should be possible for the compiler to "find out", that all constant values of the enumeration type are processed
by the case statement.
I never realized the reason behind this, so I ask.

Michael Dreher
Fri, Aug 21 2015 12:47 PMPermanent Link

Tim Young [Elevate Software]

Elevate Software, Inc.

Avatar

Email timyoung@elevatesoft.com

Michael,

<< It should be possible for the compiler to "find out", that all constant values of the enumeration type are processed by the case statement.  I never realized the reason behind this, so I ask. >>

I can't really speak for Delphi. But, with EWB, it's not that simple because the case comparison value may be a runtime cast from an integer, so it may not actually be within the range of enumeration members.  I suppose if I added runtime range-checking on the integer casts, it would be possible, but I'm really loathe to add runtime code that isn't a one-to-one to the original source code.

Tim Young
Elevate Software
www.elevatesoft.com
Image