Skip to content
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

Interface methods should be public #214

Open
3 tasks done
zaneduffield opened this issue Apr 15, 2024 · 1 comment
Open
3 tasks done

Interface methods should be public #214

zaneduffield opened this issue Apr 15, 2024 · 1 comment
Labels
feature New feature or request rule Improvements or additions to rules

Comments

@zaneduffield
Copy link
Collaborator

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

Interface methods should be public

Rule description

When implementing a method for an interface, Delphi doesn't require that the method be as visible as the interface would suggest.

The following code compiles

type
  I = interface
    procedure Foo;
  end;

  T = class(TInterfacedObject, I)
    private
      procedure Foo;
  end;

procedure T.Foo; begin end;

This is an issue because one might look at private procedure Foo and assume that the method can not be accessed from outside the declaring file. However, the method is accessible via the interface:

var
  tobj: T;
begin
  tobj.Foo;         // doesn't compile (outside of the file `T` is declared in)
  (tobj as I).Foo;  // no issues
end;

This rule would require that all interface methods are implemented with the visibility of public or published, and raise an issue whenever the visibility is lower.

Rationale

Most developers will read the visibility of a method and assume that alone determines how accessible it is. This escape-hatch for interface methods is entirely unexpected and subverts the entire point of access control.

@zaneduffield zaneduffield added triage This needs to be triaged by a maintainer rule Improvements or additions to rules feature New feature or request labels Apr 15, 2024
@Cirras
Copy link
Collaborator

Cirras commented Apr 17, 2024

I can't imagine a good use-case for this type of visibility mismatch between interface and implementing class.
This would probably be a good Sonar Way rule.

@Cirras Cirras removed the triage This needs to be triaged by a maintainer label Apr 17, 2024
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