Skip to content

Commit 8ec3ddf

Browse files
authored
Merge pull request #4417 from Hosch250/issue4166
On local error inspection
2 parents b519bda + 7913c27 commit 8ec3ddf

17 files changed

+295
-3
lines changed
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
using System.Collections.Generic;
2+
using System.Linq;
3+
using Antlr4.Runtime;
4+
using Rubberduck.Inspections.Abstract;
5+
using Rubberduck.Inspections.Results;
6+
using Rubberduck.Parsing;
7+
using Rubberduck.Parsing.Grammar;
8+
using Rubberduck.Parsing.Inspections.Abstract;
9+
using Rubberduck.Resources.Inspections;
10+
using Rubberduck.Parsing.VBA;
11+
using Rubberduck.VBEditor;
12+
using Antlr4.Runtime.Misc;
13+
14+
namespace Rubberduck.Inspections.Concrete
15+
{
16+
public sealed class OnLocalErrorInspection : ParseTreeInspectionBase
17+
{
18+
public OnLocalErrorInspection(RubberduckParserState state)
19+
: base(state) { }
20+
21+
public override IInspectionListener Listener { get; } =
22+
new OnLocalErrorListener();
23+
24+
protected override IEnumerable<IInspectionResult> DoGetInspectionResults()
25+
{
26+
return Listener.Contexts
27+
.Where(result => !IsIgnoringInspectionResultFor(result.ModuleName, result.Context.Start.Line))
28+
.Select(result => new QualifiedContextInspectionResult(this,
29+
InspectionResults.OnLocalErrorInspection,
30+
result));
31+
}
32+
33+
public class OnLocalErrorListener : VBAParserBaseListener, IInspectionListener
34+
{
35+
private readonly List<QualifiedContext<ParserRuleContext>> _contexts = new List<QualifiedContext<ParserRuleContext>>();
36+
public IReadOnlyList<QualifiedContext<ParserRuleContext>> Contexts => _contexts;
37+
38+
public QualifiedModuleName CurrentModuleName { get; set; }
39+
40+
public void ClearContexts()
41+
{
42+
_contexts.Clear();
43+
}
44+
45+
public override void ExitOnErrorStmt([NotNull] VBAParser.OnErrorStmtContext context)
46+
{
47+
if (context.ON_LOCAL_ERROR() != null)
48+
{
49+
_contexts.Add(new QualifiedContext<ParserRuleContext>(CurrentModuleName, context));
50+
}
51+
}
52+
}
53+
}
54+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
using Rubberduck.Inspections.Abstract;
2+
using Rubberduck.Inspections.Concrete;
3+
using Rubberduck.Parsing.Grammar;
4+
using Rubberduck.Parsing.Inspections.Abstract;
5+
using Rubberduck.Parsing.VBA;
6+
7+
namespace Rubberduck.Inspections.QuickFixes
8+
{
9+
public sealed class RemoveLocalErrorQuickFix : QuickFixBase
10+
{
11+
private readonly RubberduckParserState _state;
12+
13+
public RemoveLocalErrorQuickFix(RubberduckParserState state)
14+
: base(typeof(OnLocalErrorInspection))
15+
{
16+
_state = state;
17+
}
18+
19+
public override void Fix(IInspectionResult result)
20+
{
21+
var errorStmt = (VBAParser.OnErrorStmtContext)result.Context;
22+
23+
var rewriter = _state.GetRewriter(result.QualifiedSelection.QualifiedName);
24+
rewriter.Replace(errorStmt.ON_LOCAL_ERROR(), Tokens.On + " " + Tokens.Error);
25+
}
26+
27+
public override string Description(IInspectionResult result) => Resources.Inspections.QuickFixes.RemoveLocalErrorQuickFix;
28+
29+
public override bool CanFixInProcedure => true;
30+
public override bool CanFixInModule => true;
31+
public override bool CanFixInProject => true;
32+
}
33+
}

Rubberduck.CodeAnalysis/Rubberduck.CodeAnalysis.csproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@
8181
<Compile Include="Inspections\Concrete\EmptyForEachBlockInspection.cs" />
8282
<Compile Include="Inspections\Concrete\EmptyForLoopBlockInspection.cs" />
8383
<Compile Include="Inspections\Concrete\BooleanAssignedInIfElseInspection.cs" />
84+
<Compile Include="Inspections\Concrete\OnLocalErrorInspection.cs" />
8485
<Compile Include="Inspections\Concrete\ModuleWithoutFolderInspection.cs" />
8586
<Compile Include="Inspections\Concrete\EmptyWhileWendBlockInspection.cs" />
8687
<Compile Include="Inspections\Concrete\ObsoleteCallingConventionInspection.cs" />
@@ -157,6 +158,7 @@
157158
<Compile Include="Inspections\Concrete\ProcedureNotUsedInspection.cs" />
158159
<Compile Include="Properties\AssemblyInfo.cs" />
159160
<Compile Include="QuickFixes\AccessSheetUsingCodeNameQuickFix.cs" />
161+
<Compile Include="QuickFixes\RemoveLocalErrorQuickFix.cs" />
160162
<Compile Include="QuickFixes\RemoveDuplicatedAnnotationQuickFix.cs" />
161163
<Compile Include="QuickFixes\ReplaceIfElseWithConditionalStatementQuickFix.cs" />
162164
<Compile Include="QuickFixes\AddIdentifierToWhiteListQuickFix.cs" />

Rubberduck.Core/Properties/Settings.Designer.cs

Lines changed: 4 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Rubberduck.Core/Properties/Settings.settings

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -280,6 +280,7 @@
280280
&lt;CodeInspection Name="ObsoleteCallingConventionInspection" Severity="Warning" InspectionType="CodeQualityIssues" /&gt;
281281
&lt;CodeInspection Name="DuplicatedAnnotationInspection" Severity="Error" InspectionType="RubberduckOpportunities" /&gt;
282282
&lt;CodeInspection Name="ModuleWithoutFolderInspection" Severity="Suggestion" InspectionType="RubberduckOpportunities" /&gt;
283+
&lt;CodeInspection Name="OnLocalErrorInspection" Severity="Suggestion" InspectionType="LanguageOpportunities" /&gt;
283284
&lt;/CodeInspections&gt;
284285
&lt;WhitelistedIdentifiers /&gt;
285286
&lt;RunInspectionsOnSuccessfulParse&gt;true&lt;/RunInspectionsOnSuccessfulParse&gt;

Rubberduck.Core/app.config

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -404,6 +404,8 @@
404404
InspectionType="RubberduckOpportunities" />
405405
<CodeInspection Name="ModuleWithoutFolderInspection" Severity="Suggestion"
406406
InspectionType="RubberduckOpportunities" />
407+
<CodeInspection Name="OnLocalErrorInspection" Severity="Suggestion"
408+
InspectionType="LanguageOpportunities" />
407409
</CodeInspections>
408410
<WhitelistedIdentifiers />
409411
<RunInspectionsOnSuccessfulParse>true</RunInspectionsOnSuccessfulParse>

Rubberduck.Resources/Inspections/InspectionInfo.Designer.cs

Lines changed: 9 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Rubberduck.Resources/Inspections/InspectionInfo.resx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -337,4 +337,7 @@ If the parameter can be null, ignore this inspection result; passing a null valu
337337
<data name="ModuleWithoutFolderInspection" xml:space="preserve">
338338
<value>Modules without the '@Folder' annotation cannot receive custom groupings in the Code Explorer. </value>
339339
</data>
340+
<data name="OnLocalErrorInspection" xml:space="preserve">
341+
<value>On Local Error exists only for compatibility with previous versions of Visual Basic, and all Errors are treated as Local regardless of the Error statement. Use of this keyword inaccurately gives the impression that there is a distinction between types of error handling when there is not.</value>
342+
</data>
340343
</root>

Rubberduck.Resources/Inspections/InspectionNames.Designer.cs

Lines changed: 9 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Rubberduck.Resources/Inspections/InspectionNames.resx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -336,4 +336,7 @@
336336
<data name="ModuleWithoutFolderInspection" xml:space="preserve">
337337
<value>Module without '@Folder' annotation</value>
338338
</data>
339+
<data name="OnLocalErrorInspection" xml:space="preserve">
340+
<value>On Local Error statement</value>
341+
</data>
339342
</root>

0 commit comments

Comments
 (0)