メインコンテンツにスキップ

test_types_in_equals

安定版

operator ==(Object other) の引数の型をテストします。

詳細

#

推奨 operator ==(Object other) の引数の型をテストします。

型のテストを行わないと、クラスの利用者にとって予期せぬ実行時型エラーが発生する可能性があります。

悪い例

dart
class Field {
}

class Bad {
  final Field someField;

  Bad(this.someField);

  @override
  bool operator ==(Object other) {
    Bad otherBad = other as Bad; // LINT
    bool areEqual = otherBad != null && otherBad.someField == someField;
    return areEqual;
  }

  @override
  int get hashCode {
    return someField.hashCode;
  }
}

良い例

dart
class Field {
}

class Good {
  final Field someField;

  Good(this.someField);

  @override
  bool operator ==(Object other) {
    if (identical(this, other)) {
      return true;
    }
    return other is Good &&
        this.someField == other.someField;
  }

  @override
  int get hashCode {
    return someField.hashCode;
  }
}

有効にする

#

test_types_in_equals ルールを有効にするには、analysis_options.yaml ファイルの linter > rules の下に test_types_in_equals を追加してください。

analysis_options.yaml
yaml
linter:
  rules:
    - test_types_in_equals

代わりに YAML マップ構文を使用して linter ルールを設定している場合は、linter > rules の下に test_types_in_equals: true を追加してください。

analysis_options.yaml
yaml
linter:
  rules:
    test_types_in_equals: true