diff --git a/analysis/opequals_without_tohash.d b/analysis/opequals_without_tohash.d index 6561e00..aaed048 100644 --- a/analysis/opequals_without_tohash.d +++ b/analysis/opequals_without_tohash.d @@ -40,6 +40,7 @@ class OpEqualsWithoutToHashCheck : BaseAnalyzer { bool hasOpEquals = false; bool hasToHash = false; + bool hasOpCmp = false; // Just return if missing children if (!structBody @@ -61,20 +62,28 @@ class OpEqualsWithoutToHashCheck : BaseAnalyzer hasOpEquals = true; else if (methodName == "toHash") hasToHash = true; + else if (methodName == "opCmp") + hasOpCmp = true; } // Warn if has opEquals, but not toHash if (hasOpEquals && !hasToHash) { - string message = "Has method 'opEquals', but not 'toHash'."; + string message = "'" ~ name.text ~ "' has method 'opEquals', but not 'toHash'."; addErrorMessage(name.line, name.column, message); } // Warn if has toHash, but not opEquals else if (!hasOpEquals && hasToHash) { - string message = "Has method 'toHash', but not 'opEquals'."; + string message = "'" ~ name.text ~ "' has method 'toHash', but not 'opEquals'."; addErrorMessage(name.line, name.column, message); } + + if (hasOpCmp && !hasOpEquals) + { + addErrorMessage(name.line, name.column, + "'" ~ name.text ~ "' has method 'opCmp', but not 'opEquals'."); + } } } @@ -99,7 +108,7 @@ unittest } // Fail on class opEquals - class Rabbit // [warn]: Has method 'opEquals', but not 'toHash'. + class Rabbit // [warn]: 'Rabbit' has method 'opEquals', but not 'toHash'. { const bool opEquals(Object a, Object b) { @@ -108,7 +117,7 @@ unittest } // Fail on class toHash - class Kangaroo // [warn]: Has method 'toHash', but not 'opEquals'. + class Kangaroo // [warn]: 'Kangaroo' has method 'toHash', but not 'opEquals'. { override const hash_t toHash() { @@ -117,7 +126,7 @@ unittest } // Fail on struct opEquals - struct Tarantula // [warn]: Has method 'opEquals', but not 'toHash'. + struct Tarantula // [warn]: 'Tarantula' has method 'opEquals', but not 'toHash'. { const bool opEquals(Object a, Object b) { @@ -126,7 +135,7 @@ unittest } // Fail on struct toHash - struct Puma // [warn]: Has method 'toHash', but not 'opEquals'. + struct Puma // [warn]: 'Puma' has method 'toHash', but not 'opEquals'. { const nothrow @safe hash_t toHash() {