|
1 | 1 | using System.Linq;
|
| 2 | +using Antlr4.Runtime.Tree; |
2 | 3 | using Rubberduck.CodeAnalysis.Inspections.Abstract;
|
3 | 4 | using Rubberduck.Parsing;
|
4 | 5 | using Rubberduck.Parsing.Grammar;
|
@@ -122,48 +123,58 @@ protected override string ResultDescription(Declaration declaration)
|
122 | 123 | private class FunctionReturnValueAssignmentLocator : VBAParserBaseVisitor<bool>
|
123 | 124 | {
|
124 | 125 | private readonly string _name;
|
125 |
| - private bool _result; |
126 | 126 | private bool _inFunctionReturnWithExpression;
|
127 | 127 |
|
128 | 128 | public FunctionReturnValueAssignmentLocator(string name)
|
129 | 129 | {
|
130 | 130 | _name = name;
|
| 131 | + _inFunctionReturnWithExpression = false; |
131 | 132 | }
|
132 | 133 |
|
133 |
| - public override bool VisitBlock(VBAParser.BlockContext context) |
| 134 | + protected override bool DefaultResult => false; |
| 135 | + |
| 136 | + protected override bool ShouldVisitNextChild(IRuleNode node, bool currentResult) |
134 | 137 | {
|
135 |
| - base.VisitBlock(context); |
136 |
| - return _result; |
| 138 | + return !currentResult; |
137 | 139 | }
|
| 140 | + |
| 141 | + //This is actually the default implementation, but for explicities sake stated here. |
| 142 | + protected override bool AggregateResult(bool aggregate, bool nextResult) |
| 143 | + { |
| 144 | + return nextResult; |
| 145 | + } |
| 146 | + |
138 | 147 | public override bool VisitWithStmt(VBAParser.WithStmtContext context)
|
139 | 148 | {
|
140 | 149 | var oldInFunctionReturnWithExpression = _inFunctionReturnWithExpression;
|
141 | 150 | _inFunctionReturnWithExpression = context.expression().GetText() == _name;
|
142 |
| - base.VisitWithStmt(context); |
| 151 | + var result = base.VisitWithStmt(context); |
143 | 152 | _inFunctionReturnWithExpression = oldInFunctionReturnWithExpression;
|
144 |
| - return _result; |
| 153 | + return result; |
145 | 154 | }
|
146 | 155 |
|
147 | 156 | public override bool VisitLetStmt(VBAParser.LetStmtContext context)
|
148 | 157 | {
|
149 | 158 | var LHS = context.lExpression();
|
| 159 | + if (_inFunctionReturnWithExpression |
| 160 | + && LHS is VBAParser.WithMemberAccessExprContext) |
| 161 | + { |
| 162 | + return true; |
| 163 | + } |
150 | 164 | var leftmost = LHS.GetChild(0).GetText();
|
151 |
| - _result = _result |
152 |
| - || leftmost == _name |
153 |
| - || (_inFunctionReturnWithExpression |
154 |
| - && LHS is VBAParser.WithMemberAccessExprContext); |
155 |
| - return _result; |
| 165 | + return leftmost == _name; |
156 | 166 | }
|
157 | 167 |
|
158 | 168 | public override bool VisitSetStmt(VBAParser.SetStmtContext context)
|
159 | 169 | {
|
160 | 170 | var LHS = context.lExpression();
|
| 171 | + if (_inFunctionReturnWithExpression |
| 172 | + && LHS is VBAParser.WithMemberAccessExprContext) |
| 173 | + { |
| 174 | + return true; |
| 175 | + } |
161 | 176 | var leftmost = LHS.GetChild(0).GetText();
|
162 |
| - _result = _result |
163 |
| - || leftmost == _name |
164 |
| - || (_inFunctionReturnWithExpression |
165 |
| - && LHS is VBAParser.WithMemberAccessExprContext); |
166 |
| - return _result; |
| 177 | + return leftmost == _name; |
167 | 178 | }
|
168 | 179 | }
|
169 | 180 | }
|
|
0 commit comments