Skip to content

Commit 9b81989

Browse files
committed
Fix #334
1 parent d62a572 commit 9b81989

File tree

1 file changed

+33
-28
lines changed

1 file changed

+33
-28
lines changed

src/analysis/label_var_same_name_check.d

Lines changed: 33 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ module analysis.label_var_same_name_check;
66

77
import dparse.ast;
88
import dparse.lexer;
9-
9+
import dsymbol.scope_ : Scope;
1010
import analysis.base;
1111
import analysis.helpers;
1212

@@ -20,33 +20,13 @@ class LabelVarNameCheck : BaseAnalyzer
2020
super(fileName, sc);
2121
}
2222

23-
override void visit(const Module mod)
24-
{
25-
pushScope();
26-
mod.accept(this);
27-
popScope();
28-
}
29-
30-
override void visit(const BlockStatement block)
31-
{
32-
pushScope();
33-
block.accept(this);
34-
popScope();
35-
}
36-
37-
override void visit(const StructBody structBody)
38-
{
39-
pushScope();
40-
structBody.accept(this);
41-
popScope();
42-
}
43-
44-
override void visit(const CaseStatement caseStatement)
45-
{
46-
pushScope();
47-
caseStatement.accept(this);
48-
popScope();
49-
}
23+
mixin ScopedVisit!Module;
24+
mixin ScopedVisit!BlockStatement;
25+
mixin ScopedVisit!StructBody;
26+
mixin ScopedVisit!CaseStatement;
27+
mixin ScopedVisit!ForStatement;
28+
mixin ScopedVisit!IfStatement;
29+
mixin ScopedVisit!TemplateDeclaration;
5030

5131
override void visit(const VariableDeclaration var)
5232
{
@@ -76,6 +56,16 @@ private:
7656

7757
Thing[string][] stack;
7858

59+
template ScopedVisit(NodeType)
60+
{
61+
override void visit(const NodeType n)
62+
{
63+
pushScope();
64+
n.accept(this);
65+
popScope();
66+
}
67+
}
68+
7969
void duplicateCheck(const Token name, bool fromLabel, bool isConditional)
8070
{
8171
import std.conv : to;
@@ -163,6 +153,21 @@ unittest
163153
int a = 20;
164154
int a; // [warn]: Variable "a" has the same name as a variable defined on line 28.
165155
}
156+
template T(stuff)
157+
{
158+
int b;
159+
}
160+
161+
void main(string[] args)
162+
{
163+
for (int a = 0; a < 10; a++)
164+
things(a);
165+
166+
for (int a = 0; a < 10; a++)
167+
things(a);
168+
int b;
169+
}
170+
166171
}c, sac);
167172
stderr.writeln("Unittest for LabelVarNameCheck passed.");
168173
}

0 commit comments

Comments
 (0)