no_self_assignments
Don't assign a variable to itself.
This rule is available as of Dart 3.1.
Details
#DON'T assign a variable to itself. Usually this is a mistake.
BAD:
dart
class C {
int x;
C(int x) {
x = x;
}
}
GOOD:
dart
class C {
int x;
C(int x) : x = x;
}
GOOD:
dart
class C {
int x;
C(int x) {
this.x = x;
}
}
BAD:
dart
class C {
int _x = 5;
int get x => _x;
set x(int x) {
_x = x;
_customUpdateLogic();
}
void _customUpdateLogic() {
print('updated');
}
void example() {
x = x;
}
}
GOOD:
dart
class C {
int _x = 5;
int get x => _x;
set x(int x) {
_x = x;
_customUpdateLogic();
}
void _customUpdateLogic() {
print('updated');
}
void example() {
_customUpdateLogic();
}
}
BAD:
dart
class C {
int x = 5;
void update(C other) {
this.x = this.x;
}
}
GOOD:
dart
class C {
int x = 5;
void update(C other) {
this.x = other.x;
}
}
Usage
#To enable the no_self_assignments
rule,
add no_self_assignments
under linter > rules in your
analysis_options.yaml
file:
analysis_options.yaml
yaml
linter:
rules:
- no_self_assignments
除非另有说明,文档之所提及适用于 Dart 3.5.3 版本,本页面最后更新时间: 2024-08-02。 查看文档源码 或者 报告页面问题。