Modify rule S1206: "equals(Object obj)" and "hashCode()" should be overridden in pairs (hash_and_equals)
This commit is contained in:
parent
81a624d470
commit
abcf7a1edf
@ -1,15 +1,23 @@
|
||||
== Why is this an issue?
|
||||
|
||||
In Dart, you must override either both or neither of the operator `==` and `hashCode()` method in order to keep the contract between the two:
|
||||
In Dart, you must override either both or neither of the operator `==` and the `hashCode()` method.
|
||||
|
||||
- Whenever the `hashCode` method is invoked on the same object more than once, it must consistently return the same integer, provided no information used in `==` comparisons on the object is modified.
|
||||
- If two objects are equal according to the `==` operator, then calling the `hashCode` method on each of the two objects must produce the same integer result.
|
||||
That is required to keep the contract between the two:
|
||||
|
||||
* whenever the `hashCode` method is invoked on the same object more than once, it must consistently return the same integer, provided no information used in `==` comparisons on the object is modified
|
||||
* if two objects are equal according to the `==` operator, then calling the `hashCode` method on each of the two objects must produce the same integer result
|
||||
|
||||
By overriding only one of the two methods with a non-trivial implementation, this contract is almost certainly broken.
|
||||
|
||||
=== Exceptions
|
||||
|
||||
The rule does not raise in mixins, because they are not supposed to be instantiated directly, but rather included in other classes.
|
||||
|
||||
The rule raises, however, on mixin classes, since they can also be used as normal classes, and instantiated directly.
|
||||
|
||||
=== Noncompliant code example
|
||||
|
||||
[source,dart]
|
||||
[source,dart,diff-id=1,diff-type=noncompliant]
|
||||
----
|
||||
class A {
|
||||
final int value;
|
||||
@ -22,7 +30,7 @@ class A {
|
||||
|
||||
=== Compliant solution
|
||||
|
||||
[source,dart]
|
||||
[source,dart,diff-id=1,diff-type=compliant]
|
||||
----
|
||||
class A {
|
||||
final int value;
|
||||
@ -38,4 +46,28 @@ class A {
|
||||
|
||||
== Resources
|
||||
|
||||
* https://dart.dev/tools/linter-rules/unnecessary_overrides[Dart Lint rule]
|
||||
* Dart Docs - https://dart.dev/tools/linter-rules/hash_and_equals[Dart Linter rule - hash_and_equals]
|
||||
* Dart Docs - https://dart.dev/language/mixins[Language - Classes & objects - Mixins]
|
||||
|
||||
ifdef::env-github,rspecator-view[]
|
||||
|
||||
'''
|
||||
== Implementation Specification
|
||||
(visible only on this page)
|
||||
|
||||
=== Message
|
||||
|
||||
* Missing a corresponding override of 'hashCode'.
|
||||
* Missing a corresponding override of '=='.
|
||||
|
||||
=== Highlighting
|
||||
|
||||
The identifier of the overridden method: either `==` or `hashCode`.
|
||||
|
||||
'''
|
||||
== Comments And Links
|
||||
(visible only on this page)
|
||||
|
||||
include::../comments-and-links.adoc[]
|
||||
|
||||
endif::env-github,rspecator-view[]
|
||||
|
Loading…
x
Reference in New Issue
Block a user