unnecessary-this

Same as unnecessary_this in Dart lints

Group
Style
Path
lint/style/unnecessary-this
Default
On (recommended)
Origin
Dart lints
Rule
475 of 512
recommended

Flags a redundant this. qualifier on member access.

Lexical scopes are prebound before checking instance member bodies.

Invalid

example.dartdart
// `this.` where no parameter or local shadows the member.
class A {
  int x = 0;
  int y = 0;
  void m() {
    this.x = 1;
    print(this.y);
  }
  int get sum => this.x + this.y;
}

class B {
  int value = 0;
  void update(int v) {
    this.helper();
    value = v;
  }
  void helper() {}
}

class C {
  int count = 0;
  void inc() {
    this.count += 1;
  }
}

class SourceOrdered {
  int value = 0;
  void run() {
    print(this.value);
    final value = 1;
    print(value);
  }
}

class NestedScopes {
  int value = 0;
  void run() {
    {
      final value = 1;
      print(this.value + value);
    }
    print(this.value);
    (() {
      final value = 2;
      print(this.value + value);
    })();
    print(this.value);
  }
}

class TryScopes {
  int value = 0;
  void run() {
    try {
      final value = 1;
      print(value);
    } catch (_) {
      print(this.value);
    } finally {
      print(this.value);
    }
    print(this.value);
  }
}

class BranchScopes {
  int value = 0;
  void run(bool flag, Object subject) {
    if (flag) {
      final value = 1;
      print(this.value + value);
    } else {
      print(this.value);
    }
    if (subject case final value) {
      print(this.value + value.hashCode);
    } else {
      print(this.value);
    }
    while (flag) {
      final value = 2;
      print(this.value + value);
      break;
    }
    print(this.value);
  }
}

bool isNull(Object? value) => value == null;

class DeclaredMember {
  bool get isNull => false;
  bool check() => this.isNull;
}

class LaterNestedBlock {
  int value = 0;
  void run() {
    print(this.value);
    {
      final value = 1;
      print(value);
    }
  }
}

extension DeclaredExtension on String {
  bool get isNull => false;
  bool check() => this.isNull;
}

int headerValue = 0;
int representationValue = 0;

// Declaring header parameters are direct members, ahead of library names.
class HeaderMember(final int headerValue) {
  int read() => this.headerValue;
  int shadowed(int headerValue) => this.headerValue + headerValue;
}

extension type RepresentationMember(int representationValue) {
  int read() => this.representationValue;
  int shadowed(int representationValue) =>
      this.representationValue + representationValue;
}

class DoWhileScopes {
  int value = 0;
  void run() {
    do {
      final value = 1;
      print(this.value + value);
    } while (this.value > 0);
    print(this.value);
  }
}

class CollectionForScopes {
  int value = 0;
  void forIn(List<int> values) {
    final items = [
      for (final value in values) this.value,
    ];
    print(this.value);
    print(items);
  }
  void patternForIn(List<(int,)> values) {
    final items = [
      for (final (value,) in values) this.value,
    ];
    print(this.value);
    print(items);
  }
  void variableHeader() {
    final items = [
      for (var value = 0; value < 1; value++) this.value,
    ];
    print(this.value);
    print(items);
  }
}

class CollectionIfCaseScopes {
  int value = 0;
  void run(Object? subject) {
    final items = [
      if (subject case final value? when this.value > 0)
        this.value,
    ];
    print(this.value);
    if (subject case final value?) {
      final nested = [
        if (subject case final other?) other,
      ];
      print(this.value + value.hashCode);
      print(nested);
    }
    print(this.value);
    print(items);
  }
}

class PatternDeclarationReportOrder {
  int value = 0;
  (int, int) pair = (0, 0);
  void run() {
    // Initializer diagnostics precede pattern diagnostics.
    var (== this.value, _) = this.pair;
  }
}
Remove the unnecessary `this.` qualifier.
5 void m() {
6 this.x = 1;
Remove the unnecessary `this.` qualifier.
6 this.x = 1;
7 print(this.y);
Remove the unnecessary `this.` qualifier.
8 }
9 int get sum => this.x + this.y;
Remove the unnecessary `this.` qualifier.
8 }
9 int get sum => this.x + this.y;
Remove the unnecessary `this.` qualifier.
14 void update(int v) {
15 this.helper();
Remove the unnecessary `this.` qualifier.
23 void inc() {
24 this.count += 1;
Remove the unnecessary `this.` qualifier.
43 }
44 print(this.value);
Remove the unnecessary `this.` qualifier.
48 })();
49 print(this.value);
Remove the unnecessary `this.` qualifier.
59 } catch (_) {
60 print(this.value);
Remove the unnecessary `this.` qualifier.
61 } finally {
62 print(this.value);
Remove the unnecessary `this.` qualifier.
63 }
64 print(this.value);
Remove the unnecessary `this.` qualifier.
74 } else {
75 print(this.value);
Remove the unnecessary `this.` qualifier.
79 } else {
80 print(this.value);
Remove the unnecessary `this.` qualifier.
86 }
87 print(this.value);
Remove the unnecessary `this.` qualifier.
94 bool get isNull => false;
95 bool check() => this.isNull;
Remove the unnecessary `this.` qualifier.
100 void run() {
101 print(this.value);
Remove the unnecessary `this.` qualifier.
110 bool get isNull => false;
111 bool check() => this.isNull;
Remove the unnecessary `this.` qualifier.
118class HeaderMember(final int headerValue) {
119 int read() => this.headerValue;
Remove the unnecessary `this.` qualifier.
123extension type RepresentationMember(int representationValue) {
124 int read() => this.representationValue;
Remove the unnecessary `this.` qualifier.
134 print(this.value + value);
135 } while (this.value > 0);
Remove the unnecessary `this.` qualifier.
135 } while (this.value > 0);
136 print(this.value);
Remove the unnecessary `this.` qualifier.
143 final items = [
144 for (final value in values) this.value,
Remove the unnecessary `this.` qualifier.
145 ];
146 print(this.value);
Remove the unnecessary `this.` qualifier.
150 final items = [
151 for (final (value,) in values) this.value,
Remove the unnecessary `this.` qualifier.
152 ];
153 print(this.value);
Remove the unnecessary `this.` qualifier.
168 final items = [
169 if (subject case final value? when this.value > 0)
Remove the unnecessary `this.` qualifier.
169 if (subject case final value? when this.value > 0)
170 this.value,
Remove the unnecessary `this.` qualifier.
171 ];
172 print(this.value);
Remove the unnecessary `this.` qualifier.
179 }
180 print(this.value);
Remove the unnecessary `this.` qualifier.
189 // Initializer diagnostics precede pattern diagnostics.
190 var (== this.value, _) = this.pair;
Remove the unnecessary `this.` qualifier.
189 // Initializer diagnostics precede pattern diagnostics.
190 var (== this.value, _) = this.pair;

Valid

example.dartdart
// `this.` needed because of a shadowing parameter or local, or no `this.`.
class A {
  int x = 0;
  void m(int x) {
    this.x = x;
  }
}

class B {
  int value = 0;
  void update() {
    var value = 5;
    this.value = value;
  }
}

class C {
  int count = 0;
  void inc() {
    count += 1;
  }
  int get doubled => count * 2;
}

class D {
  int total = 0;
  void add(int total) {
    this.total += total;
  }
}

class E {
  void run(int helper) {
    this.helper();
  }
  void helper() {}
}

class ScopedBindings {
  int value = 0;

  void catchesAndLoops() {
    try {
      throw 1;
    } catch (value) {
      print(this.value + value.hashCode);
    }
    for (final value in [1]) {
      print(this.value + value);
    }
    if (Object() case final value) {
      print(this.value + value.hashCode);
    }
  }

  void closures() {
    ((value) => print(this.value + value))(1);
  }
}

class InitializerScope {
  int value = 1;
  void ordinary() {
    final value = this.value;
    print(value);
  }
  void pattern() {
    final (value,) = (this.value,);
    print(value);
  }
  void later() {
    print(this.value);
    final value = 1;
    print(value);
  }
  void capturedLater() {
    void read() => print(this.value);
    final value = 1;
    read();
    print(value);
  }
}

bool isNull(Object? value) => value == null;

extension NullableCheck on Object? {
  bool get isNull => this == null;
}

class ExtensionMember {
  bool check() => this.isNull;
}

class InheritedBase {
  bool get isNull => false;
}

class InheritedMember extends InheritedBase {
  bool check() => this.isNull;
}

class LoopInitializers {
  int x = 0;
  int y = 1;
  List<int> values = [1];
  void run() {
    for (var x = this.x; x < 1; x++) {}
    for (var x = this.y, y = 1; x < y; x++) {}
    for (final values in this.values) {
      print(values);
    }
  }
}

class SwitchGuardBodyScope {
  int y = 0;
  void run(int v) {
    switch (v) {
      // The SDK linter's ancestor scope includes the case body's declarations.
      case _ when this.y > 0:
        print(this.y);
        var y = 1;
        print(y);
    }
  }
}

Configure unnecessary-this

Set its severity in falcon.json. See the configuration guide.

falcon.jsonjson
{
  "linter": {
    "rules": {
      "style": {
        "unnecessary-this": "error"
      }
    }
  }
}

Or silence one occurrence with a suppression comment; the reason is required.

example.dartdart
// falcon-ignore lint/style/unnecessary-this: <reason>
  • unreachable-from-mainunreachable_from_main

    Report public declarations that no entry point of an executable library reaches.

  • use-colored-boxuse_colored_boxrecommendedflutter

    Prefers ColoredBox over a Container that only paints a color behind its child.

  • use-decorated-boxuse_decorated_boxrecommendedflutter

    Prefers DecoratedBox over a Container that only decorates its child.

  • use-design-system-itemuse-design-system-itemflutter

    Flags construction of a widget or type that should be replaced by its design-system equivalent.

  • use-enumsuse_enums

    Flags classes that emulate an enum and could be declared as one.

  • use-existing-destructuring

    Reuse or extend an existing record destructuring instead of rereading a field.

  • use-existing-variable

    Reuse a local for stable SDK arithmetic and String.length/int parity getters.

  • use-full-hex-values-for-flutter-colorsuse_full_hex_values_for_flutter_colorsrecommendedflutter

    Require an eight-digit hexadecimal integer for the canonical Flutter Color default constructor.