0
Fixed

[W527] False positive

Lübbe Onken 10 years ago updated by HFUKUSHI 10 years ago 12
W527 returns a false positive when an encapsulated object with the same property name is accessed in the getter/setter.

Example:
TMyObj = class
private
  FSubObject : TSubObject;
  function GetText: String;
public
  property Text:string read GetText;
end;

function TMyObj.GetText: String;
begin
  try
    Result := FSubObject.Text
  except
    Result := 'Not Assigned';
  end
end;
It also happens when the encapsulated object is a lookup table of objects/records, so it's not just the name of the encapsulated objects property as I initially thought.

function TVersionInfo.GetLanguage(Index: integer): WORD;
begin
  result := TranslationTable[Index].LanguageID;
end;
+1
You have declared "property LanguageID: WORD read GetLanguage", right?
You're right, the declaration is:
   property LanguageID[Index: integer]: WORD read GetLanguage;
Should fix this in my code :)
+1
I wonder if this would make sense as another convention "Getter/Setter Name is different from property declaration" ?
The convention would be that the getter/setter name = Get<propertyname>/Set<propertyname>
Good idea! Thanks :)
I'll add this to the ideas section ok?
It would be great :)

On FixInsight 2016.03, this problem seems to be reproduced. See below codes.


unit Unit2;


interface


uses
System.Generics.Collections;


type
TChild = class(TObject)
end;


TChildList = TObjectList<TChild>;


TParent = class(TObject)
private
FChildList: TObjectList<TChild>;
function GetItems(Index: Integer): TChild;
public
constructor Create;
destructor Destroy; override;
property Items[Index: Integer]: TChild read GetItems;
end;


implementation


constructor TParent.Create;
begin
inherited;
FChildList := TObjectList<TChild>.Create(True);
end;


destructor TParent.Destroy;
begin
FChildList.Free;
inherited;
end;


function TParent.GetItems(Index: Integer): TChild;
begin
Result := FChildList.Items[Index]; // W527 Property access in getter or setter
// Result := FChildList[Index]; // OK
end;


end.

On FixInsight 2016.04, W527 false positive problem is resolved. Thank you.

in FixInsight 2016.03 I still get a similar warning:


function TGrepHistoryList.GetItems(AIndex: Integer): TGrepHistoryListItem;

begin
Result := FHistoryList[FListMode].Items[AIndex];
end;

That's a false positive FHistoryList[].Items is not the property to which GetItems is the getter.