Skip to main content

unnecessary_this_alias

Details about the 'unnecessary_this_alias' diagnostic produced by the Dart analyzer.

Unnecessary 'this' alias.

Description

#

The analyzer produces this diagnostic when a local variable is initialized to this and the variable is only used in ways that could just use this.

Example

#

The following code produces this diagnostic because the local variable self is initialized to this, is never reassigned, and its only usage is a promotion check that could be performed on this directly under the this-promotion feature:

dart
class C {
  void m() {
    var self = this;
    if (self is D) {
      // ...
    }
  }
}

class D extends C {}

Common fixes

#

Remove the local variable and use this directly:

dart
class C {
  void m() {
    if (this is D) {
      // ...
    }
  }
}

class D extends C {}