Skip to content

Commit

Permalink
NoLint: RAII push/pop
Browse files Browse the repository at this point in the history
  • Loading branch information
ricardaxel committed Oct 12, 2023
1 parent 701e67b commit a830b3f
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 24 deletions.
13 changes: 4 additions & 9 deletions src/dscanner/analysis/base.d
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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(
Expand Down
32 changes: 25 additions & 7 deletions src/dscanner/analysis/nolint.d
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import std.regex: regex, matchAll;
import std.string: strip;
import std.typecons;


struct NoLint
{
bool containsCheck(in string check) const
Expand All @@ -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
{
Expand All @@ -28,7 +39,7 @@ package:
disabledChecks[check]++;
}

void push(in Nullable!NoLint other)
void merge(in Nullable!NoLint other)
{
if(other.isNull)
return;
Expand All @@ -37,6 +48,7 @@ package:
this.disabledChecks[item.key] += item.value;
}

private:
void pop(in Nullable!NoLint other)
{
if(other.isNull)
Expand All @@ -51,9 +63,15 @@ package:
}
}

struct Poppable {
void delegate() onPop;

private:
int[string] disabledChecks;
~this() {
onPop();
}
}

int[string] disabledChecks;
}

struct NoLintFactory
Expand All @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -163,7 +181,7 @@ private:

auto str = primaryExpression.primary.text.strip("\"");
Nullable!NoLint currNoLint = NoLintFactory.fromString(str);
noLint.push(currNoLint);
noLint.merge(currNoLint);
}
}

Expand Down
5 changes: 2 additions & 3 deletions src/dscanner/analysis/style.d
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down
7 changes: 2 additions & 5 deletions src/dscanner/analysis/useless_initializer.d
Original file line number Diff line number Diff line change
Expand Up @@ -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) ||
Expand Down

0 comments on commit a830b3f

Please sign in to comment.