56 lines
1.1 KiB
Plaintext
56 lines
1.1 KiB
Plaintext
== Why is this an issue?
|
|
|
|
The use of `this` is optional except when redirecting to a named constructor, and when it's needed to distinguish between property names and arguments or other variables. For the sake of brevity, `this` should be omitted when it's not strictly required.
|
|
|
|
=== Noncompliant code example
|
|
|
|
[source,dart]
|
|
----
|
|
class Car {
|
|
int color;
|
|
|
|
Car(this.color); // Mandatory to distinguish between 'color' property and 'color' parameter
|
|
|
|
void fade() {
|
|
this.color--; // Noncompliant
|
|
}
|
|
}
|
|
----
|
|
|
|
=== Compliant solution
|
|
|
|
[source,dart]
|
|
----
|
|
class Car {
|
|
int color;
|
|
|
|
Car(this.color); // Mandatory to distinguish between 'color' property and 'color' parameter
|
|
|
|
void fade() {
|
|
color--;
|
|
}
|
|
}
|
|
----
|
|
|
|
== Resources
|
|
|
|
* Dart Docs - https://dart.dev/tools/linter-rules/unnecessary_this[Dart Linter rule - unnecessary_this]
|
|
* https://dart.dev/effective-dart/usage#dont-use-this-when-not-needed-to-avoid-shadowing[Effective Dart]
|
|
|
|
ifdef::env-github,rspecator-view[]
|
|
|
|
'''
|
|
== Implementation Specification
|
|
(visible only on this page)
|
|
|
|
=== Message
|
|
|
|
Unnecessary 'this.' qualifier.
|
|
|
|
=== Highlighting
|
|
|
|
'this' keyword
|
|
|
|
|
|
endif::env-github,rspecator-view[]
|