Skip to content

Commit 2ab49f2

Browse files
authored
Merge pull request #5345 from IvenBach/Issue5314_implicitlytyped_const_inspection
Adds ImplicitlyTypedConstInspection and tests
2 parents 2b341dd + 7d977d5 commit 2ab49f2

File tree

13 files changed

+240
-41
lines changed

13 files changed

+240
-41
lines changed

Rubberduck.CodeAnalysis/Inspections/Abstract/QuickFixBase.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
using Rubberduck.JunkDrawer.Extensions;
77
using Rubberduck.Parsing.Inspections.Abstract;
88
using Rubberduck.Parsing.Rewriter;
9-
using Rubberduck.Parsing.VBA.Extensions;
109
using Rubberduck.Parsing.VBA.Parsing;
1110

1211
namespace Rubberduck.Inspections.Abstract
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
using System.Collections.Generic;
2+
using System.Linq;
3+
using Rubberduck.Inspections.Abstract;
4+
using Rubberduck.Parsing.VBA;
5+
using Rubberduck.Parsing.Inspections.Abstract;
6+
using Rubberduck.Inspections.Results;
7+
using Rubberduck.Parsing.Symbols;
8+
using Rubberduck.Resources.Inspections;
9+
10+
namespace Rubberduck.CodeAnalysis.Inspections.Concrete
11+
{
12+
public sealed class ImplicitlyTypedConstInspection : InspectionBase
13+
{
14+
public ImplicitlyTypedConstInspection(RubberduckParserState state)
15+
: base(state) { }
16+
17+
protected override IEnumerable<IInspectionResult> DoGetInspectionResults()
18+
{
19+
var declarationFinder = State.DeclarationFinder;
20+
21+
var implicitlyTypedConsts = declarationFinder.UserDeclarations(DeclarationType.Constant)
22+
.Where(declaration => !declaration.IsTypeSpecified);
23+
24+
return implicitlyTypedConsts.Select(Result);
25+
}
26+
27+
private IInspectionResult Result(Declaration declaration)
28+
{
29+
var description = string.Format(InspectionResults.ImplicitlyTypedConstInspection, declaration.IdentifierName);
30+
31+
return new DeclarationInspectionResult(
32+
this,
33+
description,
34+
declaration);
35+
}
36+
}
37+
}

Rubberduck.Parsing/Grammar/PartialExtensions/VBAParserPartialExtensions.cs

Lines changed: 16 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,7 @@ public Interval IdentifierTokens
4747
{
4848
get
4949
{
50-
Interval tokenInterval;
51-
Identifier.GetName(this, out tokenInterval);
50+
Identifier.GetName(this, out Interval tokenInterval);
5251
return tokenInterval;
5352
}
5453
}
@@ -84,8 +83,7 @@ public Interval IdentifierTokens
8483
{
8584
get
8685
{
87-
Interval tokenInterval;
88-
Identifier.GetName(this, out tokenInterval);
86+
Identifier.GetName(this, out Interval tokenInterval);
8987
return tokenInterval;
9088
}
9189
}
@@ -122,8 +120,7 @@ public Interval IdentifierTokens
122120
{
123121
get
124122
{
125-
Interval tokenInterval;
126-
Identifier.GetName(this, out tokenInterval);
123+
Identifier.GetName(this, out Interval tokenInterval);
127124
return tokenInterval;
128125
}
129126
}
@@ -160,8 +157,7 @@ public Interval IdentifierTokens
160157
{
161158
get
162159
{
163-
Interval tokenInterval;
164-
Identifier.GetName(this, out tokenInterval);
160+
Identifier.GetName(this, out Interval tokenInterval);
165161
return tokenInterval;
166162
}
167163
}
@@ -198,8 +194,7 @@ public Interval IdentifierTokens
198194
{
199195
get
200196
{
201-
Interval tokenInterval;
202-
Identifier.GetName(this, out tokenInterval);
197+
Identifier.GetName(this, out Interval tokenInterval);
203198
return tokenInterval;
204199
}
205200
}
@@ -213,8 +208,7 @@ public Interval IdentifierTokens
213208
{
214209
get
215210
{
216-
Interval tokenInterval;
217-
Identifier.GetName(this, out tokenInterval);
211+
Identifier.GetName(this, out Interval tokenInterval);
218212
return tokenInterval;
219213
}
220214
}
@@ -249,8 +243,7 @@ public Interval IdentifierTokens
249243
{
250244
get
251245
{
252-
Interval tokenInterval;
253-
Identifier.GetName(this, out tokenInterval);
246+
Identifier.GetName(this, out Interval tokenInterval);
254247
return tokenInterval;
255248
}
256249
}
@@ -285,8 +278,7 @@ public Interval IdentifierTokens
285278
{
286279
get
287280
{
288-
Interval tokenInterval;
289-
Identifier.GetName(this, out tokenInterval);
281+
Identifier.GetName(this, out Interval tokenInterval);
290282
return tokenInterval;
291283
}
292284
}
@@ -321,8 +313,7 @@ public Interval IdentifierTokens
321313
{
322314
get
323315
{
324-
Interval tokenInterval;
325-
Identifier.GetName(this, out tokenInterval);
316+
Identifier.GetName(this, out Interval tokenInterval);
326317
return tokenInterval;
327318
}
328319
}
@@ -357,8 +348,7 @@ public Interval IdentifierTokens
357348
{
358349
get
359350
{
360-
Interval tokenInterval;
361-
Identifier.GetName(this, out tokenInterval);
351+
Identifier.GetName(this, out Interval tokenInterval);
362352
return tokenInterval;
363353
}
364354
}
@@ -393,8 +383,7 @@ public Interval IdentifierTokens
393383
{
394384
get
395385
{
396-
Interval tokenInterval;
397-
Identifier.GetName(this, out tokenInterval);
386+
Identifier.GetName(this, out Interval tokenInterval);
398387
return tokenInterval;
399388
}
400389
}
@@ -429,8 +418,7 @@ public Interval IdentifierTokens
429418
{
430419
get
431420
{
432-
Interval tokenInterval;
433-
Identifier.GetName(this, out tokenInterval);
421+
Identifier.GetName(this, out Interval tokenInterval);
434422
return tokenInterval;
435423
}
436424
}
@@ -465,8 +453,7 @@ public Interval IdentifierTokens
465453
{
466454
get
467455
{
468-
Interval tokenInterval;
469-
Identifier.GetName(this, out tokenInterval);
456+
Identifier.GetName(this, out Interval tokenInterval);
470457
return tokenInterval;
471458
}
472459
}
@@ -501,8 +488,7 @@ public Interval IdentifierTokens
501488
{
502489
get
503490
{
504-
Interval tokenInterval;
505-
Identifier.GetName(this, out tokenInterval);
491+
Identifier.GetName(this, out Interval tokenInterval);
506492
return tokenInterval;
507493
}
508494
}
@@ -537,8 +523,7 @@ public Interval IdentifierTokens
537523
{
538524
get
539525
{
540-
Interval tokenInterval;
541-
Identifier.GetName(this, out tokenInterval);
526+
Identifier.GetName(this, out Interval tokenInterval);
542527
return tokenInterval;
543528
}
544529
}
@@ -573,8 +558,7 @@ public Interval IdentifierTokens
573558
{
574559
get
575560
{
576-
Interval tokenInterval;
577-
Identifier.GetName(this, out tokenInterval);
561+
Identifier.GetName(this, out Interval tokenInterval);
578562
return tokenInterval;
579563
}
580564
}

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
@@ -433,4 +433,7 @@ If the parameter can be null, ignore this inspection result; passing a null valu
433433
<data name="SuspiciousLetAssignmentInspection" xml:space="preserve">
434434
<value>Whenever both sides of an assignment without Set are objects, there is an assignment from the default member of the RHS to the one on the LHS. Although this might be intentional, in many situations it will just mask an erroneously forgotten Set.</value>
435435
</data>
436+
<data name="ImplicitlyTypedConstInspection" xml:space="preserve">
437+
<value>If the type clause for a Const isn't included it is implicitly typed. Include an explicit ' As &lt;Type&gt;' replacing '&lt;Type&gt;' with the correct data type to explicitly type your const value.</value>
438+
</data>
436439
</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
@@ -437,4 +437,7 @@
437437
<data name="SuspiciousLetAssignmentInspection" xml:space="preserve">
438438
<value>Suspicious Let assignment</value>
439439
</data>
440+
<data name="ImplicitlyTypedConstInspection" xml:space="preserve">
441+
<value>Implicitly typed Const</value>
442+
</data>
440443
</root>

Rubberduck.Resources/Inspections/InspectionResults.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/InspectionResults.resx

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -498,4 +498,7 @@ In memoriam, 1972-2018</value>
498498
<value>There is an assignment from the default member of the result of expression '{1}' to that of the expression '{0}'.</value>
499499
<comment>{0} lhsExpression; {1} rhsExpression</comment>
500500
</data>
501-
</root>
501+
<data name="ImplicitlyTypedConstInspection" xml:space="preserve">
502+
<value>Constant '{0}' is implicitly typed.</value>
503+
</data>
504+
</root>

0 commit comments

Comments
 (0)