Warn about opCmp without opEquals

This commit is contained in:
Hackerpilot 2014-07-23 14:13:27 -07:00
parent 4197b0c829
commit 622c5e75cb
1 changed files with 15 additions and 6 deletions

View File

@ -40,6 +40,7 @@ class OpEqualsWithoutToHashCheck : BaseAnalyzer
{ {
bool hasOpEquals = false; bool hasOpEquals = false;
bool hasToHash = false; bool hasToHash = false;
bool hasOpCmp = false;
// Just return if missing children // Just return if missing children
if (!structBody if (!structBody
@ -61,20 +62,28 @@ class OpEqualsWithoutToHashCheck : BaseAnalyzer
hasOpEquals = true; hasOpEquals = true;
else if (methodName == "toHash") else if (methodName == "toHash")
hasToHash = true; hasToHash = true;
else if (methodName == "opCmp")
hasOpCmp = true;
} }
// Warn if has opEquals, but not toHash // Warn if has opEquals, but not toHash
if (hasOpEquals && !hasToHash) 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); addErrorMessage(name.line, name.column, message);
} }
// Warn if has toHash, but not opEquals // Warn if has toHash, but not opEquals
else if (!hasOpEquals && hasToHash) 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); 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 // 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) const bool opEquals(Object a, Object b)
{ {
@ -108,7 +117,7 @@ unittest
} }
// Fail on class toHash // 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() override const hash_t toHash()
{ {
@ -117,7 +126,7 @@ unittest
} }
// Fail on struct opEquals // 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) const bool opEquals(Object a, Object b)
{ {
@ -126,7 +135,7 @@ unittest
} }
// Fail on struct toHash // 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() const nothrow @safe hash_t toHash()
{ {