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

Lint proposal: prefer static member function #57108

Open
stephane-archer opened this issue Jul 8, 2024 · 7 comments
Open

Lint proposal: prefer static member function #57108

stephane-archer opened this issue Jul 8, 2024 · 7 comments
Labels
analyzer-linter Issues with the analyzer's support for the linter package area-analyzer Use area-analyzer for Dart analyzer issues, including the analysis server and code completion. linter-lint-proposal linter-status-pending type-enhancement A request for a change that isn't a bug

Comments

@stephane-archer
Copy link

suggest all member functions that can be static

example

class foo {
   void foo(String a) {
       print(a);
   }
}

suggest:

class foo {
   static void foo(String a) {
       print(a);
   }
}
@bwilkerson
Copy link
Member

Personally I'd rather have a lint to suggest converting such functions to extension methods on the first argument:

extension StringExtension on String {
  void foo() {
    print(this);
  }
}

Whether or not we have any lints like these, we should add assists for both transformations.

@lrhn
Copy link
Member

lrhn commented Jul 9, 2024

This sounds like something that cannot be determined locally.

If some other library calls foo.foo("a") then it cannot be static. That's impossible to know.
If the function references a type parameter, it cannot be static. That may be visible in the code, not necessarily in the signature.

@stephane-archer
Copy link
Author

@lrhn let me rephrase to make sure I understood correctly,
moving to:

class Foo {
   static void foo(String a) {
       print(a);
   }
}

would also mean modifying all calls like foo.foo("a") to Foo.foo("a") and result in an API breaking change for your library because an instance of a class can't access "static member function"

finding all references to foo in your codebase can be done by the editor, so I don't think this is an issue here.

Could you clarify, when a function can locally become static and will result in something impossible for the caller? instance.call() can always be turned into Type.call() from my understanding, does I miss something?

@bwilkerson
Copy link
Member

I believe that the comments from @lrhn are more related to the assist I suggested than the lint rule. We could certainly flag all instance methods that don't refer to this (either explicitly or implicitly) and leave it up to the developer to add ignore comments where the method is required to be an instance method.

@lrhn
Copy link
Member

lrhn commented Jul 9, 2024

If you convert a method to be a static function and all calls to be static function calls, then it should keep working - as long as nobody overrides the method. And as long as you can reference the type, which you should be for non-pathological code.
(Don't leak private types in your API. If you do, people may get objects of types that they cannot reference by name. That was always a bad idea, so I don't expect that to happen.)

That obviously assumes that the method can be static (doesn't reference this or type variables) and that it's not intended to be overridden. The last part is the one that's not decidable locally.

@bwilkerson
Copy link
Member

That obviously assumes that the method can be static (doesn't reference this or type variables) and that it's not intended to be overridden. The last part is the one that's not decidable locally.

Good point. And because lints can't see anything beyond the local context, such a lint might well have too many false positives to be useful.

@stephane-archer
Copy link
Author

@bwilkerson From my understanding, only functions that could be static but are intended to be overridden would make a false positive.
Because Lint rules are optional, the user will be judged if this happens a lot or not in their codebase.
In my codebase, it rarely happens, if something needs to be overridden then it's an abstract method.

@srawlins srawlins added the type-enhancement A request for a change that isn't a bug label Jul 19, 2024
@devoncarew devoncarew transferred this issue from dart-lang/linter Nov 15, 2024
@devoncarew devoncarew added analyzer-linter Issues with the analyzer's support for the linter package area-analyzer Use area-analyzer for Dart analyzer issues, including the analysis server and code completion. labels Nov 15, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
analyzer-linter Issues with the analyzer's support for the linter package area-analyzer Use area-analyzer for Dart analyzer issues, including the analysis server and code completion. linter-lint-proposal linter-status-pending type-enhancement A request for a change that isn't a bug
Projects
None yet
Development

No branches or pull requests

5 participants