[fpc-devel] [Patch/RFC] Warnings for (in/over)complete case statements
Michael Van Canneyt
michael at freepascal.org
Wed Jan 2 11:19:30 CET 2019
On Tue, 1 Jan 2019, Martok wrote:
> Hi all,
> The attached patch adds two messages inspired by C compiler's -Wswitch-enum and
> -Wcovered-switch-default. Building on the recent label count refactoring, this
> was fairly straightforward.
> - If a case statement on an ordinal does not contain labels for all values of
> the ordinal, and no else statement is given, raise a new warning (W6059). This
> is actually defined as an error in ISO7185 and a dynamic-violation in IEC10206.
> - If a case statement has labels for the entire range of the type, and an (now
> never used) else statement is present, a warning "Unreachable code" is raised.
> Both cases are clearly something where the compiler and programmer don't agree
> on something, hence drawing attention to these situations.
> The checks are enabled only for enumerated types and small (1-byte) integers. In
> C, they are only for enumerated types, I added small types because they are
> often used as tag types, where this check is extra useful.
> Now, the RFC part. I have a few open questions...
> * does it make sense to do that to all integral types? In ISO mode, it's
> formally required for all case statements. I left it out for now as sometimes
> case..of is used as a shorthand for multiple in..[range], there would be more
> detections that look like false positives.
It does not make sense. Consider the following:
TMyClass = class
function GetString(AIndex: Integer): string;
Property MyString : String Index 1 Read GetString;
function TMyClass.GetString(AIndex: Integer): string;
case AIndex of
1 : Result:=GenerateSomestringvalue;
I don't think there should be errors or warnings.
More information about the fpc-devel