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

Analyzer should report a hint when a Future.then() callback fails to return the proper type. #18531

Closed
stereotype441 opened this issue Apr 29, 2014 · 6 comments
Labels
analyzer-warning Issues with the analyzer's Warning codes area-analyzer Use area-analyzer for Dart analyzer issues, including the analysis server and code completion. P2 A bug or feature request we're likely to work on type-enhancement A request for a change that isn't a bug

Comments

@stereotype441
Copy link
Member

The following code illustrates a mistake I have made frequently, and would like the analyzer to issue a hint about:

import 'dart:async';

Future foo(int i) {
  return new Future(() {});
}

Future<int> bar() {
  return foo(1).then(() {
    /* return */ foo(2).then((
) {
      return 5;
    });
  });
}

I meant to create a method bar() that would perform foo(1) asynchronously, then perform foo(2) asynchronously, then complete the result with the number 5. However, because I left out a "return" keyword, the closure passed to foo(1).then() returns null. (Uncommenting the "return" keyword fixes the mistake).

It would be nice if the analyzer could infer that the closure passed to foo(1).then() was intended to return either an int or a Future<int>, and issue a warning to tell me that it's returning null instead.

@bwilkerson
Copy link
Member

Added this to the Later milestone.
Removed Priority-Unassigned label.
Added Priority-Medium, Analyzer-Hint labels.

@kasperl
Copy link

kasperl commented Jul 10, 2014

Removed this from the Later milestone.
Added Oldschool-Milestone-Later label.

@kasperl
Copy link

kasperl commented Aug 4, 2014

Removed Oldschool-Milestone-Later label.

@stereotype441
Copy link
Member Author

An alternative (and more general) fix would be to generate a hint when a return value of type Future is ignored by the caller.

In addition to catching the issue documented above, it would have caught the bug fixed by revision 40637.

The challenge would be to find an easy way to allow the user to suppress the hint in the (presumably rare) case where ignoring the future really is the correct thing to do.

@DartBot
Copy link

DartBot commented Sep 25, 2014

This comment was originally written by @zoechi


Thanks for creating this issue.
This is a real pain point and I stumble upon this regularly myself and I think it is the most common Dart question on StackOverflow (why doesn't do this code what I expect).

@stereotype441 stereotype441 added Type-Enhancement area-analyzer Use area-analyzer for Dart analyzer issues, including the analysis server and code completion. analyzer-warning Issues with the analyzer's Warning codes labels Sep 25, 2014
@kevmoo kevmoo added P2 A bug or feature request we're likely to work on type-enhancement A request for a change that isn't a bug and removed triaged labels Mar 1, 2016
@matanlurey matanlurey added the closed-obsolete Closed as the reported issue is no longer relevant label Mar 11, 2018
@bwilkerson bwilkerson reopened this Mar 12, 2018
@bwilkerson bwilkerson removed the closed-obsolete Closed as the reported issue is no longer relevant label Mar 12, 2018
@srawlins
Copy link
Member

This has been fixed. The analyzer now reports errors for the above example:

$ dartanalyzer --version
dartanalyzer version 2.0.0-dev.63.0
$ cat 18531.dart 
import 'dart:async';

Future foo(int i) {
  return new Future(() {});
}

Future<int> bar() {
  return foo(1).then(() {
    /* return */ foo(2).then(() {
      return 5;
    });
  });
}
$ dartanalyzer 18531.dart 
Analyzing 18531.dart...
  error • The argument type '() → Null' can't be assigned to the parameter type '(dynamic) → FutureOr<int>' at 18531.dart:8:22 • argument_type_not_assignable
  error • The argument type '() → int' can't be assigned to the parameter type '(dynamic) → FutureOr<dynamic>' at 18531.dart:9:30 • argument_type_not_assignable
2 errors found.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
analyzer-warning Issues with the analyzer's Warning codes area-analyzer Use area-analyzer for Dart analyzer issues, including the analysis server and code completion. P2 A bug or feature request we're likely to work on type-enhancement A request for a change that isn't a bug
Projects
None yet
Development

No branches or pull requests

7 participants