1
1
using System . Collections . Generic ;
2
2
using System . Linq ;
3
3
using Rubberduck . Inspections . Abstract ;
4
+ using Rubberduck . Inspections . Inspections . Abstract ;
4
5
using Rubberduck . Inspections . Inspections . Extensions ;
5
6
using Rubberduck . Inspections . Results ;
6
7
using Rubberduck . Parsing . Grammar ;
@@ -77,53 +78,47 @@ namespace Rubberduck.CodeAnalysis.Inspections.Concrete
77
78
/// End Sub
78
79
/// ]]>
79
80
/// </example>
80
- public class ArgumentWithIncompatibleObjectTypeInspection : InspectionBase
81
+ public class ArgumentWithIncompatibleObjectTypeInspection : ArgumentReferenceInspectionFromDeclarationsBase
81
82
{
82
- private readonly IDeclarationFinderProvider _declarationFinderProvider ;
83
83
private readonly ISetTypeResolver _setTypeResolver ;
84
84
85
85
public ArgumentWithIncompatibleObjectTypeInspection ( RubberduckParserState state , ISetTypeResolver setTypeResolver )
86
86
: base ( state )
87
87
{
88
- _declarationFinderProvider = state ;
89
88
_setTypeResolver = setTypeResolver ;
90
89
91
90
//This will most likely cause a runtime error. The exceptions are rare and should be refactored or made explicit with an @Ignore annotation.
92
91
Severity = CodeInspectionSeverity . Error ;
93
92
}
94
93
95
- protected override IEnumerable < IInspectionResult > DoGetInspectionResults ( )
94
+ protected override IEnumerable < Declaration > ObjectionableDeclarations ( DeclarationFinder finder )
96
95
{
97
- var finder = _declarationFinderProvider . DeclarationFinder ;
98
-
99
- var strictlyTypedObjectParameters = finder . DeclarationsWithType ( DeclarationType . Parameter )
100
- . Where ( ToBeConsidered )
101
- . OfType < ParameterDeclaration > ( ) ;
102
-
103
- var offendingArguments = strictlyTypedObjectParameters
104
- . SelectMany ( param => param . ArgumentReferences )
105
- . Select ( argumentReference => ArgumentReferenceWithArgumentTypeName ( argumentReference , finder ) )
106
- . Where ( argumentReferenceWithTypeName => argumentReferenceWithTypeName . argumentTypeName != null
107
- && ! ArgumentPossiblyLegal (
108
- argumentReferenceWithTypeName . argumentReference . Declaration ,
109
- argumentReferenceWithTypeName . argumentTypeName ) ) ;
110
-
111
- return offendingArguments
112
- // Ignoring the Declaration disqualifies all assignments
113
- . Where ( argumentReferenceWithTypeName => ! argumentReferenceWithTypeName . Item1 . Declaration . IsIgnoringInspectionResultFor ( AnnotationName ) )
114
- . Select ( argumentReference => InspectionResult ( argumentReference , _declarationFinderProvider ) ) ;
96
+ return finder . DeclarationsWithType ( DeclarationType . Parameter )
97
+ . Where ( ToBeConsidered ) ;
115
98
}
116
99
117
100
private static bool ToBeConsidered ( Declaration declaration )
118
101
{
119
- return declaration != null
120
- && declaration . AsTypeDeclaration != null
102
+ return declaration ? . AsTypeDeclaration != null
121
103
&& declaration . IsObject ;
122
104
}
123
105
124
- private ( IdentifierReference argumentReference , string argumentTypeName ) ArgumentReferenceWithArgumentTypeName ( IdentifierReference argumentReference , DeclarationFinder finder )
106
+ protected override ( bool isResult , object properties ) IsUnsuitableArgumentWithAdditionalProperties ( ArgumentReference reference , DeclarationFinder finder )
125
107
{
126
- return ( argumentReference , ArgumentSetTypeName ( argumentReference , finder ) ) ;
108
+ var argumentSetTypeName = ArgumentSetTypeName ( reference , finder ) ;
109
+
110
+ if ( argumentSetTypeName == null || ArgumentPossiblyLegal ( reference . Declaration , argumentSetTypeName ) )
111
+ {
112
+ return ( false , null ) ;
113
+ }
114
+
115
+ return ( true , argumentSetTypeName ) ;
116
+ }
117
+
118
+ protected override bool IsUnsuitableArgument ( ArgumentReference reference , DeclarationFinder finder )
119
+ {
120
+ //No need to implement this since we overwrite IsUnsuitableArgumentWithAdditionalProperties.
121
+ throw new System . NotImplementedException ( ) ;
127
122
}
128
123
129
124
private string ArgumentSetTypeName ( IdentifierReference argumentReference , DeclarationFinder finder )
@@ -146,7 +141,7 @@ private bool ArgumentPossiblyLegal(Declaration parameterDeclaration , string ass
146
141
|| HasSubType ( parameterDeclaration , assignedTypeName ) ;
147
142
}
148
143
149
- private bool HasBaseType ( Declaration declaration , string typeName )
144
+ private static bool HasBaseType ( Declaration declaration , string typeName )
150
145
{
151
146
var ownType = declaration . AsTypeDeclaration ;
152
147
if ( ownType == null || ! ( ownType is ClassModuleDeclaration classType ) )
@@ -157,7 +152,7 @@ private bool HasBaseType(Declaration declaration, string typeName)
157
152
return classType . Subtypes . Select ( subtype => subtype . QualifiedModuleName . ToString ( ) ) . Contains ( typeName ) ;
158
153
}
159
154
160
- private bool HasSubType ( Declaration declaration , string typeName )
155
+ private static bool HasSubType ( Declaration declaration , string typeName )
161
156
{
162
157
var ownType = declaration . AsTypeDeclaration ;
163
158
if ( ownType == null || ! ( ownType is ClassModuleDeclaration classType ) )
@@ -168,20 +163,12 @@ private bool HasSubType(Declaration declaration, string typeName)
168
163
return classType . Supertypes . Select ( supertype => supertype . QualifiedModuleName . ToString ( ) ) . Contains ( typeName ) ;
169
164
}
170
165
171
- private IInspectionResult InspectionResult ( ( IdentifierReference argumentReference , string argumentTypeName ) argumentReferenceWithTypeName , IDeclarationFinderProvider declarationFinderProvider )
172
- {
173
- var ( argumentReference , argumentTypeName ) = argumentReferenceWithTypeName ;
174
- return new IdentifierReferenceInspectionResult ( this ,
175
- ResultDescription ( argumentReference , argumentTypeName ) ,
176
- declarationFinderProvider ,
177
- argumentReference ) ;
178
- }
179
-
180
- private string ResultDescription ( IdentifierReference argumentReference , string argumentTypeName )
166
+ protected override string ResultDescription ( IdentifierReference reference , dynamic properties = null )
181
167
{
182
- var parameterName = argumentReference . Declaration . IdentifierName ;
183
- var parameterTypeName = argumentReference . Declaration . FullAsTypeName ;
184
- var argumentExpression = argumentReference . Context . GetText ( ) ;
168
+ var parameterName = reference . Declaration . IdentifierName ;
169
+ var parameterTypeName = reference . Declaration . FullAsTypeName ;
170
+ var argumentExpression = reference . Context . GetText ( ) ;
171
+ var argumentTypeName = ( string ) properties ;
185
172
return string . Format ( InspectionResults . SetAssignmentWithIncompatibleObjectTypeInspection , parameterName , parameterTypeName , argumentExpression , argumentTypeName ) ;
186
173
}
187
174
}
0 commit comments