Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
3 tasks done
Skarvion opened this issue Oct 15, 2024 · 2 comments
Open
3 tasks done
Labels
feature New feature or request rule Improvements or additions to rules

Comments

@Skarvion
Copy link

Prerequisites

  • This rule has not already been suggested.
  • This should be a new rule, not an improvement to an existing rule.
  • This rule would be generally useful, not specific to my code or setup.

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.
@Skarvion Skarvion added feature New feature or request rule Improvements or additions to rules triage This needs to be triaged by a maintainer labels Oct 15, 2024
@Cirras
Copy link
Collaborator

Cirras commented Oct 15, 2024

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

@Cirras Cirras removed the triage This needs to be triaged by a maintainer label Oct 15, 2024
@Cirras
Copy link
Collaborator

Cirras commented Oct 15, 2024

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature New feature or request rule Improvements or additions to rules
Projects
None yet
Development

No branches or pull requests

2 participants