linter icon indicating copy to clipboard operation
linter copied to clipboard

omit_local_variable_types has spurious diagnostics when the variable type impacts inference

Open natebosch opened this issue 1 year ago • 8 comments

void main() async {
  final String s1 = await someGeneric(['']); // omit_local_variable_types
  print(typeOf(s1)); // String
  // Removing the annotation results in a static type change:
  final s2 = await someGeneric(['']);
  print(typeOf(s2)); // dynamic
}

Future<T> someGeneric<T, S extends List<T>>(S v) async => v.first;

Type typeOf<T>(T arg) => T;

The lint fires and asks for the local variable type to be removed, but doing so changes the static type of the variable. We don't correctly check when the variable impacts inference, because we only check whether the return type is a type variable.

https://github.com/dart-lang/sdk/blob/6885e82cda23c253fbe5e8720d334070ebda9d86/pkg/linter/lib/src/rules/omit_local_variable_types.dart#L132

Once the type is more complicated, like Future<T>, the lint misfires.

cc @pq @yanok

natebosch avatar Oct 05 '23 19:10 natebosch