/
FunctionReturnValueNotUsedInspection.cs
176 lines (155 loc) · 7.5 KB
/
FunctionReturnValueNotUsedInspection.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
using System;
using System.Collections.Generic;
using System.Linq;
using Rubberduck.Inspections.Abstract;
using Rubberduck.Inspections.Results;
using Rubberduck.Parsing;
using Rubberduck.Parsing.Grammar;
using Rubberduck.Parsing.Inspections;
using Rubberduck.Parsing.Inspections.Abstract;
using Rubberduck.Resources.Inspections;
using Rubberduck.Parsing.Symbols;
using Rubberduck.Parsing.VBA;
using Rubberduck.VBEditor;
namespace Rubberduck.Inspections.Concrete
{
// bug: quick fix for converting to sub is exposed for interface members now
public sealed class FunctionReturnValueNotUsedInspection : InspectionBase
{
public FunctionReturnValueNotUsedInspection(RubberduckParserState state)
: base(state) { }
protected override IEnumerable<IInspectionResult> DoGetInspectionResults()
{
// Note: This inspection does not find dictionary calls (e.g. foo!bar) since we do not know what the
// default member is of a class.
var interfaceMembers = State.DeclarationFinder.FindAllInterfaceMembers().ToList();
var interfaceImplementationMembers = State.DeclarationFinder.FindAllInterfaceImplementingMembers();
var functions = State.DeclarationFinder
.UserDeclarations(DeclarationType.Function)
.Where(item => !IsIgnoringInspectionResultFor(item, AnnotationName) &&
item.References.Any(r => !IsReturnStatement(item, r) && !r.IsAssignment))
.ToList();
var interfaceMemberIssues = GetInterfaceMemberIssues(interfaceMembers);
var nonInterfaceFunctions = functions.Except(interfaceMembers.Union(interfaceImplementationMembers));
var nonInterfaceIssues = GetNonInterfaceIssues(nonInterfaceFunctions);
return interfaceMemberIssues.Union(nonInterfaceIssues);
}
private IEnumerable<IInspectionResult> GetInterfaceMemberIssues(IEnumerable<Declaration> interfaceMembers)
{
return from interfaceMember in interfaceMembers
let implementationMembers =
State.DeclarationFinder.FindInterfaceImplementationMembers(interfaceMember).ToList()
where interfaceMember.DeclarationType == DeclarationType.Function &&
!IsReturnValueUsed(interfaceMember) &&
implementationMembers.All(member => !IsReturnValueUsed(member))
let implementationMemberIssues =
implementationMembers.Select(
implementationMember =>
Tuple.Create(implementationMember.Context,
new QualifiedSelection(implementationMember.QualifiedName.QualifiedModuleName,
implementationMember.Selection), implementationMember))
select CreateInspectionResult(this, interfaceMember);
}
private IEnumerable<IInspectionResult> GetNonInterfaceIssues(IEnumerable<Declaration> nonInterfaceFunctions)
{
var returnValueNotUsedFunctions = nonInterfaceFunctions.Where(function => function.DeclarationType == DeclarationType.Function && !IsReturnValueUsed(function));
var nonInterfaceIssues = returnValueNotUsedFunctions
.Where(function => !IsRecursive(function))
.Select(function =>
new DeclarationInspectionResult(
this,
string.Format(InspectionResults.FunctionReturnValueNotUsedInspection, function.IdentifierName),
function));
return nonInterfaceIssues;
}
private bool IsRecursive(Declaration function)
{
return function.References.Any(usage => usage.ParentScoping.Equals(function) && IsIndexExprOrCallStmt(usage));
}
private bool IsReturnValueUsed(Declaration function)
{
// TODO: This is O(MG) at work here. Need to refactor the whole shebang.
return (from usage in function.References
where !IsLet(usage)
where !IsSet(usage)
where !IsCallStmt(usage)
where !IsTypeOfExpression(usage)
where !IsAddressOfCall(usage)
select usage).Any(usage => !IsReturnStatement(function, usage));
}
private bool IsAddressOfCall(IdentifierReference usage)
{
return usage.Context.IsDescendentOf<VBAParser.AddressOfExpressionContext>();
}
private bool IsTypeOfExpression(IdentifierReference usage)
{
return usage.Context.IsDescendentOf<VBAParser.TypeofexprContext>();
}
private bool IsReturnStatement(Declaration function, IdentifierReference assignment)
{
return assignment.ParentScoping.Equals(function) && assignment.Declaration.Equals(function);
}
private bool IsIndexExprOrCallStmt(IdentifierReference usage)
{
return IsCallStmt(usage) || IsIndexExprContext(usage);
}
private bool IsCallStmt(IdentifierReference usage)
{
var callStmt = usage.Context.GetAncestor<VBAParser.CallStmtContext>();
if (callStmt == null)
{
return false;
}
var indexExpr = usage.Context.GetAncestor<VBAParser.IndexExprContext>();
if (indexExpr != null)
{
var memberAccessStmt = usage.Context.GetAncestor<VBAParser.MemberAccessExprContext>();
if (memberAccessStmt != null &&
callStmt.SourceInterval.ProperlyContains(memberAccessStmt.SourceInterval) &&
memberAccessStmt.SourceInterval.ProperlyContains(indexExpr.SourceInterval))
{
return false;
}
}
var argumentList = CallStatement.GetArgumentList(callStmt);
if (argumentList == null)
{
return true;
}
return !usage.Context.IsDescendentOf(argumentList);
}
private bool IsIndexExprContext(IdentifierReference usage)
{
var indexExpr = usage.Context.GetAncestor<VBAParser.IndexExprContext>();
if (indexExpr == null)
{
return false;
}
var argumentList = indexExpr.argumentList();
if (argumentList == null)
{
return true;
}
return !usage.Context.IsDescendentOf(argumentList);
}
private bool IsLet(IdentifierReference usage)
{
var letStmt = usage.Context.GetAncestor<VBAParser.LetStmtContext>();
return letStmt != null && letStmt == usage.Context;
}
private bool IsSet(IdentifierReference usage)
{
var setStmt = usage.Context.GetAncestor<VBAParser.SetStmtContext>();
return setStmt != null && setStmt == usage.Context;
}
private DeclarationInspectionResult CreateInspectionResult(IInspection inspection, Declaration interfaceMember)
{
dynamic properties = new PropertyBag();
properties.DisableFixes = nameof(QuickFixes.ConvertToProcedureQuickFix);
return new DeclarationInspectionResult(inspection,
string.Format(InspectionResults.FunctionReturnValueNotUsedInspection,
interfaceMember.IdentifierName),
interfaceMember, properties: properties);
}
}
}