given this code:

given this code:

var
LItem: TListItem;

[...]

LItem := nil; // * here *
Folder := GetFolder(tvFolders.Selected);
ListView.Items.BeginUpdate;
try
mFile := TGXFile.Create(Folder);
try
LItem := ListView.Items.Add;
FileToListItem(mFile, LItem);
except
on E: Exception do
begin
FreeAndNil(mFile);
raise;
end;
end;
finally
ListView.Items.EndUpdate;
end;

if Assigned(LItem) then
begin
ListView.Selected := nil;
ListView.Selected := LItem; //FI:W508 - Assignment has side effects
ListView.ItemFocused := LItem;
end;

Should the compiler really emit a hint H2077: Value assigned to 'LItem' never used in the line marked above?

Comments

  1. I think so, since both assignments to LItem will always execute without conditions (there is nothing between them which would jump out and skip the second one), so the first one seems to be superfluous.

    ReplyDelete
  2. Yes this is a good hint. Previously the compiler was wrongly ignoring it, and create other unexpected hints instead.

    ReplyDelete
  3. Yes, the code cannot reach the if statement without passing the assigment inside the try except.

    FWIW the except should be changed to finally because you are raising in any case (unless there is other code in there that you did not show here).

    ReplyDelete
  4. Stefan Glienke there is more code. But you might be right anyway. I'll have to check.

    ReplyDelete
  5. Off-topic question: in the above code is it necessary to use "on E: Exception"? Wouldn't "raise" work anyway?

    ReplyDelete
  6. It's needless to pick out only Exception. After all, you can throw things that aren't derived from Exception.

    ReplyDelete
  7. Yes, if you do not access the Exception instance E in any way, you do not need it. Also as David says you might miss an exception object based on something else (or not based at all if that's semantically possible).

    ReplyDelete

Post a Comment