内容

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 文件中将 list_remove_unrelated_type 添加到 linter > rules 下。

analysis_options.yaml
yaml
linter:
  rules:
    - list_remove_unrelated_type