sdk icon indicating copy to clipboard operation
sdk copied to clipboard

Migration tool should show reason why `var` was changed to a type, and offer a one-click way to reverse the change

Open stereotype441 opened this issue 3 years ago • 0 comments

An internal customer ran into a situation like this:

Original code (pre-null-safety):

f(Map<String, String> m, String k) {
  var s = '';
  if (m.containsKey(k)) {
    s = m[k];
    ...
  }
  ...
}

Migrated code:

f(Map<String, String> m, String k) {
  String? s = '';
  if (m.containsKey(k)) {
    s = m[k];
    ...
  }
  ...
}

The reason the migration tool changes var to String? is because it's not sophisticated enough to see that m[k] is guaranteed to return a non-null string. Unfortunately, the tool doesn't show the reasoning for this change in the "edit details" view, so the user has to stop and think to figure out what's happening.

It would be much nicer if (a) the "edit details" view showed why the migration tool wants to make s nullable (in the same way that it would if the pre-null-safety code had been String s = '';), and (b) the tool offered a one-click way to prevent the change from var to String?, so that the user could re-run migration and get:

f(Map<String, String> m, String k) {
  var s = '';
  if (m.containsKey(k)) {
    s = m[k]!;
    ...
  }
  ...
}

stereotype441 avatar Aug 04 '22 15:08 stereotype441