no_self_assignments
Don't assign a variable to itself.
此规则自 Dart 3.1 版本起可用。
详情
#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;
}
}
使用方法
#要启用 no_self_assignments
规则,请在你的 analysis_options.yaml
文件中,在 linter > rules 下添加 no_self_assignments
:
analysis_options.yaml
yaml
linter:
rules:
- no_self_assignments
除非另有说明,否则本网站上的文档反映的是 Dart 3.6.0。页面最后更新于 2025-02-05。 查看源代码 或 报告问题.