Skip to content

Lint against invalid use of Future.value or Completer.complete #58420

@simolus3

Description

@simolus3

Describe the rule you'd like to see implemented

The optional parameters to Future.value, Completer.complete (and perhaps other functions that I'm not aware of) should be considered required when T excludes null.

For instance, both Future<int>.value() or Future<int>.value(null) are valid at compile-time because the parameter is declared to be nullable for backwards compatibility.
The SDK implementation will cast those values though, so they are guaranteed to throw at runtime.

I think we should have a lint to warn us about those invocations. It should apply to Future.value and Completer.complete when

  • the library making the call is opted into null-safety
  • T is resolved to a type that excludes null
  • the argument is absent, or not assignable to T while being assignable to T?

Examples

Future<int>.value(); // LINT!
Future<int?>.value(); 
Future<int>.value(null); // LINT!
Future<int?>.value(null);

int? someVariable;
String? anotherVariable;
Future<int>.value(someVariable); // LINT!
Future<int?>.value(someVariable);
Future<int>.value(anotherVariable); // already a compile-time error, let's not lint it as well

var completer = Completer<int>();
completer.complete(); // LINT
completer.complete(null); // LINT

var nullableCompleter = Completer<int?>();
nullableCompleter.complete();
nullableCompleter.complete(null);
// @dart=2.11
Future<int>.value(); // ok

Additional context

I'm not sure if there are other SDK methods which could benefit from this lint, or if we want to make it an annotation that third-party code could use as well, e.g.

external factory Future.value([@nullableForBackwardsCompatibility T? value]);

See also: The language discussion at dart-lang/language#1299

I am happy to implement the simple form of this lint myself if there isn't anything like it yet.

Metadata

Metadata

Assignees

No one assigned

    Labels

    devexp-linterIssues with the analyzer's support for the linter packagelegacy-area-analyzerUse area-devexp instead.type-enhancementA request for a change that isn't a bug

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions