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

void should not have the same behavior as dynamic #53759

Closed
hgraceb opened this issue Oct 15, 2023 · 1 comment
Closed

void should not have the same behavior as dynamic #53759

hgraceb opened this issue Oct 15, 2023 · 1 comment
Labels
area-analyzer Use area-analyzer for Dart analyzer issues, including the analysis server and code completion.

Comments

@hgraceb
Copy link
Contributor

hgraceb commented Oct 15, 2023

Description

  • When using <Null>, it prompts Unnecessary use of the type 'Null'. and suggests Try using 'void' instead.
  • After changing <Null> to <void>, the behavior completely changed.

Example

https://dartpad.dev/33706e19df021e52d98c?id=2a911d811fe62681110cd69734328c22

class Class<T> {
  isNull() => null is T;

  isBool() => true is T;

  isString() => '' is T;

  isNever() => Never is T;
}

void main() {
  print('Class<Null>().isNull(): ${Class<Null>().isNull()}');           // true
  print('Class<Null>().isBool(): ${Class<Null>().isBool()}');           // false
  print('Class<Null>().isString(): ${Class<Null>().isString()}');       // false
  print('Class<Null>().isNever(): ${Class<Null>().isNever()}');         // false

  print('Class<void>().isNull(): ${Class<void>().isNull()}');           // true
  print('Class<void>().isBool(): ${Class<void>().isBool()}');           // true
  print('Class<void>().isString(): ${Class<void>().isString()}');       // true
  print('Class<void>().isNever(): ${Class<void>().isNever()}');         // true

  print('Class<String>().isNull(): ${Class<String>().isNull()}');       // false
  print('Class<String>().isBool(): ${Class<String>().isBool()}');       // false
  print('Class<String>().isString(): ${Class<String>().isString()}');   // true
  print('Class<String>().isNever(): ${Class<String>().isNever()}');     // false

  print('Class<dynamic>().isNull(): ${Class<dynamic>().isNull()}');     // true
  print('Class<dynamic>().isBool(): ${Class<dynamic>().isBool()}');     // true
  print('Class<dynamic>().isString(): ${Class<dynamic>().isString()}'); // true
  print('Class<dynamic>().isNever(): ${Class<dynamic>().isNever()}');   // true
}
Snipaste_2023-10-15_14-04-43

Expected

  • There is no suggestion to change <Null> to <void>.
  • Modify the behavior of <void> if necessary.
@lrhn
Copy link
Member

lrhn commented Oct 15, 2023

The behavior of void is correct, it's a supertype of any type, which is why any type can be assigned to it.

The warning is the issue.
It's a known issue that the "prefer void to null" lint has false positives, and might have outlived its usefulness: #59309

@lrhn lrhn added the area-analyzer Use area-analyzer for Dart analyzer issues, including the analysis server and code completion. label Oct 15, 2023
@hgraceb hgraceb closed this as completed Oct 15, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-analyzer Use area-analyzer for Dart analyzer issues, including the analysis server and code completion.
Projects
None yet
Development

No branches or pull requests

2 participants