Linter: unnecessary_this and null on extensions

Created on 8 Jun 2020  Â·  13Comments  Â·  Source: dart-lang/linter

The linter "unnecessary this" shouldn't occur with extensions method and nullable:

extension aa on Object {
  String foo() => this == null ? 'yep' : 'nopes';
  bool twoChars() => this?.length == 2; // the unnecessary_this appears here, however, this can be null as within foo() above
}

void main() {
  Object a;

  print(a.foo()); // prints yep
}
bug false positive recommended

Most helpful comment

The following code runs without error:

import 'dart:math';

extension ListExt on List<bool> {
  int weight() {
    var weight = 0;
    asMap().forEach((index, element) {
      if (element) {
        weight += pow(2, index);
      }
    });

    //print("weight: " + weight.toString());

    return weight;
  }
}

main() {
  print([true, false, true].weight());
}

The only changes from what you posted are the removal of this. before asMap() and the addition of <bool> in the on clause. (The second isn't necessary, but it is cleaner because then the extension method can't be invoked on something different, such as a List<String>.)

All 13 comments

i bumped into this today. i'll have to turn this rule off until this is resolved.

@matthewkooshad : are you on a recent dev build of SDK? This should have been fixed as of https://github.com/dart-lang/linter/commit/bc2cd98bc6ee84cd5e078edbdd2eff9d57092a69.

@jodinathan: are you still seeing the issue?

@jodinathan: are you still seeing the issue?

I am on 2.9.3 and it still happens.

Thanks for checking! This was fixed in 0.1.118 which made its way into the SDK in the 2.10.0-5.0.dev release. If you bump to a more recent SDK you should see the fix.

from flutter --version, i see: Tools • Dart 2.10.0 (build 2.10.0-110.3.beta)

Super weird. The fix _should_ be in 2.10.0-110.3.beta. FWIW if doing a flutter upgrade is an option, my latest give me build 2.10.0-11.0.dev which doesn't repro. Sorry for the confusion and the lag in getting the fix to you!

flutter upgrade
Flutter is already up to date on channel beta
Flutter 1.22.0-12.1.pre • channel beta • https://github.com/flutter/flutter.git
Framework • revision 8b3760638a (8 days ago) • 2020-09-15 17:47:13 -0700
Engine • revision 4654fc6cf6
Tools • Dart 2.10.0 (build 2.10.0-110.3.beta)

i'll give it a shot the next time there's a new version of beta available.

Got it. I should have checked but it looks like I'm on master which is likely a little more fresh than you want. 😄

But... still I'm puzzled. Does the example above repro for you?

i'm actually bumping into: https://dart-lang.github.io/linter/lints/unnecessary_this.html
The message is: Don't access members with this unless avoiding shadowing.

here's one of my ext files that has the issue:

import 'dart:math';

extension ListExt on List {
  int weight() {
    var weight = 0;
    this.asMap().forEach((index, element) {
      if (element) {
        weight += pow(2, index);
      }
    });

    //print("weight: " + weight.toString());

    return weight;
  }
}

I think that message is accurate. You don't need to use this in extension methods except in cases where you'd need it in an instance methods.

hmm, i don't know a cleaner way to achieve this in the ext method.
i don't see this case come up here: https://dart.dev/guides/language/extension-methods

The following code runs without error:

import 'dart:math';

extension ListExt on List<bool> {
  int weight() {
    var weight = 0;
    asMap().forEach((index, element) {
      if (element) {
        weight += pow(2, index);
      }
    });

    //print("weight: " + weight.toString());

    return weight;
  }
}

main() {
  print([true, false, true].weight());
}

The only changes from what you posted are the removal of this. before asMap() and the addition of <bool> in the on clause. (The second isn't necessary, but it is cleaner because then the extension method can't be invoked on something different, such as a List<String>.)

Thanks Brian for the two helpful aspects for me in that example. i didn't realize that "this." was completely unnecessary in extension methods in Dart.

Was this page helpful?
0 / 5 - 0 ratings