NoLint: RAII push/pop

This commit is contained in:
Axel Ricard 2023-10-12 22:28:04 +02:00
parent ebed325964
commit 959e46970b
4 changed files with 33 additions and 24 deletions

View file

@ -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(

View file

@ -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);
}
}

View file

@ -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)
{

View file

@ -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) ||