Casting an object only to free it is superfluous. TObject.Free will always call the
correct destructor, regardless of the variable's type.
Also, because the Free method is on TObject, it is present on
all objects inheriting from TObject - no casting is needed to access it.
This rule excludes cases in which the object being cast is untyped, as an untyped variable
would need to be cast to be used with FreeAndNil or TObject.Free.
TMyObject(MyVar).Free; // Noncompliant (MyVar as TMyObject).Free; // Noncompliant FreeAndNil(TMyObject(MyVar)); // Noncompliant FreeAndNil(MyVar as TMyObject); // Noncompliant MyVar.Free; // Compliant FreeAndNil(MyVar); // Compliant
Remove the unnecessary cast:
TMyObject(MyVar).Free; FreeAndNil(TMyObject(MyVar));
MyVar.Free; FreeAndNil(MyVar);