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

[avoid_classes_with_only_static_members ] consider reverting fix to flag classes w/ only static methods #58459

Closed
pq opened this issue Jul 20, 2021 · 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. customer-flutter type-task A well-defined stand-alone task

Comments

@pq
Copy link
Member

pq commented Jul 20, 2021

The change is dart-lang/linter@b48290e addressing #58439 and is currently blocking an SDK roll (try here).

flutter-analyze-try

   info • Avoid defining a class that contains only static members • dev/integration_tests/flutter_gallery/lib/demo/shrine/model/products_repository.dart:7:1 • avoid_classes_with_only_static_members
   info • Avoid defining a class that contains only static members • packages/flutter/lib/src/widgets/binding.dart:1234:1 • avoid_classes_with_only_static_members
   info • Avoid defining a class that contains only static members • packages/flutter/test/widgets/widget_inspector_test.dart:232:1 • avoid_classes_with_only_static_members
   info • Avoid defining a class that contains only static members • packages/flutter_driver/lib/src/common/fuchsia_compat.dart:79:1 • avoid_classes_with_only_static_members
   info • Avoid defining a class that contains only static members • packages/flutter_test/lib/src/event_simulation.dart:26:1 • avoid_classes_with_only_static_members

See: https://logs.chromium.org/logs/dart/buildbucket/cr-buildbucket.appspot.com/8841227602458710784/+/u/analyze_flutter_flutter/stdout

A few violations are in tests but 2 are API:

/fyi @goderbauer @bwilkerson

@pq pq added customer-flutter type-task A well-defined stand-alone task labels Jul 20, 2021
@bwilkerson
Copy link
Member

My understanding is that the fix is correct and that the lint was previously broken.

If that's true, and if the cases that the lint is flagging are intentional, then perhaps we should consider marking them with an ignore comment rather than reverting the fix.

@goderbauer
Copy link
Contributor

https://github.com/flutter/flutter/blob/38c2af6a67ff8cefc8f6d8dee79584357fa2b941/packages/flutter/lib/src/widgets/binding.dart#L1237

This looks like a false positive to me that should not be flagged by the lint. The class's purpose is to combine several mixins into a usable (i.e. instantiable) class. It's arguably not defining a class that is just holding statics.

https://github.com/flutter/flutter/blob/38c2af6a67ff8cefc8f6d8dee79584357fa2b941/packages/flutter_driver/lib/src/common/fuchsia_compat.dart#L85

This one appears to be correctly flagged. I'd be OK with throwing an ignore on this one as it appears to be intentional.

@pq
Copy link
Member Author

pq commented Jul 20, 2021

This looks like a false positive to me that should not be flagged by the lint.

Quite right. It's interesting but the implementation is ignoring inherited members entirely (and that doesn't seem right).

This one appears to be correctly flagged. I'd be OK with throwing an ignore on this one as it appears to be intentional.

Cool. Should we take the same approach for the classes in test/?

@goderbauer
Copy link
Contributor

Does the lint fire if you add a private constructor to a class with static-only members to make it non-instantiable?

class Foo {
  // This class is not meant to be instantiated or extended; this constructor
  // prevents instantiation and extension.
  Foo._();

  // static members
}

About the other failures:

dev/integration_tests/flutter_gallery/lib/demo/shrine/model/products_repository.dart:7:1

We should refactor this to just remove the class.

packages/flutter/test/widgets/widget_inspector_test.dart:232:1

This seems like a little bit of a hack, but I think this is also a false positive since the class inherits non-static members.

packages/flutter/lib/src/widgets/binding.dart:1234:1

False positive as discussed above.

packages/flutter_driver/lib/src/common/fuchsia_compat.dart:79:1
packages/flutter_test/lib/src/event_simulation.dart:26:1

These two should get a private empty constructor to mark it as non-instantiable. That will hopefully shut up the lint. If not, ignore seems fine here.

@pq
Copy link
Member Author

pq commented Jul 20, 2021

Does the lint fire if you add a private constructor to a class with static-only members to make it non-instantiable?

Nope. This will not fire the lint.

I'm working on a fix for the false positives but would you be up for helping with the proposed refactor? No worries either way.

@goderbauer
Copy link
Contributor

I'm working on a fix for the false positives but would you be up for helping with the proposed refactor? No worries either way.

I'll put together a PR for flutter to do that refactoring and to add the private constructors.

@pq
Copy link
Member Author

pq commented Jul 21, 2021

Thanks for the help @goderbauer! With my fixes and your changes, I think we can close this out.

image

🎉

@pq pq closed this as completed Jul 21, 2021
@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 19, 2024
@devoncarew devoncarew transferred this issue from dart-lang/linter Nov 19, 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. customer-flutter type-task A well-defined stand-alone task
Projects
None yet
Development

No branches or pull requests

4 participants