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
Flags a redundant this. qualifier on member access.
Lexical scopes are prebound before checking instance member bodies.
Invalid
// `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;
}
} void m() { this.x = 1; this.x = 1; print(this.y); } int get sum => this.x + this.y; } int get sum => this.x + this.y; void update(int v) { this.helper(); void inc() { this.count += 1; } print(this.value); })(); print(this.value); } catch (_) { print(this.value); } finally { print(this.value); } print(this.value); } else { print(this.value); } else { print(this.value); } print(this.value); bool get isNull => false; bool check() => this.isNull; void run() { print(this.value); bool get isNull => false; bool check() => this.isNull;class HeaderMember(final int headerValue) { int read() => this.headerValue;extension type RepresentationMember(int representationValue) { int read() => this.representationValue; print(this.value + value); } while (this.value > 0); } while (this.value > 0); print(this.value); final items = [ for (final value in values) this.value, ]; print(this.value); final items = [ for (final (value,) in values) this.value, ]; print(this.value); final items = [ if (subject case final value? when this.value > 0) if (subject case final value? when this.value > 0) this.value, ]; print(this.value); } print(this.value); // Initializer diagnostics precede pattern diagnostics. var (== this.value, _) = this.pair; // Initializer diagnostics precede pattern diagnostics. var (== this.value, _) = this.pair;Valid
// `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.
{
"linter": {
"rules": {
"style": {
"unnecessary-this": "error"
}
}
}
}Or silence one occurrence with a suppression comment; the reason is required.
// falcon-ignore lint/style/unnecessary-this: <reason>More style rules
- unreachable-from-main
unreachable_from_mainReport public declarations that no entry point of an executable library reaches.
- use-colored-box
use_colored_boxrecommendedflutterPrefers
ColoredBoxover aContainerthat only paints a color behind its child. - use-decorated-box
use_decorated_boxrecommendedflutterPrefers
DecoratedBoxover aContainerthat only decorates its child. - use-design-system-item
use-design-system-itemflutterFlags construction of a widget or type that should be replaced by its design-system equivalent.
- use-enums
use_enumsFlags 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-colors
use_full_hex_values_for_flutter_colorsrecommendedflutterRequire an eight-digit hexadecimal integer for the canonical Flutter Color default constructor.