From aaf06fd5004f259f72f8a3f7f3925419c9278345 Mon Sep 17 00:00:00 2001 From: Basile Burg Date: Tue, 12 Jun 2018 11:18:55 +0200 Subject: [PATCH] fix #658 - warning about non-const should not be emitted if the methods are `@disable` --- src/dscanner/analysis/objectconst.d | 34 ++++++++++++++++++++++++----- 1 file changed, 28 insertions(+), 6 deletions(-) diff --git a/src/dscanner/analysis/objectconst.d b/src/dscanner/analysis/objectconst.d index c20bc5c..c089e76 100644 --- a/src/dscanner/analysis/objectconst.d +++ b/src/dscanner/analysis/objectconst.d @@ -51,13 +51,25 @@ final class ObjectConstCheck : BaseAnalyzer constBlock = true; } - if (inAggregate && d.functionDeclaration !is null && !constColon && !constBlock - && isInteresting(d.functionDeclaration.name.text) - && !hasConst(d.functionDeclaration.memberFunctionAttributes)) + bool containsDisable(A)(const A[] attribs) { - addErrorMessage(d.functionDeclaration.name.line, - d.functionDeclaration.name.column, "dscanner.suspicious.object_const", - "Methods 'opCmp', 'toHash', 'opEquals', 'opCast', and/or 'toString' are non-const."); + import std.algorithm.searching : canFind; + return attribs.canFind!(a => a.atAttribute !is null && + 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); @@ -130,6 +142,16 @@ unittest 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 class Dog {