/
EmptyStringLiteralInspection.cs
71 lines (67 loc) · 2.78 KB
/
EmptyStringLiteralInspection.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
67
68
69
70
71
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>
/// Flags uses of an empty string literal ("").
/// </summary>
/// <remarks>
/// Treating an empty string literal as equal to the 'vbNullString' constant
/// requires using the PermissiveAssertClass. The default AssertClass is more strict about data types, and tells them apart.
/// </remarks>
/// <why>
/// Standard library constant 'vbNullString' is more explicit about its intent, and should be preferred to a string literal.
/// While the memory gain is meaningless, an empty string literal still takes up 2 bytes of memory,
/// but 'vbNullString' is a null string pointer, and doesn't. In VB6 and VBA this makes little to no difference however,
/// but in earlier versions each instance of an empty string literal in source code resulted in the allocation of these 2 bytes every time.
/// </why>
/// <example hasResult="true">
/// <module name="MyModule" type="Standard Module">
/// <![CDATA[
/// Public Sub DoSomething(ByVal foo As String)
/// If foo = "" Then
/// ' ...
/// End If
/// End Sub
/// ]]>
/// </module>
/// </example>
/// <example hasResult="false">
/// <module name="MyModule" type="Standard Module">
/// <![CDATA[
/// Public Sub DoSomething(ByVal foo As String)
/// If foo = vbNullString Then
/// ' ...
/// End If
/// End Sub
/// ]]>
/// </module>
/// </example>
internal sealed class EmptyStringLiteralInspection : ParseTreeInspectionBase<VBAParser.LiteralExpressionContext>
{
public EmptyStringLiteralInspection(IDeclarationFinderProvider declarationFinderProvider)
: base(declarationFinderProvider)
{
ContextListener = new EmptyStringLiteralListener();
}
protected override IInspectionListener<VBAParser.LiteralExpressionContext> ContextListener { get; }
protected override string ResultDescription(QualifiedContext<VBAParser.LiteralExpressionContext> context)
{
return InspectionResults.EmptyStringLiteralInspection;
}
private class EmptyStringLiteralListener : InspectionListenerBase<VBAParser.LiteralExpressionContext>
{
public override void ExitLiteralExpression(VBAParser.LiteralExpressionContext context)
{
var literal = context.STRINGLITERAL();
if (literal != null && literal.GetText() == "\"\"")
{
SaveContext(context);
}
}
}
}
}