Updated checker to check for opEquals without toHash, as well as toHash without opEquals.
This commit is contained in:
parent
2a4fcbbd9d
commit
d4a72712a9
|
@ -12,8 +12,8 @@ import analysis.base;
|
||||||
import analysis.helpers;
|
import analysis.helpers;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Checks for when a class/struct has an opEquals method without a toHash
|
* Checks for when a class/struct has the method opEquals without toHash, or
|
||||||
* method.
|
* toHash without opEquals.
|
||||||
*/
|
*/
|
||||||
class OpEqualsWithoutToHashCheck : BaseAnalyzer
|
class OpEqualsWithoutToHashCheck : BaseAnalyzer
|
||||||
{
|
{
|
||||||
|
@ -66,7 +66,13 @@ class OpEqualsWithoutToHashCheck : BaseAnalyzer
|
||||||
// Warn if has opEquals, but not toHash
|
// Warn if has opEquals, but not toHash
|
||||||
if (hasOpEquals && !hasToHash)
|
if (hasOpEquals && !hasToHash)
|
||||||
{
|
{
|
||||||
string message = "Should override toHash if it has opEquals";
|
string message = "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";
|
||||||
addErrorMessage(name.line, name.column, message);
|
addErrorMessage(name.line, name.column, message);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -89,8 +95,8 @@ unittest
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Fail on struct
|
// Fail on class opEquals
|
||||||
struct Tarantula // [warn]: Should override toHash if it has opEquals
|
class Rabbit // [warn]: Has method opEquals, but not toHash
|
||||||
{
|
{
|
||||||
const bool opEquals(Object a, Object b)
|
const bool opEquals(Object a, Object b)
|
||||||
{
|
{
|
||||||
|
@ -98,8 +104,17 @@ unittest
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Fail on class
|
// Fail on class toHash
|
||||||
class Kangaroo // [warn]: Should override toHash if it has opEquals
|
class Kangaroo // [warn]: Has method toHash, but not opEquals
|
||||||
|
{
|
||||||
|
override const hash_t toHash()
|
||||||
|
{
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Fail on struct opEquals
|
||||||
|
struct Tarantula // [warn]: Has method opEquals, but not toHash
|
||||||
{
|
{
|
||||||
const bool opEquals(Object a, Object b)
|
const bool opEquals(Object a, Object b)
|
||||||
{
|
{
|
||||||
|
@ -107,12 +122,12 @@ unittest
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Fail on class with inheritance
|
// Fail on struct toHash
|
||||||
class ChaosMonkey : Chimp // [warn]: Should override toHash if it has opEquals
|
struct Puma // [warn]: Has method toHash, but not opEquals
|
||||||
{
|
{
|
||||||
override const bool opEquals(Object a, Object b)
|
const nothrow @safe hash_t toHash()
|
||||||
{
|
{
|
||||||
return true;
|
return 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}c, analysis.run.AnalyzerCheck.opequals_tohash_check);
|
}c, analysis.run.AnalyzerCheck.opequals_tohash_check);
|
||||||
|
|
Loading…
Reference in New Issue