Wrong warning case:

Wrong warning case:

program WrongWarning;

{$APPTYPE CONSOLE}

{$R *.res}

uses
SysUtils;

var
I: Integer;

function Test(Cond: Boolean; out Value: Integer): Integer;
var
Cnt: Integer;

begin
if Cond then begin
Cnt:= 1;
Result:= 1;
end
else
Result:= 0;
if Result = 1 then
Value:= Cnt // W1036 Variable 'Cnt' might not have been initialized
else
Value:= 0;
end;

begin
Test(True, I);
end.

Comments

  1. The second condition is nonsense as setting value can be done after the first condition. No need to have transitive dependencies between the two conditions.

    ReplyDelete
  2. I must think like a compiler since I immediately thought "well there is a situation where the Cnt variable might not have been initialised". I guess the compiler is tiring to say just that ... that there might be a situation where it cannot see if the cnt variable is initialised or not.

    It's not saying that it isn't or is initialised, it's saying that there might be a situation where it cannot determine if it's initialised or not :-P

    Guess I'm one of those strange developers who doesn't second guess what the compiler tells me but always second guesses the way a piece of code is written.

    ReplyDelete
  3. Sergey Kasandrov wrote: "+Uwe Raabe There is no such path in the Test function."

    Yes, that's precisely what there is, in the Test function. The Test function doesn't know that it is going to be called only with the Cond set to TRUE.

    If that's the case, why have the parameter in the first place? You could just as well replace it with a constant; or, better yet, eliminate the IF statement altogether.

    But, since you haven't done that, this other stuff you talk about, that's not included here, presumably needs it and sometimes uses it. So the compiler can't know which branch of the IF statement will execute, so you will have to make sure all variables you later use will have assigned values regardless of which branch of the IF statement executes.

    ReplyDelete

Post a Comment