Skip to content

analyzer does not report an error on incorrect code in strong mode #31858

Closed
@mraleph

Description

@mraleph
typedef T RecognizerCallback<T>();

class Test {
  T invokeCallback<T>(RecognizerCallback<T> callback) {
    return callback();
  }

  void callback() {
  }

  void test() {
    invokeCallback<String>(callback);
    invokeCallback<Null>(callback);
  }
}
╭─ ~/src/dart/sdk ‹master›
╰─$ sdk/bin/dartanalyzer --strong -v t/analysis.dart                                                                                3 ↵
Analyzing t/analysis.dart...
Using default analysis options
No issues found!

If I now move the code to top-level scope then analyzer reports an error

typedef T RecognizerCallback<T>();

  T invokeCallback<T>(RecognizerCallback<T> callback) {
    return callback();
  }

  void callback() {
  }

  void test() {
    invokeCallback<String>(callback);
    invokeCallback<Null>(callback);
  }
╭─ ~/src/dart/sdk ‹master›
╰─$ sdk/bin/dartanalyzer --strong -v t/analysis.dart
Analyzing t/analysis.dart...
Using default analysis options
  error • The function 'callback' has type '() → void' that isn't of expected type '() → String'. This means its parameter or return type does not match what is expected at t/analysis.dart:13:28 • strong_mode_invalid_cast_function
  error • The function 'callback' has type '() → void' that isn't of expected type '() → Null'. This means its parameter or return type does not match what is expected at t/analysis.dart:14:26 • strong_mode_invalid_cast_function
1 error found.

[Flutter is full of this sort of mistyped code (except they use Null instead of String) , so once this is fixed we would heed to do a pass over their code and fix it /cc @leafpetersen ]

@devoncarew could you help triage this? Thanks.

Metadata

Metadata

Assignees

Labels

P2A bug or feature request we're likely to work onlegacy-area-analyzerUse area-devexp instead.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions