fix #658 - warning about non-const should not be emitted if the methods are `@disable`
This commit is contained in:
parent
4b394c2a7d
commit
aaf06fd500
|
@ -51,13 +51,25 @@ final class ObjectConstCheck : BaseAnalyzer
|
||||||
constBlock = true;
|
constBlock = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (inAggregate && d.functionDeclaration !is null && !constColon && !constBlock
|
bool containsDisable(A)(const A[] attribs)
|
||||||
&& isInteresting(d.functionDeclaration.name.text)
|
|
||||||
&& !hasConst(d.functionDeclaration.memberFunctionAttributes))
|
|
||||||
{
|
{
|
||||||
addErrorMessage(d.functionDeclaration.name.line,
|
import std.algorithm.searching : canFind;
|
||||||
d.functionDeclaration.name.column, "dscanner.suspicious.object_const",
|
return attribs.canFind!(a => a.atAttribute !is null &&
|
||||||
"Methods 'opCmp', 'toHash', 'opEquals', 'opCast', and/or 'toString' are non-const.");
|
a.atAttribute.identifier.text == "disable");
|
||||||
|
}
|
||||||
|
|
||||||
|
if (const FunctionDeclaration fd = d.functionDeclaration)
|
||||||
|
{
|
||||||
|
const isDeclationDisabled = containsDisable(d.attributes) ||
|
||||||
|
containsDisable(fd.memberFunctionAttributes);
|
||||||
|
|
||||||
|
if (inAggregate && !constColon && !constBlock && !isDeclationDisabled
|
||||||
|
&& isInteresting(fd.name.text) && !hasConst(fd.memberFunctionAttributes))
|
||||||
|
{
|
||||||
|
addErrorMessage(d.functionDeclaration.name.line,
|
||||||
|
d.functionDeclaration.name.column, "dscanner.suspicious.object_const",
|
||||||
|
"Methods 'opCmp', 'toHash', 'opEquals', 'opCast', and/or 'toString' are non-const.");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
d.accept(this);
|
d.accept(this);
|
||||||
|
@ -130,6 +142,16 @@ unittest
|
||||||
const{ override string toString() { return "foo"; }} // ok
|
const{ override string toString() { return "foo"; }} // ok
|
||||||
}
|
}
|
||||||
|
|
||||||
|
class Rat
|
||||||
|
{
|
||||||
|
bool opEquals(Object a, Object b) @disable; // ok
|
||||||
|
}
|
||||||
|
|
||||||
|
class Ant
|
||||||
|
{
|
||||||
|
@disable bool opEquals(Object a, Object b); // ok
|
||||||
|
}
|
||||||
|
|
||||||
// Will warn, because none are const
|
// Will warn, because none are const
|
||||||
class Dog
|
class Dog
|
||||||
{
|
{
|
||||||
|
|
Loading…
Reference in New Issue