/
EmptyCaseBlockInspection.cs
66 lines (62 loc) · 2.18 KB
/
EmptyCaseBlockInspection.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
using Antlr4.Runtime.Misc;
using Rubberduck.CodeAnalysis.Inspections.Abstract;
using Rubberduck.Parsing;
using Rubberduck.Parsing.Grammar;
using Rubberduck.Parsing.VBA;
using Rubberduck.Resources.Inspections;
namespace Rubberduck.CodeAnalysis.Inspections.Concrete
{
/// <summary>
/// Identifies empty 'Case' blocks that can be safely removed.
/// </summary>
/// <why>
/// Case blocks in VBA do not "fall through"; an empty 'Case' block might be hiding a bug.
/// </why>
/// <example hasResult="true">
/// <module name="MyModule" type="Standard Module">
/// <![CDATA[
/// Public Sub DoSomething(ByVal foo As Long)
/// Select Case foo
/// Case 0 ' empty block
/// Case Is > 0
/// Debug.Print foo ' does not run if foo is 0.
/// End Select
/// End Sub
/// ]]>
/// </module>
/// </example>
/// <example hasResult="false">
/// <module name="MyModule" type="Standard Module">
/// <![CDATA[
/// Public Sub DoSomething(ByVal foo As Long)
/// Select Case foo
/// Case 0
/// '...code...
/// Case Is > 0
/// '...code...
/// End Select
/// End Sub
/// ]]>
/// </module>
/// </example>
internal sealed class EmptyCaseBlockInspection : EmptyBlockInspectionBase<VBAParser.CaseClauseContext>
{
public EmptyCaseBlockInspection(IDeclarationFinderProvider declarationFinderProvider)
: base(declarationFinderProvider)
{
ContextListener = new EmptyCaseBlockListener();
}
protected override IInspectionListener<VBAParser.CaseClauseContext> ContextListener { get; }
protected override string ResultDescription(QualifiedContext<VBAParser.CaseClauseContext> context)
{
return InspectionResults.EmptyCaseBlockInspection;
}
private class EmptyCaseBlockListener : EmptyBlockInspectionListenerBase
{
public override void EnterCaseClause([NotNull] VBAParser.CaseClauseContext context)
{
InspectBlockForExecutableStatements(context.block(), context);
}
}
}
}