Rules / Style / unnecessary-this
unnecessary-this
Flags a redundant this. qualifier on member access.
Writing this.x or this.m() is only necessary when a parameter or local
variable in scope shadows the member name; otherwise the qualifier is noise
that most Dart style guides ask you to drop. The analysis is deliberately
conservative: it over-approximates the shadow set, treating every parameter,
local variable, loop/catch/pattern binding, and closure parameter anywhere in
the member as a potential shadow, so a name that could plausibly be shadowed
is never flagged. Constructor initializer lists — where this. genuinely
disambiguates a field from a parameter — are not visited. Remove the this..
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;
}
}
Unnecessary 'this.' qualifier.
5 void m() {
6 this.x = 1;
∙
Unnecessary 'this.' qualifier.
6 this.x = 1;
7 print(this.y);
∙
Unnecessary 'this.' qualifier.
8 }
9 int get sum => this.x + this.y;
∙
Unnecessary 'this.' qualifier.
8 }
9 int get sum => this.x + this.y;
∙
Unnecessary 'this.' qualifier.
14 void update(int v) {
15 this.helper();
∙
Unnecessary 'this.' qualifier.
23 void inc() {
24 this.count += 1;
∙
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() {}
}
How to configure
Set the severity of unnecessary-this in your falcon.json:
falcon.jsonjson
{
"linter": {
"rules": {
"style": {
"unnecessary-this": "error"
}
}
}
}