integrated-application-development / sonar-delphi

Delphi language plugin for SonarQube
GNU Lesser General Public License v3.0
104 stars 15 forks source link

New Rule: Class method should not be invoked from an object instance #303

Open Skarvion opened 2 weeks ago

Skarvion commented 2 weeks ago

Prerequisites

Suggested rule title

Class method should not be invoked from an object instance

Rule description

Calling a class method from an object instance would mislead reader that the method is an object method and may affect the object itself. Call class method using class name as prefix.

Rationale

// class declaration and implemeantion
type
  TFoo = class(TObject)
  public
    class procedure Bar;
  end;

implementation

class procedure TFoo.Bar;
begin
  // do something
end;

// calling
var
  FooObj: TFoo;
begin
  FooObj := TFoo.Create;
  FooObj.Bar; // non-compliant

  TFoo.Bar; // compliant
end.
Cirras commented 2 weeks ago

Agreed that a rule (or rules) to enforce "class constants/variables/methods should be referenced statically" would be useful.

Cirras commented 2 weeks ago

Class methods can be polymorphic, so we should probably exclude virtual class methods from the rule.