Forum > FPC development

Case statement does not handle all possible cases

(1/3) > >>

Zoran:
When compiling my programs with FPC from trunk, the compiler gives a lot of warnings:

--- Quote ---Warning: Case statement does not handle all possible cases
--- End quote ---

Isn't it quite normal programming practice, to do something in some cases, without covering all possible values from some enumeration?

Why should a programmer be warned to cover all possible cases?

Kays:

--- Quote from: Zoran on June 19, 2021, 06:54:22 pm ---Why should a programmer be warned to cover all possible cases?
--- End quote ---
It is an ISO requirement. ISO 7185, § 6.8.3.5 “Case-statements”
--- Quote ---One of the case-constants shall be equal to the value of the case-index upon entry to the case-statement; otherwise, it shall be an error
--- End quote ---
Now an “error” is a
--- Quote ---violation by a program of the requirements of the requirements to this International Standard that a processor is permitted to leave undetected.
--- End quote ---
In other words, an “error” may produce just a warning as the FPC [trunk version] does now. This becomes more apparent by the wording of ISO 10206 (“Extended Pascal”), § 6.9.3.5 “Case-statements”, which says “dynamic violation” (cf. § 3.1).


--- Quote from: Zoran on June 19, 2021, 06:54:22 pm ---Isn't it quite normal programming practice, to do something in some cases, without covering all possible values from some enumeration?
--- End quote ---
I think it was TP that introduced the else-clause and the possibility of a fall-through/bypassed case-statement. For Delphi-compatibility the FPC adopted this malpractice. Pascal is very well designed and there are reasons why all possible case-labels have to appear in case-statements.

winni:

--- Quote from: Kays on June 19, 2021, 07:08:37 pm ---I think it was TP that introduced the else-clause and the possibility of a fall-through/bypassed case-statement. For Delphi-compatibility the FPC adopted this malpractice. Pascal is very well designed and there are reasons why all possible case-labels have to appear in case-statements.

--- End quote ---

Hi!

TP did not invent the else-clause for the case statement. The misstreated it.

The original syntax for Pascal is:

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---Case i of1: .....;2: ....;otherwise ....:end;

fpc knows the original syntax.

Winni

Zoran:

Thank you, Kays.

However, for decades, FPC, Delphi and TP programmers have been very used to case statements without covering all possible values.

Perhaps the warning should be issued in iso mode only?

Bart:
If it bothers you filter it out using the appropriate -vmxxxx in your fpc.cfg?

E.g. I have this in my fpc.cfg

--- Code: ---#Don't show the "function X; marked as inline is not inlined" warning
-vm6058
--- End code ---
Because I don't bother.

Bart

Navigation

[0] Message Index

[#] Next page

Go to full version