list_remove_unrelated_type
Invocation of remove
with references of unrelated types.
此规则已从最新的 Dart 版本中移除。
详情
#NOTE: This rule is removed in Dart 3.3.0; it is no longer functional.
DON'T invoke remove
on List
with an instance of different type than the parameter type.
Doing this will invoke ==
on its elements and most likely will return false
.
BAD:
dart
void someFunction() {
var list = <int>[];
if (list.remove('1')) print('someFunction'); // LINT
}
BAD:
dart
void someFunction3() {
List<int> list = <int>[];
if (list.remove('1')) print('someFunction3'); // LINT
}
BAD:
dart
void someFunction8() {
List<DerivedClass2> list = <DerivedClass2>[];
DerivedClass3 instance;
if (list.remove(instance)) print('someFunction8'); // LINT
}
BAD:
dart
abstract class SomeList<E> implements List<E> {}
abstract class MyClass implements SomeList<int> {
bool badMethod(String thing) => this.remove(thing); // LINT
}
GOOD:
dart
void someFunction10() {
var list = [];
if (list.remove(1)) print('someFunction10'); // OK
}
GOOD:
dart
void someFunction1() {
var list = <int>[];
if (list.remove(1)) print('someFunction1'); // OK
}
GOOD:
dart
void someFunction4() {
List<int> list = <int>[];
if (list.remove(1)) print('someFunction4'); // OK
}
GOOD:
dart
void someFunction5() {
List<ClassBase> list = <ClassBase>[];
DerivedClass1 instance;
if (list.remove(instance)) print('someFunction5'); // OK
}
abstract class ClassBase {}
class DerivedClass1 extends ClassBase {}
GOOD:
dart
void someFunction6() {
List<Mixin> list = <Mixin>[];
DerivedClass2 instance;
if (list.remove(instance)) print('someFunction6'); // OK
}
abstract class ClassBase {}
abstract class Mixin {}
class DerivedClass2 extends ClassBase with Mixin {}
GOOD:
dart
void someFunction7() {
List<Mixin> list = <Mixin>[];
DerivedClass3 instance;
if (list.remove(instance)) print('someFunction7'); // OK
}
abstract class ClassBase {}
abstract class Mixin {}
class DerivedClass3 extends ClassBase implements Mixin {}
使用方法
#要启用 list_remove_unrelated_type
规则,请在你的 analysis_options.yaml
文件中,在 linter > rules 下添加 list_remove_unrelated_type
:
analysis_options.yaml
yaml
linter:
rules:
- list_remove_unrelated_type
除非另有说明,否则本网站上的文档反映的是 Dart 3.6.0。页面最后更新于 2025-02-05。 查看源代码 或 报告问题.