目录

list_remove_unrelated_type

使用不相关类型的引用调用 remove

此规则已在最新的 Dart 版本中删除。

详细信息

#

注意:此规则已在 Dart 3.3.0 中删除;它不再起作用。

不要List 上使用与参数类型不同的实例调用 remove

这样做会在其元素上调用 ==,并且很可能会返回 false

错误

dart
void someFunction() {
  var list = <int>[];
  if (list.remove('1')) print('someFunction'); // LINT
}

错误

dart
void someFunction3() {
  List<int> list = <int>[];
  if (list.remove('1')) print('someFunction3'); // LINT
}

错误

dart
void someFunction8() {
  List<DerivedClass2> list = <DerivedClass2>[];
  DerivedClass3 instance;
  if (list.remove(instance)) print('someFunction8'); // LINT
}

错误

dart
abstract class SomeList<E> implements List<E> {}

abstract class MyClass implements SomeList<int> {
  bool badMethod(String thing) => this.remove(thing); // LINT
}

正确

dart
void someFunction10() {
  var list = [];
  if (list.remove(1)) print('someFunction10'); // OK
}

正确

dart
void someFunction1() {
  var list = <int>[];
  if (list.remove(1)) print('someFunction1'); // OK
}

正确

dart
void someFunction4() {
  List<int> list = <int>[];
  if (list.remove(1)) print('someFunction4'); // OK
}

正确

dart
void someFunction5() {
  List<ClassBase> list = <ClassBase>[];
  DerivedClass1 instance;
  if (list.remove(instance)) print('someFunction5'); // OK
}

abstract class ClassBase {}

class DerivedClass1 extends ClassBase {}

正确

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 {}

正确

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