Recent

Author Topic: missed warning  (Read 478 times)

Paolo

  • Hero Member
  • *****
  • Posts: 571
missed warning
« on: February 08, 2025, 10:33:57 am »
Hello

here a simple code to test

Code: Pascal  [Select][+][-]
  1. procedure TForm1.Button2Click(Sender: TObject);
  2. var
  3.   X : double;
  4.   i : integer;
  5.   Str1 : string;
  6. begin
  7.   i:=2;
  8.   Case i of
  9.     1:begin
  10.       //X:=10;                     //<--- enable/disable this line
  11.       Str1:=FloatToStr(X);
  12.     end;
  13.     2:begin
  14.       Str1:=FloatToStr(X);
  15.     end;
  16.   end;
  17. end;
  18.  

in the code above if row 10 is disabled a warning was emitted : "X not initialized", but if enabled no warning at all is emitted, even on row 14.

In general it seems to me that the warning should be raised for both the rows if X is never set, and selectively raised in case some case branch are setting X

or I am wrong ?

DragoRosso

  • Guest
Re: missed warning
« Reply #1 on: February 08, 2025, 04:13:20 pm »
In my little opinion you are right, a warning should be emmited for every case.

PascalDragon

  • Hero Member
  • *****
  • Posts: 5904
  • Compiler Developer
Re: missed warning
« Reply #2 on: February 08, 2025, 06:40:39 pm »
For this situation to be detected correctly you need to compile with -Oodfa (or -O3) as this is rather expensive to calculate and thus is not enabled by default.

Paolo

  • Hero Member
  • *****
  • Posts: 571
Re: missed warning
« Reply #3 on: February 08, 2025, 07:30:04 pm »
Understood, thanks.

What can happen is that if you forgot to initialize a variable, then in the first branch you see the warning, if you correct this the warning is gone and you can have the false impression that everything in the other case branchs is fine, but this should not be the case.

 

TinyPortal © 2005-2018