Skip to content

prefer_void_to_null false positive in case of nullable generics #58959

New issue

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

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

Already on GitHub? # to your account

Open
maRci002 opened this issue Dec 9, 2022 · 3 comments
Open

prefer_void_to_null false positive in case of nullable generics #58959

maRci002 opened this issue Dec 9, 2022 · 3 comments
Labels
area-devexp For issues related to the analysis server, IDE support, linter, `dart fix`, and diagnostic messages. devexp-linter Issues with the analyzer's support for the linter package linter-false-positive linter-set-recommended P2 A bug or feature request we're likely to work on type-bug Incorrect behavior (everything from a crash to more subtle misbehavior)

Comments

@maRci002
Copy link

maRci002 commented Dec 9, 2022

Describe the issue

abstract class Data {}

abstract class DataHolder<T extends Data?> {
  const DataHolder({required this.data});

  final T? data;

  bool isNullable() => null is T;
}

class FooData extends Data {}

class FooHolder extends DataHolder<FooData> {
  FooHolder({required super.data});
}

// prefer_void_to_null: Don't use the Null type, unless you are positive that you don't want void.
class ExplicitNullHolder extends DataHolder<Null> {
  ExplicitNullHolder() : super(data: null);
}

class ImplicitNullHolder extends DataHolder {
  ImplicitNullHolder() : super(data: null);
}

void main() {
  print(FooHolder(data: FooData()).isNullable()); // false
  print(ExplicitNullHolder().isNullable()); // true
  print(ImplicitNullHolder().isNullable()); // true
}

Expected behavior
Using explicit Null shouldn't trigger prefer_void_to_null lint rule.

@bwilkerson
Copy link
Member

There are two potential problems here:

  • Null should be allowed when the bound is nullable, and
  • void shouldn't be suggested when void isn't compatible with the bound.

The second condition might only apply when there's a bug in the code to start with (for example, making the bound for T be Data), but it's still a case where the lint shouldn't produce a diagnostic.

@bwilkerson bwilkerson added the P2 A bug or feature request we're likely to work on label Dec 13, 2022
@maRci002
Copy link
Author

Null should be allowed when the bound is nullable

It is allowed, the program compiles successfully.

@bwilkerson
Copy link
Member

I meant: allowed by the lint (as in not flagged as being a problem).

@srawlins srawlins added the type-bug Incorrect behavior (everything from a crash to more subtle misbehavior) label Mar 29, 2024
@devoncarew devoncarew added devexp-linter Issues with the analyzer's support for the linter package legacy-area-analyzer Use area-devexp instead. labels Nov 19, 2024
@devoncarew devoncarew transferred this issue from dart-archive/linter Nov 19, 2024
@bwilkerson bwilkerson added area-devexp For issues related to the analysis server, IDE support, linter, `dart fix`, and diagnostic messages. and removed legacy-area-analyzer Use area-devexp instead. labels Feb 21, 2025
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
area-devexp For issues related to the analysis server, IDE support, linter, `dart fix`, and diagnostic messages. devexp-linter Issues with the analyzer's support for the linter package linter-false-positive linter-set-recommended P2 A bug or feature request we're likely to work on type-bug Incorrect behavior (everything from a crash to more subtle misbehavior)
Projects
None yet
Development

No branches or pull requests

5 participants