Skip to content

[Feature request] Safe guard when iterate over properties. #60695

Open
@chunhtai

Description

@chunhtai

I would like to have a better warning when writing method that needs to account for all properties.
For example when a class overrides operator== and hashCode of a class.

class A {
  int? a;
  int? b;

  void printAll() {
    if (a != null) {
      print(a);
    }
    if (b != null) {
      print(b);
    }
  }

  @override
  bool operator ==(Object other) {
    return other is A && other.a == a && other.b == b;
  }

    @override
  int get hashCode => Object.hashAll([a, b]);

}

it is very easy to forgot to update these method when adding new propperty which cause obscure bugs.

class A {
  int? a;
  int? b;
  int? c;

  void printAll() {
    if (a != null) {
      print(a);
    }
    if (b!= null) {
      print(b);
    }
    // may forgot to add
    if (c!= null) {
      print(c);
    }
  }

  @override
  bool operator ==(Object other) {
    return other is A && other.a == a && other.b == b
     // forgot 
     && other.c ==c ;
  }

    @override
  int get hashCode => Object.hashAll([a, b, /*forgot*/ c]);

}

I hope we could have a way to get warming about cases like this.

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-languageDart language related items (some items might be better tracked at github.com/dart-lang/language).

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions