The compiler shows me the following warning for the code below:
Warning: W1036 Variable 'Address' might not have been initialized
The code (an MVCE snippet based on real code):
function DoFoo(): Integer;
var
i: Integer;
Address, Bar: Cardinal;
begin
for i := 1 to 5 do
begin
try
Address := Hex2CardPos(IntToStr(i));
except on EConvertError do
continue;
end;
Bar := Address + 42; // "Warning: Address might not have been initialized"
end;
Result := 42;
end;
As you can see, Address
is either:
Hex2CardPos()
Hex2CardPos()
throws an error and the loop iteration is immediately skipped.I tried to fix this by adding a useless Address := 0;
to the beginning of the loop, but then the warning is just replaced with another:
Hint: H2077 Value assigned to 'Address' never used.
Is this a compiler bug or does the warning have substance?
The problem is in your code. "Bar"
assignation has to be in the try except block because when an exception happens you dont want assign "Bar"
function DoFoo(): Integer;
var
i: Integer;
Address, Bar: Cardinal;
begin
for i := 1 to 5 do
begin
try
Address := Hex2CardPos(IntToStr(i));
Bar := Address + 42;
except on EConvertError do
continue;
end;
end;
Result := 42;
end;
Btw this code has a "H2077 Value assigned to 'Bar' never used"
that's correct.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With