/
EmptyDoWhileBlockInspection.cs
64 lines (60 loc) · 2.17 KB
/
EmptyDoWhileBlockInspection.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
using Antlr4.Runtime.Misc;
using Rubberduck.Inspections.Abstract;
using Rubberduck.Inspections.Results;
using Rubberduck.Parsing.Common;
using Rubberduck.Parsing.Grammar;
using Rubberduck.Parsing.Inspections.Abstract;
using Rubberduck.Resources.Inspections;
using Rubberduck.Parsing.VBA;
using System.Collections.Generic;
using System.Linq;
using Rubberduck.Resources.Experimentals;
using Rubberduck.Inspections.Inspections.Extensions;
namespace Rubberduck.Inspections.Concrete
{
/// <summary>
/// Identifies empty 'Do...Loop While' blocks that can be safely removed.
/// </summary>
/// <why>
/// Dead code should be removed. A loop without a body is usually redundant.
/// </why>
/// <example>
/// <![CDATA[
/// Public Sub DoSomething(ByVal foo As Long)
/// Do
/// ' no executable statement...
/// Loop While foo < 100
/// End Sub
/// ]]>
/// </example>
/// <example>
/// <![CDATA[
/// Public Sub DoSomething(ByVal foo As Long)
/// Do
/// Debug.Print foo
/// Loop While foo < 100
/// End Sub
/// ]]>
/// </example>
[Experimental(nameof(ExperimentalNames.EmptyBlockInspections))]
internal class EmptyDoWhileBlockInspection : ParseTreeInspectionBase
{
public EmptyDoWhileBlockInspection(RubberduckParserState state)
: base(state) { }
protected override IEnumerable<IInspectionResult> DoGetInspectionResults()
{
return Listener.Contexts
.Where(result => !result.IsIgnoringInspectionResultFor(State.DeclarationFinder, AnnotationName))
.Select(result =>
new QualifiedContextInspectionResult(this, InspectionResults.EmptyDoWhileBlockInspection, result));
}
public override IInspectionListener Listener { get; } = new EmptyDoWhileBlockListener();
public class EmptyDoWhileBlockListener : EmptyBlockInspectionListenerBase
{
public override void EnterDoLoopStmt([NotNull] VBAParser.DoLoopStmtContext context)
{
InspectBlockForExecutableStatements(context.block(), context);
}
}
}
}