diff --git a/src/dscanner/analysis/base.d b/src/dscanner/analysis/base.d index 9e9cdebb..b19a52fb 100644 --- a/src/dscanner/analysis/base.d +++ b/src/dscanner/analysis/base.d @@ -415,10 +415,8 @@ public: { if(mod.moduleDeclaration !is null) { - auto currNoLint = NoLintFactory.fromModuleDeclaration(mod.moduleDeclaration); - noLint.push(currNoLint); - scope(exit) noLint.pop(currNoLint); - mod.accept(this); + with(noLint.push(NoLintFactory.fromModuleDeclaration(mod.moduleDeclaration))) + mod.accept(this); } else mod.accept(this); @@ -431,11 +429,8 @@ public: */ override void visit(const(Declaration) decl) { - auto currNoLint = NoLintFactory.fromDeclaration(decl); - noLint.push(currNoLint); - scope(exit) noLint.pop(currNoLint); - - decl.accept(this); + with(noLint.push(NoLintFactory.fromDeclaration(decl))) + decl.accept(this); } AutoFix.CodeReplacement[] resolveAutoFix( diff --git a/src/dscanner/analysis/nolint.d b/src/dscanner/analysis/nolint.d index e3eeed75..2c3f7c8a 100644 --- a/src/dscanner/analysis/nolint.d +++ b/src/dscanner/analysis/nolint.d @@ -8,7 +8,6 @@ import std.regex: regex, matchAll; import std.string: strip; import std.typecons; - struct NoLint { bool containsCheck(in string check) const @@ -17,6 +16,18 @@ struct NoLint disabledChecks[check] > 0; } + // automatic pop when returned value goes out of scope + Poppable push(in Nullable!NoLint other) + { + if(other.isNull) + return Poppable((){}); + + foreach(item; other.get.getDisabledChecks.byKeyValue) + this.disabledChecks[item.key] += item.value; + + return Poppable(() => this.pop(other)); + } + package: const(int[string]) getDisabledChecks() const { @@ -28,7 +39,7 @@ package: disabledChecks[check]++; } - void push(in Nullable!NoLint other) + void merge(in Nullable!NoLint other) { if(other.isNull) return; @@ -37,6 +48,7 @@ package: this.disabledChecks[item.key] += item.value; } +private: void pop(in Nullable!NoLint other) { if(other.isNull) @@ -51,9 +63,15 @@ package: } } + struct Poppable { + void delegate() onPop; -private: - int[string] disabledChecks; + ~this() { + onPop(); + } + } + + int[string] disabledChecks; } struct NoLintFactory @@ -63,7 +81,7 @@ struct NoLintFactory NoLint noLint; foreach(atAttribute; moduleDeclaration.atAttributes) - noLint.push(NoLintFactory.fromAtAttribute(atAttribute)); + noLint.merge(NoLintFactory.fromAtAttribute(atAttribute)); if(!noLint.getDisabledChecks.length) return nullNoLint; @@ -75,7 +93,7 @@ struct NoLintFactory { NoLint noLint; foreach(attribute; declaration.attributes) - noLint.push(NoLintFactory.fromAttribute(attribute)); + noLint.merge(NoLintFactory.fromAttribute(attribute)); if(!noLint.getDisabledChecks.length) return nullNoLint; @@ -163,7 +181,7 @@ private: auto str = primaryExpression.primary.text.strip("\""); Nullable!NoLint currNoLint = NoLintFactory.fromString(str); - noLint.push(currNoLint); + noLint.merge(currNoLint); } } diff --git a/src/dscanner/analysis/style.d b/src/dscanner/analysis/style.d index a80ca707..37826563 100644 --- a/src/dscanner/analysis/style.d +++ b/src/dscanner/analysis/style.d @@ -34,9 +34,8 @@ final class StyleChecker : BaseAnalyzer override void visit(const ModuleDeclaration dec) { - auto currNoLint = NoLintFactory.fromModuleDeclaration(dec); - noLint.push(currNoLint); - scope(exit) noLint.pop(currNoLint); + with(noLint.push(NoLintFactory.fromModuleDeclaration(dec))) + dec.accept(this); foreach (part; dec.moduleName.identifiers) { diff --git a/src/dscanner/analysis/useless_initializer.d b/src/dscanner/analysis/useless_initializer.d index 68a4fbf2..939f4c2d 100644 --- a/src/dscanner/analysis/useless_initializer.d +++ b/src/dscanner/analysis/useless_initializer.d @@ -94,11 +94,8 @@ public: { _inStruct.insert(decl.structDeclaration !is null); - auto currNoLint = NoLintFactory.fromDeclaration(decl); - noLint.push(currNoLint); - scope(exit) noLint.pop(currNoLint); - - decl.accept(this); + with(noLint.push(NoLintFactory.fromDeclaration(decl))) + decl.accept(this); if (_inStruct.length > 1 && _inStruct[$-2] && decl.constructor && ((decl.constructor.parameters && decl.constructor.parameters.parameters.length == 0) ||