Skip to content

Commit a830b3f

Browse files
committed
NoLint: RAII push/pop
1 parent 701e67b commit a830b3f

File tree

4 files changed

+33
-24
lines changed

4 files changed

+33
-24
lines changed

src/dscanner/analysis/base.d

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -415,10 +415,8 @@ public:
415415
{
416416
if(mod.moduleDeclaration !is null)
417417
{
418-
auto currNoLint = NoLintFactory.fromModuleDeclaration(mod.moduleDeclaration);
419-
noLint.push(currNoLint);
420-
scope(exit) noLint.pop(currNoLint);
421-
mod.accept(this);
418+
with(noLint.push(NoLintFactory.fromModuleDeclaration(mod.moduleDeclaration)))
419+
mod.accept(this);
422420
}
423421
else
424422
mod.accept(this);
@@ -431,11 +429,8 @@ public:
431429
*/
432430
override void visit(const(Declaration) decl)
433431
{
434-
auto currNoLint = NoLintFactory.fromDeclaration(decl);
435-
noLint.push(currNoLint);
436-
scope(exit) noLint.pop(currNoLint);
437-
438-
decl.accept(this);
432+
with(noLint.push(NoLintFactory.fromDeclaration(decl)))
433+
decl.accept(this);
439434
}
440435

441436
AutoFix.CodeReplacement[] resolveAutoFix(

src/dscanner/analysis/nolint.d

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import std.regex: regex, matchAll;
88
import std.string: strip;
99
import std.typecons;
1010

11-
1211
struct NoLint
1312
{
1413
bool containsCheck(in string check) const
@@ -17,6 +16,18 @@ struct NoLint
1716
disabledChecks[check] > 0;
1817
}
1918

19+
// automatic pop when returned value goes out of scope
20+
Poppable push(in Nullable!NoLint other)
21+
{
22+
if(other.isNull)
23+
return Poppable((){});
24+
25+
foreach(item; other.get.getDisabledChecks.byKeyValue)
26+
this.disabledChecks[item.key] += item.value;
27+
28+
return Poppable(() => this.pop(other));
29+
}
30+
2031
package:
2132
const(int[string]) getDisabledChecks() const
2233
{
@@ -28,7 +39,7 @@ package:
2839
disabledChecks[check]++;
2940
}
3041

31-
void push(in Nullable!NoLint other)
42+
void merge(in Nullable!NoLint other)
3243
{
3344
if(other.isNull)
3445
return;
@@ -37,6 +48,7 @@ package:
3748
this.disabledChecks[item.key] += item.value;
3849
}
3950

51+
private:
4052
void pop(in Nullable!NoLint other)
4153
{
4254
if(other.isNull)
@@ -51,9 +63,15 @@ package:
5163
}
5264
}
5365

66+
struct Poppable {
67+
void delegate() onPop;
5468

55-
private:
56-
int[string] disabledChecks;
69+
~this() {
70+
onPop();
71+
}
72+
}
73+
74+
int[string] disabledChecks;
5775
}
5876

5977
struct NoLintFactory
@@ -63,7 +81,7 @@ struct NoLintFactory
6381
NoLint noLint;
6482

6583
foreach(atAttribute; moduleDeclaration.atAttributes)
66-
noLint.push(NoLintFactory.fromAtAttribute(atAttribute));
84+
noLint.merge(NoLintFactory.fromAtAttribute(atAttribute));
6785

6886
if(!noLint.getDisabledChecks.length)
6987
return nullNoLint;
@@ -75,7 +93,7 @@ struct NoLintFactory
7593
{
7694
NoLint noLint;
7795
foreach(attribute; declaration.attributes)
78-
noLint.push(NoLintFactory.fromAttribute(attribute));
96+
noLint.merge(NoLintFactory.fromAttribute(attribute));
7997

8098
if(!noLint.getDisabledChecks.length)
8199
return nullNoLint;
@@ -163,7 +181,7 @@ private:
163181

164182
auto str = primaryExpression.primary.text.strip("\"");
165183
Nullable!NoLint currNoLint = NoLintFactory.fromString(str);
166-
noLint.push(currNoLint);
184+
noLint.merge(currNoLint);
167185
}
168186
}
169187

src/dscanner/analysis/style.d

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,8 @@ final class StyleChecker : BaseAnalyzer
3434

3535
override void visit(const ModuleDeclaration dec)
3636
{
37-
auto currNoLint = NoLintFactory.fromModuleDeclaration(dec);
38-
noLint.push(currNoLint);
39-
scope(exit) noLint.pop(currNoLint);
37+
with(noLint.push(NoLintFactory.fromModuleDeclaration(dec)))
38+
dec.accept(this);
4039

4140
foreach (part; dec.moduleName.identifiers)
4241
{

src/dscanner/analysis/useless_initializer.d

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -94,11 +94,8 @@ public:
9494
{
9595
_inStruct.insert(decl.structDeclaration !is null);
9696

97-
auto currNoLint = NoLintFactory.fromDeclaration(decl);
98-
noLint.push(currNoLint);
99-
scope(exit) noLint.pop(currNoLint);
100-
101-
decl.accept(this);
97+
with(noLint.push(NoLintFactory.fromDeclaration(decl)))
98+
decl.accept(this);
10299

103100
if (_inStruct.length > 1 && _inStruct[$-2] && decl.constructor &&
104101
((decl.constructor.parameters && decl.constructor.parameters.parameters.length == 0) ||

0 commit comments

Comments
 (0)