sdk icon indicating copy to clipboard operation
sdk copied to clipboard

`void_checks` quick-fix suggestion

Open FMorschel opened this issue 5 months ago • 3 comments

Repro:

void foo(void _) {}
void bar({void value}) {}

void main() {
  foo(0);
  bar(value: 0);
}

When the value is required, we should replace it with null, and when it isn't, we should remove the given parameter.

CC @bwilkerson

FMorschel avatar Jun 12 '25 16:06 FMorschel

Both of those fixes seem reasonable, but I probably wouldn't spend time working on them. I suspect that the number of parameters with a value of void is exceedingly small. (If I'm wrong I'd be happy to change my mind.)

The real value would be a lint that would prevent the use of void as (a) the type of a local variable, parameter, field, or top-level variable or as the return type of a getter. (I might make an exception for a parameter if the method overrides another method, as I could imagine a use case for that, though I'd find a different solution.)

bwilkerson avatar Jun 12 '25 18:06 bwilkerson

We already have a fix for avoid_redundant_argument_values, which with some small tweaks should work here.

void bar({int myValue = 0}) {
  bar(myValue: 0);  // <- Remove parameter
}

The real value would be a lint that would prevent the use of void as (a) the type of a local variable, parameter, field, or top-level variable or as the return type of a getter. (I might make an exception for a parameter if the method overrides another method, as I could imagine a use case for that, though I'd find a different solution.)

Agreed. If you ever think this should be done with some priority, please let me know so I can help on this too. Do you want me to open another issue just so this doesn't get lost?

FMorschel avatar Jun 12 '25 18:06 FMorschel

For related discussions, see

  • https://github.com/dart-lang/sdk/issues/58053
  • https://github.com/dart-lang/sdk/issues/58765

I added a question to one of them.

bwilkerson avatar Jun 12 '25 18:06 bwilkerson